You need to set up the connection to your database first.
I have three files, the main PHP script like yours, a db_config file:
<?php
/**
* Database config variables
*/
define("DB_SERVER", "%address%");
define("DB_USER", "%username%");
define("DB_PASSWORD", "%password%");
define("DB_DATABASE", "%dbname%");
?>
Then a connect utility script:
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
}
}
?>
Then you can use this in your main script to open the connection:
// include db connect class
require_once __DIR__ . '/db_connect.php';
at the beginning to open the connection to your database. You can then do a query as you did in your question above. This format makes it nice and easy to have queries going from multiple PHP scripts as the connector a separate class.
You can then do the logic for executing that SQL query when the button is clicked
For removing the image URL you want something like
UPDATE `users` SET `profile` = NULL WHERE `user_id` = $user_id
You can't directly access the button click as it were. The PHP is just telling the client page to display a button. If you want to receieve when it was clicked you need to use something like a POST self-referral like below to make a button which when clicked sends a signal back to the server page:
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="submit" name="submit" value="Click Me">
</form>
Then, at the top of your PHP page you can do
if(isset($_POST['submit'])) {
deleteimage($user_id);
}
To run the function to do the URL delete function only once the button is clicked.
To migrate your script to MYSQLI for future compatibility, take a look at the oracle wiki guide here.
It has a tool which you point at your PHP scripts and it will make the changes for you. Took me about 2 mins to convert all my scripts
Hope this is helpful