Exmaple – Delete multiple records from MySQL in PHP

4

Delete records one by one is very time consuming. Multiple records delete on a single click is strongly recommended for every data management section. Today we will discuss how to delete multiple rows from MySQL database in PHP with checkbox.

In this tutorial we will display records from users table using MySQL and PHP. The checkboxes should be displayed with each row. Checkboxes would be used for select multiple records from users list. We will also provide a checkbox at the header for select all records on a single click. Once the delete button is clicked, we will display a alert popup for confirmation. After confirmation the selected records would be removed from the database.

users table:

Into the users table we have stored users first_name, last_name, email and phone fields.

To implement delete multiple records in PHP we have used 3 files, the files details are given below.

dbConfig.php:

This file is used to connect with the database.

index.php:

At first you need to include jQuery library. JavaScript delete_confirm() function is used for display delete confirmation popup.
Into the $(document).ready(function(){}); we have wrote some jQuery for implement select all checkbox functionality.

For database access we have included the dbConfig.php file. Using MySQL we will fetch the users data from users table and display the users row into table using PHP.

action.php:

Once the delete button is clicked the form would be submitted to the action.php file. In this file we will get the selected rows ID and delete records from users table depending on the respective ID. When the data deletion process is completed, we will store success message into SESSION and redirect to index.php.

If you have any query about this tutorial and scripts, feel free to comment here.

Leave A Reply

Your email address will not be published.