Delete a record in a MySql table with PHP

 

If you want to delete a record in a table, use the DELETE Keyword. Like this:

$SQL = "DELETE FROM AddressBook WHERE First_Name = 'Bill' AND Surname = 'Gates'";

After the DELETE word, you need FROM. Then you type the name of the table. Next, you need to specify which record you want to delete. It's a good idea to make sure your WHERE clause is going to be a unique value. In the code above, we might have more than one Bill Gates in the table. If we do, everybody called Bill Gates will be deleted! A better solution is to use a unique field from your table, such as an ID field:

$SQL = "DELETE FROM AddressBook WHERE ID = '7' ";

Now, only the record that has number 7 in the ID field will be deleted.

 

In the next part, we'll take a closer look at WHERE.