Table of Contents:
PHP MySQL Delete Data – Complete Beginner Tutorial
Deleting data from a database is an important operation in PHP-MySQL applications. Whether you are building an admin panel, user management system, or content management system, you must be able to safely remove unwanted records.
This tutorial explains how to delete data from MySQL using PHP with simple examples and real output.
What Does Delete Data Mean in PHP MySQL?
Deleting data means removing one or more rows from a MySQL table using the SQL DELETE statement.
Basic SQL syntax:
DELETE FROM table_name WHERE condition;
The WHERE clause is critical to ensure only specific records are deleted.
Why WHERE Clause Is Important When Deleting Data
Using DELETE without a WHERE clause will remove all records from the table.
Example (dangerous):
DELETE FROM users;
Always double-check your conditions before executing DELETE queries.
Prerequisites for PHP MySQL Delete Tutorial
Before deleting data, ensure you have:
- PHP installed
- MySQL database
- Database connection file
- A table with existing records
Sample MySQL Table Used in Examples
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
age INT
);
PHP MySQL Database Connection Code
<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");
if (!$conn) {
die("Database connection failed");
}
?>
Delete Single Record Using PHP MySQL
<?php
$query = "DELETE FROM users WHERE id = 1";
if (mysqli_query($conn, $query)) {
echo "Record deleted successfully";
}
?>
Output:
Record deleted successfully
Delete Data Using PHP Variable
<?php
$id = 2;
$query = "DELETE FROM users WHERE id = $id";
mysqli_query($conn, $query);
echo "User deleted";
?>
Output:
User deleted
Delete Multiple Records Using WHERE Condition
<?php
$query = "DELETE FROM users WHERE age < 25";
mysqli_query($conn, $query);
echo "Multiple records deleted";
?>
Output:
Multiple records deleted
Delete Data Using Form in PHP MySQL
HTML Form
<form method="post">
Enter User ID: <input type="number" name="id">
<input type="submit" name="delete">
</form>
PHP Delete Script
<?php
if (isset($_POST['delete'])) {
$id = $_POST['id'];
$query = "DELETE FROM users WHERE id = $id";
if (mysqli_query($conn, $query)) {
echo "Data deleted using form";
}
}
?>
Output:
Data deleted using form
Secure PHP MySQL Delete Using Prepared Statement
Prepared statements protect against SQL injection.
<?php
$stmt = mysqli_prepare($conn, "DELETE FROM users WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $id);
$id = 3;
mysqli_stmt_execute($stmt);
echo "Record deleted securely";
?>
Output:
Record deleted securely
Delete All Records from Table (Warning)
<?php
mysqli_query($conn, "DELETE FROM users");
echo "All records deleted";
?>
This removes all data from the table. Use carefully.
Difference Between DELETE and TRUNCATE
| Feature | DELETE | TRUNCATE |
|---|---|---|
| WHERE support | Yes | No |
| Rollback | Possible | No |
| Speed | Slower | Faster |
| Auto Increment reset | No | Yes |
PHP MySQL delete data, PHP delete record MySQL, PHP MySQL DELETE query, PHP MySQLi delete example, PHP delete form data MySQL, PHP database tutorial
Common Mistakes When Deleting Data in PHP MySQL
- Forgetting WHERE clause
- Deleting wrong records
- Allowing user input without validation
- Not using prepared statements
Best Practices for PHP MySQL Delete Operations
- Always confirm before deleting
- Use prepared statements
- Use WHERE clause carefully
- Backup important data
- Log delete actions
Related tutorial:
Frequently Asked Questions (FAQ)
1. What does DELETE do in MySQL?
It removes records from a table.
2. Can DELETE be undone?
Only if using transactions or backups.
3. Is WHERE clause mandatory?
Not mandatory, but highly recommended.
4. Is prepared DELETE safer?
Yes, it prevents SQL injection.
5. Can I delete multiple records at once?
Yes, using conditions in WHERE clause.