The Delete operation in CRUD (Create, Read, Update, Delete) is used to permanently remove records from a MySQL database. In PHP, deletion is typically performed using the DELETE FROM SQL statement with MySQLi or PDO.
The basic SQL syntax used in PHP for deleting records:
DELETE FROM table_name WHERE condition;
// PHP script to delete a record from MySQL using MySQLi
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM users WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
If the record with id = 1 exists, it will be removed from the users table and a success message will be displayed.
This JavaScript example simulates a delete confirmation before running a delete action.
// Confirmation dialog before deleting a record
function confirmDelete() {
return confirm("Are you sure you want to delete this record?");
}
deleted column