9 - PHP:MySQL - Delete
This is the page that actually deletes the record, and again, this is a page the user never sees. It simply sits on the server and does its job when called upon.
copy from just below this line....
<?
// Check to see if the variable $id has a value. If it
doesn't, most likely the user came to this page
// directly, and shouldn't be here right now. If that's the case, give them
an error, else continue.
if ($id <> '')
{
//Define the connection string, as usual
$conn = mysql_connect("localhost", "username","password")
or die("Could not connect to database");
//Connect to "the database" with $theConnectionString
mysql_select_db("databaseName") or die("Could
not select database");
// Here we right our SQL statement. Note the syntax of
using the variable $id.
// And remember, that variable was passed to this page by the query string built
into the URL that brought us here.
$query = "DELETE FROM userInfo WHERE id = '$id'";
// With $query as the argument for mysql_query, the SQL
statement gets run. If it runs successfully
// continue, else give an error
if (mysql_query($query))
{
//This header function has to be used carefully. You usually can't put it in
the body.
// This line sends us back to our first page if the SQL
command executed successfully.
header("Location:php9_DeleteData.php");
// Also, it's good form to use "exit;" after
using header
exit;
}
else
{
echo("<P>Error deleting record" .
mysql_error() . "</P>");
}
//Close your connection.
mysql_close($conn);
}
else
{
// This is what the user gets if they come here directly
and $id has no value.
echo (" You shouldn't be here");
}
?>
... to just above this line.