The SQL DELETE Statement is used to delete exisiting database records.
The SQL DELETE Statement is shown below:
For more a more in depth look at the DELETE Statement see section 10.
The following represents a user record that will be deleted from a Personnel Database table. Clicking a Delete button calls a PHP rountine that executes an SQL DELETE statement to remove this record from the database table.
In addition to the form controls shown above, the page also includes a hidden textbox named "AutoNum" with a value equal to AutoNum field of the database table. This field is used to uniquely identify each record. The following code demonstrates how the page works:
<?php
if ($_POST['submitb']=="Delete Record")
{
$conn = odbc_connect('Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:\path\to\database.mdb','','');
$sqlDelete = "DELETE FROM Personnel WHERE AutoNum =" . $_POST['AutoNum'];
$rsDelete = odbc_exec($conn,$sqlDelete);
if(odbc_num_rows($rsDelete) == 1)
{
echo "Record successfully deleted!";
}
odbc_close($conn);
}
?>
After the "Delete Record" button is clicked, a connection is established with the Access Database. Next, an SQL DELETE statement is issued to delete the record from Personnel table with an AutoNum field value equal to the value of the AutoNum hidden textbox. The SQL statement is then executed. The results of the odbc_exec() function are assigned to the $rsDelete variable. The last step is to verify that the record deletion was a success and display a confirmation message. The odbc_num_rows() function is used to determine the number of rows in an ODBC result or the number of rows affected by the odbc_exec() statement. Since a single record is being deleted, if the result of odbc_num_rows() is equal to 1, the record was deleted successfully. Finally, the connection to the database connection is closed.