How to Make Delete Multiple Rows Using PHP
Submitted by whello199 on Thursday, February 25, 2016 - 16:01.
Language
Creating a website which and add an ability to delete multiple rows at a time?
This tutorial is a perfect outlet for you to learn and implement the function. we will look on how to delete multiple values from a database using PHP. In order to do so we will be using MySQL along with PHP. You can find more about object oriented approach to CRUD Operations.
Overview
The tutorial requires to create following PHP files. You need to copy the codes in respective files and save all the files in same folder with .php extension. The functions of each of the files will be discussed in their respective part below.- Action.php
- Dbconfig.php
- Index.php
- Style.css
Database Schema
- CREATE TABLE `users` (
- `id` INT(11) NOT NULL AUTO_INCREMENT,
- `first_name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
- `last_name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
- `email` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
- `phone` VARCHAR(15) COLLATE utf8_unicode_ci 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;
- INSERT INTO `users` (`id`, `first_name`, `last_name`, `email`, `phone`, `created`, `modified`, `status`) VALUES
- (NULL, 'Nitya', 'Maity', '[email protected]', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
- (NULL, 'Codex', 'World', '[email protected]', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
- (NULL, 'Raj', 'Ans', '[email protected]', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
- (NULL, 'John', 'Thomas', '[email protected]', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
- (NULL, 'Kate', 'Bell', '[email protected]', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1);
Action.php
In this file, we have the code which takes the array of ID’s that is needed to be deleted. Inside a loop, it will iterate over the values and issue SQL queries to delete the entries.- <?php
- include_once('dbConfig.php');
- $idArr = $_POST['checked_id'];
- foreach($idArr as $id){
- }
- $_SESSION['success_msg'] = 'Delete Successful';
- }
- ?>
Dbconfig.php
This file contains the database configuration codes needed to establish the connection with database.- <?php
- $dbHost = 'localhost';
- $dbUser = 'root';
- $dbPass = '';
- $dbName = 'muldeldb';
- if(!$conn){
- }
- ?>
Index.php
The codes in this file, renders the table which shows all the user data. Here, every row will contain a check box. When the delete button is pressed all the selected data will be deleted in the same time. To check all the selected check boxes a JQuery function has been used which we already discussed in the previous tutorial.- <!DOCTYPE html>
- <html>
- <head>
- <title>Tutorial-27</title>
- <link rel="stylesheet" href="style.css"/>
- <script type="text/javascript" src="jquery.min.js"></script>
- <script type="text/javascript">
- function delete_confirm(){
- var result = confirm("Are you sure to delete users?");
- if(result){
- return true;
- }else{
- return false;
- }
- }
- $(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);
- }
- });
- });
- </script>
- </head>
- <body>
- ?>
- <div class="alert alert-success"><?php echo $_SESSION['success_msg'];
- ?>
- </div>
- <?php
- include_once('dbConfig.php');
- ?>
- <form name="bulk_action_form" action="action.php" method="post" onSubmit="return delete_confirm();"/>
- <table class="bordered">
- <thead>
- <tr>
- <th><input type="checkbox" name="select_all" id="select_all" value=""/></th>
- <th>First Name</th>
- <th>Last Name</th>
- <th>Email</th>
- <th>Phone</th>
- </tr>
- </thead>
- <?php
- ?>
- <tr>
- <td align="center"><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>
- </body>
- </html>
Style.css
The CSS codes in the file will add some design to the form or display.- body {
- width: 600px;
- margin: 40px auto;
- font-family: 'trebuchet MS', 'Lucida sans', Arial;
- font-size: 14px;
- color: #444;
- }
- h1{
- font-weight: normal;
- color: #024457;
- }
- h1 span{color: #167F92;}
- table {
- *border-collapse: collapse; /* IE7 and lower */
- border-spacing: 0;
- width: 100%;
- }
- .bordered {
- border: solid #ccc 1px;
- -webkit-box-shadow: 0 1px 1px #ccc;
- -moz-box-shadow: 0 1px 1px #ccc;
- box-shadow: 0 1px 1px #ccc;
- }
- .bordered td, .bordered th {
- border-left: 1px solid #ccc;
- border-top: 1px solid #ccc;
- padding: 10px;
- text-align: left;
- }
- .bordered th {
- background-color: #dce9f9;
- background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
- background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
- background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
- background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
- background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
- background-image: linear-gradient(top, #ebf3fc, #dce9f9);
- -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
- -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
- box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
- border-top: none;
- text-shadow: 0 1px 0 rgba(255,255,255,.5);
- }
- .bordered td:first-child, .bordered th:first-child {
- border-left: none;
- }
- .btn {
- display: inline-block;
- padding: 6px 12px;
- margin-bottom: 0;
- margin-top: 10px;
- font-size: 14px;
- font-weight: 400;
- line-height: 1.42857143;
- text-align: center;
- white-space: nowrap;
- vertical-align: middle;
- -ms-touch-action: manipulation;
- touch-action: manipulation;
- cursor: pointer;
- -webkit-user-select: none;
- -moz-user-select: none;
- -ms-user-select: none;
- user-select: none;
- background-image: none;
- border: 1px solid transparent;
- border-radius: 4px;
- }
- .btn-danger {
- color: #fff;
- background-color: #d9534f;
- border-color: #d43f3a;
- }
- .alert {
- padding: 15px;
- margin-bottom: 20px;
- border: 1px solid transparent;
- border-radius: 4px;
- }
- .alert-success {
- color: #3c763d;
- background-color: #dff0d8;
- border-color: #d6e9c6;
- }
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 560 views