It is very time-consuming to delete records one by one from a large amount of data in the list. We can make it easier by deleting multiple records at once. With the bulk delete feature, you can make the large data list user-friendly by allowing the user to delete multiple rows with a single click. Delete multiple records with a single click is strongly recommended for the data management section in the web application.
You can use the checkbox to select each record and delete all the selected rows after the remove request submission. Also, jQuery can be used to select or unselect all checkboxes at once in the data list. In this tutorial, we will show you how to delete multiple records from MySQL database in PHP with checkbox.
In the example code, we will implement the following operations to delete multiple records using checkboxes in PHP.
A table needs to be created in the database to store the user’s data. The following SQL creates a users
table with some basic fields in the MySQL database.
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`email` varchar(200) NOT NULL,
`phone` varchar(15) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '1=Active, 0=Deactive',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
The dbConfig.php
file is used to connect and select the database. Specify the database hostname ($dbHost
), username ($dbUsername
), password ($dbPassword
), and name ($dbName
) as per your MySQL database credentials.
<?php
// Database configuration
$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = "root";
$dbName = "codexworld";
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
In this file, we will fetch all the records from the users
table and list them with checkboxes in an HTML table.
JavaScript Code:
jQuery is used to integrate the delete confirmation dialog and select all checkboxes functionality. So, include the jQuery library first.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
The delete_confirm()
function checks whether the user selects at least one checkbox and display an alert if the user has not checked any checkbox using JavaScript. Also, displays a confirmation popup before submitting the delete request to the server-side script.
function delete_confirm(){ if($('.checkbox:checked').length > 0){ var result = confirm("Are you sure to delete selected users?"); if(result){ return true; }else{ return false; } }else{ alert('Select at least 1 record to delete.'); return false; } }
The following jQuery code is used to implement the Select / Deselect All CheckBoxes functionality. It helps the user to check / uncheck all checkboxes in the table with a single click.
$(document).ready(function(){ $('#select_all').on('click',function(){ if(this.checked){ $('.checkbox').each(function(){ this.checked = true; }); }else{ $('.checkbox').each(function(){ this.checked = false; }); } }); $('.checkbox').on('click',function(){ if($('.checkbox:checked').length == $('.checkbox').length){ $('#select_all').prop('checked',true); }else{ $('#select_all').prop('checked',false); } }); });
HTML & PHP Code:
The user can check single or multiple records using checkbox in the table and delete multiple records from the MySQL database.
dbConfig.php
file to connect the MySQL database.<!-- Display the status message --> <?php if(!empty($statusMsg)){ ?> <div class="alert alert-success"><?php echo $statusMsg; ?></div> <?php } ?> <!-- Users data list --> <form name="bulk_action_form" action="delete_submit.php" method="post" onSubmit="return delete_confirm();"/> <table class="bordered"> <thead> <tr> <th><input type="checkbox" id="select_all" value=""/></th> <th>First Name</th> <th>Last Name</th> <th>Email</th> <th>Phone</th> </tr> </thead> <?php
// Include the database configuration file
include_once 'dbConfig.php';
// Get users from the database
$query = $db->query("SELECT * FROM users ORDER BY id DESC");
// List all records
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
?> <tr> <td><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $row['id']; ?>"/></td> <td><?php echo $row['first_name']; ?></td> <td><?php echo $row['last_name']; ?></td> <td><?php echo $row['email']; ?></td> <td><?php echo $row['phone']; ?></td> </tr> <?php } }else{ ?> <tr><td colspan="5">No records found.</td></tr> <?php } ?> </table> <input type="submit" class="btn btn-danger" name="bulk_delete_submit" value="Delete"/> </form>
After the confirmation, the form is submitted to the server-side script (delete_submit.php
) for proceeding with the delete request.
The delete_submit.php
file handles the multiple records delete operations with PHP and MySQL.
<?php
// Include the database configuration file
include_once 'dbConfig.php';
// If record delete request is submitted
if(isset($_POST['bulk_delete_submit'])){
// If id array is not empty
if(!empty($_POST['checked_id'])){
// Get all selected IDs and convert it to a string
$idStr = implode(',', $_POST['checked_id']);
// Delete records from the database
$delete = $db->query("DELETE FROM users WHERE id IN ($idStr)");
// If delete is successful
if($delete){
$statusMsg = 'Selected users have been deleted successfully.';
}else{
$statusMsg = 'Some problem occurred, please try again.';
}
}else{
$statusMsg = 'Select at least 1 record to delete.';
}
}
?>
PHP CRUD Operations with MySQLi Extension
The multiple records removal functionality is very useful for the data list. You can use bulk delete functionality in the data management section of your web application. It will make your web application user-friendly because the user doesn’t need to click multiple times to delete multiple records from the database. Using our example code you can check/uncheck all records at once and delete all records from the database on a single click using PHP.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
I cant get this code working but at the of this you say about the The action.php but in this how to that is the first and last you say thanks for all your code
thank you very much … great simple and easy way to code.
Thanks Man ! 🙂
Better tuts…
Thanks for giving a wonderful post.
thanks alot for the strong support I used this code and it is working without any disappointing.
this is very helpful thank you
Thanks for the codes.
It is what i was looking for. Instead of deleting, i want to add the selection to the database.
we check all first then all checkbox are selected but if we check single checkbox then all other selected checkbox should be uncheck…for that suggest me
@Kripal
This functionality has included into the latest script. Please check the updated JavaScript code or download the latest version.
Thanks for such a clear explanation
Thanks for such a clear explanation. It really helped me in my project. I got a thorough understanding of the coding concept now. Great Website!