Google Ads

How to Make Delete Multiple Rows Using PHP Script


How to Make Delete Multiple Rows Using PHP Script

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

 

Code:
 
  1. CREATE TABLE `users` (
  2. `id` INT(11) NOT NULL AUTO_INCREMENT,
  3. `first_name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `last_name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
  5. `email` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
  6. `phone` VARCHAR(15) COLLATE utf8_unicode_ci NOT NULL,
  7. `created` datetime NOT NULL,
  8. `modified` datetime NOT NULL,
  9. `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active, 0=Deactive',
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  12. INSERT INTO `users` (`id`, `first_name`, `last_name`, `email`, `phone`, `created`, `modified`, `status`) VALUES
  13. (NULL, 'Nitya', 'Maity', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
  14. (NULL, 'Codex', 'World', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
  15. (NULL, 'Raj', 'Ans', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
  16. (NULL, 'John', 'Thomas', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
  17. (NULL, 'Kate', 'Bell', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '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.

 

Code:
 
  1. <?php
  2. session_start();
  3. include_once('dbConfig.php');
  4. if(isset($_POST['bulk_delete_submit'])){
  5. $idArr = $_POST['checked_id'];
  6. foreach($idArr as $id){
  7. mysqli_query($conn,"DELETE FROM users WHERE id=".$id);
  8. }
  9. $_SESSION['success_msg'] = 'Delete Successful';
  10. header("Location:index.php");
  11. }
  12. ?>

 

Dbconfig.php

This file contains the database configuration codes needed to establish the connection with database.

 

Code:
 
  1. <?php
  2. $dbHost = 'localhost';
  3. $dbUser = 'root';
  4. $dbPass = '';
  5. $dbName = 'muldeldb';
  6. $conn = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName);
  7. if(!$conn){
  8. die("Database connection failed: " . mysqli_connect_error());
  9. }
  10. ?>

 

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.

 

Code:
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Tutorial-27</title>
  5. <link rel="stylesheet" href="style.css"/>
  6. <script type="text/javascript" src="jquery.min.js"></script>
  7. <script type="text/javascript">
  8. function delete_confirm(){
  9. var result = confirm("Are you sure to delete users?");
  10. if(result){
  11. return true;
  12. }else{
  13. return false;
  14. }
  15. }
  16. $(document).ready(function(){
  17. $('#select_all').on('click',function(){
  18. if(this.checked){
  19. $('.checkbox').each(function(){
  20. this.checked = true;
  21. });
  22. }else{
  23. $('.checkbox').each(function(){
  24. this.checked = false;
  25. });
  26. }
  27. });
  28. $('.checkbox').on('click',function(){
  29. if($('.checkbox:checked').length == $('.checkbox').length){
  30. $('#select_all').prop('checked',true);
  31. }else{
  32. $('#select_all').prop('checked',false);
  33. }
  34. });
  35. });
  36. </script>
  37. </head>
  38. <body>
  39. <?php session_start();
  40. if(!empty($_SESSION['success_msg'])) {
  41. ?>
  42. <div class="alert alert-success"><?php echo $_SESSION['success_msg'];
  43. ?>
  44. </div>
  45. <?php unset($_SESSION['success_msg']); } ?>
  46. <?php
  47. include_once('dbConfig.php');
  48. $query = mysqli_query($conn,"SELECT * FROM users");
  49. ?>
  50. <form name="bulk_action_form" action="action.php" method="post" onSubmit="return delete_confirm();"/>
  51. <table class="bordered">
  52. <thead>
  53. <tr>
  54. <th><input type="checkbox" name="select_all" id="select_all" value=""/></th>
  55. <th>First Name</th>
  56. <th>Last Name</th>
  57. <th>Email</th>
  58. <th>Phone</th>
  59. </tr>
  60. </thead>
  61. <?php
  62. if(mysqli_num_rows($query) > 0){
  63. while($row = mysqli_fetch_assoc($query)){
  64. ?>
  65. <tr>
  66. <td align="center"><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $row['id']; ?>"/></td>
  67. <td><?php echo $row['first_name']; ?></td>
  68. <td><?php echo $row['last_name']; ?></td>
  69. <td><?php echo $row['email']; ?></td>
  70. <td><?php echo $row['phone']; ?></td>
  71. </tr>
  72. <?php } }else{ ?>
  73. <tr><td colspan="5">No records found.</td></tr>
  74. <?php } ?>
  75. </table>
  76. <input type="submit" class="btn btn-danger" name="bulk_delete_submit" value="Delete"/>
  77. </form>
  78. </body>
  79. </html>

 

Style.css

The CSS codes in the file will add some design to the form or display.

 

Code:
 
  1. body {
  2. width: 600px;
  3. margin: 40px auto;
  4. font-family: 'trebuchet MS', 'Lucida sans', Arial;
  5. font-size: 14px;
  6. color: #444;
  7. }
  8. h1{
  9. font-weight: normal;
  10. color: #024457;
  11. }
  12. h1 span{color: #167F92;}
  13. table {
  14. *border-collapse: collapse; /* IE7 and lower */
  15. border-spacing: 0;
  16. width: 100%;
  17. }
  18. .bordered {
  19. border: solid #ccc 1px;
  20. -webkit-box-shadow: 0 1px 1px #ccc;
  21. -moz-box-shadow: 0 1px 1px #ccc;
  22. box-shadow: 0 1px 1px #ccc;
  23. }
  24. .bordered td, .bordered th {
  25. border-left: 1px solid #ccc;
  26. border-top: 1px solid #ccc;
  27. padding: 10px;
  28. text-align: left;
  29. }
  30. .bordered th {
  31. background-color: #dce9f9;
  32. background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
  33. background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
  34. background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
  35. background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
  36. background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
  37. background-image: linear-gradient(top, #ebf3fc, #dce9f9);
  38. -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  39. -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
  40. box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  41. border-top: none;
  42. text-shadow: 0 1px 0 rgba(255,255,255,.5);
  43. }
  44. .bordered td:first-child, .bordered th:first-child {
  45. border-left: none;
  46. }
  47. .btn {
  48. display: inline-block;
  49. padding: 6px 12px;
  50. margin-bottom: 0;
  51. margin-top: 10px;
  52. font-size: 14px;
  53. font-weight: 400;
  54. line-height: 1.42857143;
  55. text-align: center;
  56. white-space: nowrap;
  57. vertical-align: middle;
  58. -ms-touch-action: manipulation;
  59. touch-action: manipulation;
  60. cursor: pointer;
  61. -webkit-user-select: none;
  62. -moz-user-select: none;
  63. -ms-user-select: none;
  64. user-select: none;
  65. background-image: none;
  66. border: 1px solid transparent;
  67. border-radius: 4px;
  68. }
  69. .btn-danger {
  70. color: #fff;
  71. background-color: #d9534f;
  72. border-color: #d43f3a;
  73. }
  74. .alert {
  75. padding: 15px;
  76. margin-bottom: 20px;
  77. border: 1px solid transparent;
  78. border-radius: 4px;
  79. }
  80. .alert-success {
  81. color: #3c763d;
  82. background-color: #dff0d8;
  83. border-color: #d6e9c6;
  84. }

 

 


Related Post

Point of Sales v3.1 – Multi Outlets POS PHP ... Point of Sales v3.1 - Multi Outlets POS PHP Script Easy to use Point of Sales System and able to handle multiple bills and search opened bill optio...
Wapking Auto Index PHP Script Download Wapking Auto Index PHP Script Download   First of download the script Upload and extract on any hosting server Create a mysql with username and...
Simple Email Extractor v2.4 PHP Script Download Simple Email Extractor v2.4Simple Email Extractor v2.4A simple software to extract email addresses from websites and social networks like Facebook, Tw...
Chameleon v2.2.1 – Social Network Dating Scr... Chameleon v2.2.1 - Social Network Dating Script - NULLEDChameleon v2.2.1 - Social Network Dating Script - NULLEDProfessional Social & Dating Site Soft...
Bitcoin Transaction Accelerator PHP Script Bitcoin Transaction Accelerator PHP Script With the growth of bitcoin hundreds of thousands of transactions are being sent per day. Many transactio...

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

"