How to delete multiple record using php


Hello , friends in this article we will show you . how to delete multiple record from database . Mostly we have to need delete single record but according to project size some times we have to need delete multiple record from database .

So in this this tutorials we learn how to delete multiple record using php and mysql database . By the way we can use the for loop or foor each loop for delete multiple record . but here we use something different . We are using In clause for delete multiple record . lets start

index.php

First you have to create index.php file. inside the file you will create a form with POST method . Inside the form we will create a checkbox name = "select_all" and id = "select_all"

  1. <form action="" method="post">
  2. <div class="row">
  3. <div class="col-sm-8">
  4. <button class="btn btn-danger" type="submit" name="delete_record" id="delete_record" style="margin-bottom:10px;"><i class="glyphicon glyphicon-trash"></i>&nbsp;Delete All</button>
  5. <?php
  6. if(isset($_SESSION['msg'])){
  7. echo $_SESSION['msg'];
  8. }
  9. ?>
  10. <table class="table table-striped table-bordered">
  11. <thead>
  12. <tr class="danger">
  13. <th><input type="checkbox" name="select_all" id="select_all"></th>
  14. <th>SN</th>
  15. <th>Title </th>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. <?php
  20. $sql = "SELECT * FROM multiple_record_countries ORDER by id DESC LIMIT {$limit} , {$perpage} ";
  21. $result = $obj->execute_query($sql);
  22. if($result != FALSE){
  23. $sn = (($_GET['page'] - 1) * 20 ) + 1;
  24. foreach($result as $row){
  25. ?>
  26. <tr>
  27. <td><input type="checkbox" name="id[]" class="post_checkbox" value="<?php echo $row['id']; ?>"></td>
  28. <td><?php echo $sn; ?></td>
  29. <td><?php echo $row['name']; ?></td>
  30. </tr>
  31. <?php
  32. $sn++;
  33. }
  34. }
  35. else{
  36. ?>
  37. <tr>
  38. <td colspan="6">
  39. <P class="alert alert-info">No Record Found</P>
  40. </td>
  41. </tr>
  42. <?php
  43. }
  44. ?>
  45. </tbody>
  46. </table>
  47. </div>
  48. </div>
  49. </form>

Use this jquery code for checked and unchecked checkbox . If you want to delete a specific record you can checked perticular checkbox . If you want to delete multiple record you can checked SELECT ALL checkbox .

  1. $(document).ready(function(e) {
  2. $(document).on('click','#select_all',function(e){
  3. if( $(this).is(":checked") ){
  4. $(".post_checkbox").prop('checked',true);
  5. }
  6. else{
  7. $(".post_checkbox").prop('checked',false);
  8. }
  9. });
  10. });

When you click the submit button then you have to follow this procedure . If you checked Select All button and submit the form then delete all single page record .

  1. $table_name = "multiple_record_countries";
  2. if(isset($_POST['delete_record'])){
  3. if(count($_POST['id']) > 0){
  4. $id_arr = implode(',' , $_POST['id']);
  5. $where_con = "`id` IN (".$id_arr.")";
  6. $delete_response = $obj->delete_multiple_record($table_name , $where_con);
  7. if($delete_response){
  8. $_SESSION['msg'] = $obj->has_message('Record delete successful !!!');
  9. header('Location: ' . $_SERVER['HTTP_REFERER']);
  10. }
  11. }
  12. else{
  13. header('Location: ' . $_SERVER['HTTP_REFERER']);
  14. }
  15. }
  1. <?php
  2. public function delete_multiple_record($table_name , $con){
  3. $sql = "DELETE FROM ".$table_name." WHERE ".$con;
  4. $q = $this->db->prepare( $sql );
  5. $stmt = $q->execute();
  6. if( $stmt ) { return TRUE; }
  7. else{ return FALSE; }
  8. }
  9. ?>