PHP MySQL Delete Data


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

FeatureDELETETRUNCATE
WHERE supportYesNo
RollbackPossibleNo
SpeedSlowerFaster
Auto Increment resetNoYes

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.

Related Article
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

PHP Tutorial – Complete Guide for Beginners to Advanced Welcome to the most comprehensive PHP tutorial available online at PHPOnline.in Read more

Introduction of PHP

Introduction to PHP – Learn PHP from Scratch with Practical Examples Welcome to your complete beginner's guide to PHP. Whether Read more

Syntax Overview of PHP

Syntax Overview of PHP (2025 Edition) Welcome to phponline.in, your one-stop platform for mastering PHP. This comprehensive, SEO-rich tutorial on Read more

Environment Setup in PHP

Setting Up PHP Environment (Beginner’s Guide) If you’re planning to learn PHP or start developing websites using PHP, the first Read more

Variable Types in PHP

PHP Variable Types: Complete Beginner's Guide to PHP Data Types Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP Read more