Oj Obasi
Oj Obasi

I am also and will continue to be a student of web design and development. My tutorials may not be for you. If you feel that way, then search online for better tutorials.

PHP PDO Delete Data From a MySQL Table

The SQL DELETE statement is use to delete a record from a table. We want to remove one of our products from our 'products' table for reason that we are not more selling that product.

The product we want to delete has an id = 1(WHERE id = 1).

<?php
require 'database.php';
try{
// sql to delete a record
$sql = "DELETE FROM products WHERE id=1";
// use exec() because no results are returned
$db->exec($sql);
echo "Record deleted successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
?>

If you forget to use the WHERE id=1, all records in your database will be deleted.