Learn PHP Basics: Master CRUD Operations to Build Dynamic Applications
About Lesson

7. Remove data (Delete) 

Delete doesn’t really have a special form for itself. Instead, when you click the delete button, a JavaScript confirmation will show up. If confirmed, then we will be redirected to delete.php which just executes the sql statement for deleting data and redirecting back to index.

Create the file: delete.php. Paste in the following code:

<?php 
include('connect.php'); 

// retrieving the data to be deleted by id 
$id = $_GET['id'];

// deleting the data 
$sql = "DELETE FROM contacts WHERE id= ?"; 
$row = $connection->prepare($sql); 
$row->execute([$id]); 

// redirect to index 
echo '<script> alert("Contact deleted"); window.location="index.php" </script>'; 
?>

When you click the delete button, you’ll be asked to confirm. If you proceed, your data will be deleted.