SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
If you want to delete records from a MySQL table using PHP, you'd typically use the DELETE
SQL statement.
Here's a simple step-by-step example:
1. Connect to the MySQL database:
$servername = "your_server_name"; $username = "your_username"; $password = "your_password"; $dbname = "your_database_name"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
2. Write and Execute the DELETE Query:
Suppose you have a table named users
and you want to delete a user with the id
of 5:
$sql = "DELETE FROM users WHERE id=5"; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; }
3. Close the connection:
$conn->close();
Remember: Be very careful when using the DELETE
statement. Without a WHERE
clause, it will remove all records from the table. Always ensure you're deleting the right records to avoid unintentional data loss.
Also, for better security, consider using prepared statements to prevent SQL injection attacks.
PHP MySQL DELETE query example:
<?php $conn = new mysqli("localhost", "your_username", "your_password", "your_database"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "DELETE FROM your_table WHERE id = 1"; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; } $conn->close(); ?>
How to delete records in MySQL using PHP:
<?php $conn = new mysqli("localhost", "your_username", "your_password", "your_database"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "DELETE FROM your_table WHERE id = 1"; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; } $conn->close(); ?>
Deleting specific records with WHERE clause in PHP:
$id_to_delete = 1; $sql = "DELETE FROM your_table WHERE id = $id_to_delete";
PHP MySQL DELETE multiple rows:
$ids_to_delete = [1, 2, 3]; $id_list = implode(", ", $ids_to_delete); $sql = "DELETE FROM your_table WHERE id IN ($id_list)";
Deleting records with user input in PHP and MySQL:
$user_input = $_POST['delete_value']; $sql = "DELETE FROM your_table WHERE column_name = '$user_input'";
Confirming deletion with a prompt in PHP:
// Add a confirmation prompt in your HTML/JS // Example: <button onclick="confirmDelete()">Delete</button> function confirmDelete() { if (confirm("Are you sure you want to delete this record?")) { // Proceed with the deletion // Make an AJAX call or submit a form to trigger the PHP script } }
Using DELETE query with JOIN in PHP:
$sql = "DELETE your_table, related_table FROM your_table LEFT JOIN related_table ON your_table.id = related_table.your_table_id WHERE your_table.id = 1";
Deleting records based on date in PHP and MySQL:
$date_to_delete = "2023-01-01"; $sql = "DELETE FROM your_table WHERE date_column <= '$date_to_delete'";
Handling foreign key constraints with DELETE in PHP:
$conn->query("SET foreign_key_checks = 0"); $sql = "DELETE FROM your_table WHERE id = 1"; $conn->query("SET foreign_key_checks = 1");
PHP MySQL DELETE query for soft deletes:
$sql = "UPDATE your_table SET status = 'deleted' WHERE id = 1";
Deleting records with AJAX and PHP:
// PHP script (delete_record.php) <?php $id_to_delete = $_POST['id']; $sql = "DELETE FROM your_table WHERE id = $id_to_delete"; // Execute the query and return a response ?> // JavaScript (using AJAX) function deleteRecord(id) { if (confirm("Are you sure you want to delete this record?")) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { // Handle the response, e.g., update UI } }; xhr.open("POST", "delete_record.php", true); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.send("id=" + id); } }
PHP DELETE query error handling:
if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; }