Delete multiple records in PHP using the checkbox

Pinterest LinkedIn Tumblr

Hi, are you looking for the code in which you want to delete the multiple records using a checkbox? In this tutorial, you will learn to delete multiple records in PHP using the checkbox or selected checkbox.

In the previous tutorial, you learned about how to delete a single record. Here you learn about to delete the multiple records by selected checkbox with validation to select at least one checkbox. Here are the files and folders to delete multiple records in PHP. Check this demo.


So without wasting our time, let’s get started to create our files.


<!DOCTYPE html>
<html lang="en">
    <title>Delete records from database using PHP - Coding Birds Online</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src="validation.js"></script>
    <script src=""></script>
    <link rel="icon" href="" type="image/x-icon">
        #thead>tr>th{ color: white; }
<div style="margin-top: 20px;padding-bottom: 20px;">
        <img width="100" src=""/>
        <h3>Delete multiple records from database using PHP </h3>
<div class="container">
    <form action="delete-script.php" method="post">
    <table class="table table-bordered">
        <thead id="thead" style="background-color:#1573ff">
        include "config.php";
        include_once "Common.php";
        $common = new Common();
        $records = $common->getAllRecords($connection);
        if ($records->num_rows>0){
            $sr = 1;
            while ($record = $records->fetch_object()) {
                $recordId = $record->id;
                $name = $record->name;
                $email = $record->email;
                $contact = $record->contact;
                $department = $record->department;?>
                    <td><?php echo $sr;?></td>
                    <td><?php echo $name;?></td>
                    <td><?php echo $email;?></td>
                    <td><?php echo $contact;?></td>
                    <td><?php echo $department;?></td>
                    <td><input type="checkbox" name="recordsCheckBox[]" id="recordsCheckBox" value="<?php echo $recordId;?>"></td>
        <input type="submit" name="deleteBtn" id="deleteBtn" class="btn btn-success" onclick="return validateForm();" value="Delete Records" style="float: right"/>

This index.php file includes config.php and Common.php. These files are database connections and PHP class file. Let’s create these files.


$hostName = "localhost"; // host name
$username = "root";  // database username
$password = ""; // database password
$databaseName = "codingbirds"; // database name

$connection = new mysqli($hostName,$username,$password,$databaseName);
if (!$connection) {
    die("Error in database connection". $connection->connect_error);


class Common
    public function getAllRecords($connection) {
        $query = "SELECT * FROM bird_delete_records";
        $result = $connection->query($query) or die("Error in query1".$connection->error);
        return $result;
    public function deleteRecordById($connection,$id) {
        $query = "DELETE FROM bird_delete_records WHERE id='$id'";
        $result = $connection->query($query) or die("Error in query".$connection->error);
        return $result;


function validateForm() {
    var count_checked = $("[name='recordsCheckBox[]']:checked").length;
    if (count_checked == 0) {
        alert("Please check at least one checkbox");
        return false;
        return true;


include "config.php";
include_once "Common.php";
if (isset($_POST['deleteBtn'])){
    $recordIds = $_POST['recordsCheckBox'];
    $common = new Common();
    foreach ( $recordIds as $id ) {
        $delete = $common->deleteRecordById($connection,$id);
    if ($delete) {
        echo '<script>alert("Record deleted successfully !")</script>';
        echo '<script>window.location.href="index.php";</script>';

After running this code you will get output like this.


So this is the output when you run the code. Now if you press the delete button when you get a prompt alert that, select at least one checkbox.

After selecting and pressing the delete button the records will be deleted that you have selected before. I hope you understood the concept and the code.

You can download the full working source code from here so that there is no error in your code. You can also watch this video uploaded a year ago of mine, telling the same thing but in procedural style.

Happy Coding 🙂

I am a professional Web / Software developer, B.Tech CSE, who has been working in PHP & CodeIgniter and WordPress for more than 2+ years and know it inside and out, including the creation of Web Apps, WordPress websites from scratch and their customization.


  1. Anuj Shukla Reply

    It is very helpful to delete multiple records by single click..

  2. Thank you for your expertise. Somebody like me who is not tech savvy lol. I’m always doing things the hard way.

  3. Thank you for sharing. It was so helpful so easy to understand.

  4. Helpful post! I’m not good in doing web development but this is understandable for newbie. Thanks!

  5. Coding sounds awesome, but It’s all gibberish for me. Thank you for sharing your knowledge with us!

  6. Mallory Bohling Reply

    Thank you for sharing how to delete multiple at a time, huge time saver!

  7. Amelia Pollock Reply

    Very helpful information on deleting multiple records in PHP using the checkbox. I have recently started following your blog. You keep the coding so precise and straightforward which make it easy to understand. Thank you for sharing.

  8. Marjie Mare Reply

    That is very informative. I need to do that before the end of the year.

  9. Shnooks Gf Reply

    Thank you so much for this informative post,I am really thankful for such posts they really helped me with my blogging.

  10. Nice instructional content! Though I do not have a current need for it but I will reference it here when I do. I like the you added a YouTube video to compliment your text to ensure reader can follow along as a viewer. Well done.

Write A Comment