PHP CHAPTER 40

MySQL Delete Data

DELETE removes records from a database table.

📘

What is it?

DELETE removes records from a database table.

💡

Why do we use it?

It allows administrators or applications to remove unwanted records.

⚙️

How does it work?

DELETE uses a WHERE condition to identify the rows to remove.

🧩

Syntax

PHP Syntax
DELETE FROM students WHERE id=?;
💻

Example

PHP Example
<?php

$conn = new mysqli("localhost", "root", "", "aifa_php_lab");

$id = 1;
$stmt = $conn->prepare("DELETE FROM students WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();

echo "Student Deleted";

?>
🔍

Example Explained

The prepared statement targets the student with the specified ID and deletes it.
🚀

More Examples

Build a confirmation page before executing the delete operation.

⚠️ Important Rules

Always use a precise WHERE condition for record deletion.

❌ Common Mistakes

Executing DELETE without WHERE and accidentally removing every record.

🌍 Real-World Use

Admin panels and record management systems.

📝 Practice Exercise

Create a safe Delete Student page.

💻 Try It Yourself

Write and run this PHP lesson inside the AIFA Coding Lab.

Open Coding Lab →