Delete multiple records from MySQL in PHP with checkbox

Delete multiple records from MySQL in PHP with checkbox

As we know, Its very time consuming to delete records one by one. Everyone wants to delete bulk record on single event only.

So here i will tell you how to delete bulk records from MySQL table in PHP with checkbox, you just need to select checkbox which one you want to delete and hit submit button to delete.

Its strongly recommended for large scale of data where its not possible to delete one by one.

Here in this tutorial, i will simple create a product table for demo with dummy records to test this code.

And with every row there will be one checkbox that hold the id of records in array.

There will be one checkbox with header too for select all records on single click.

When you are going to delete records, you will be asked for confirmation "Do you really want to delete records" when you click yes then selected records will be deleted from database table.

products table

In first step, create a products table in database and insert some dummy records after creating a table.

  1. CREATE TABLE `products` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `details` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  5. `created_at` datetime NOT NULL,
  6. `updated_at` datetime NOT NULL,
  7. `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active, 0=Deactive',
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Now i have a products table with id, name, details, created_at, updated_At, status fields.

Database Configuration : db_config.php

Now i need to setup my database configuration in db_config.php file.

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

In this step, create a file where we list all records from database into html table but first include db_config.php for database access.

To work with js script, include jQuery library.

deleteConfirm() method is javascript function that is called on form submit to make sure you are really want to delete the product.

  1. <!DOCTYPE html>
  2. <html lang="en-US">
  3. <head>
  4.     <title>Delete multiple records from MySQL in PHP with checkbox - ExpertPHP</title>
  5. <link rel="stylesheet" href="">
  6.     <script src=""></script>
  7. </head>
  8. <body>
  9. <?php
  10.     session_start();
  11. include_once('db_config.php');
  12. $query = mysqli_query($conn,"SELECT * FROM products");
  13. ?>
  15. <form name="actionForm" action="action.php" method="post" onsubmit="return deleteConfirm();"/>
  16. <table class="table-striped">
  17. <thead>
  18. <tr>
  19. <th><input type="checkbox" name="check_all" id="check_all" value=""/></th>
  20. <th>Name</th>
  21. <th>Details</th>
  22. </tr>
  23. </thead>
  24. <?php
  25. if(mysqli_num_rows($query) > 0){
  26. while($row = mysqli_fetch_assoc($query)){
  27.     extract($row);
  28. ?>
  29. <tr>
  30. <td align="center"><input type="checkbox" name="selected_id[]" class="checkbox" value="<?php echo $id; ?>"/></td>
  31. <td><?php echo $name; ?></td>
  32. <td><?php echo $details; ?></td>
  33. </tr>
  34. <?php } }else{ ?>
  35. <tr><td colspan="3">No records found.</td></tr>
  36. <?php } ?>
  37. </table>
  38. <input type="submit" class="btn btn-primary" name="btn_delete" value="Delete"/>
  39. </form>
  40. <script type="text/javascript">
  41. function deleteConfirm(){
  42. var result = confirm("Do you really want to delete records?");
  43. if(result){
  44. return true;
  45. }else{
  46. return false;
  47. }
  48. }
  49. $(document).ready(function(){
  50. $('#check_all').on('click',function(){
  51. if(this.checked){
  52. $('.checkbox').each(function(){
  53. this.checked = true;
  54. });
  55. }else{
  56. $('.checkbox').each(function(){
  57. this.checked = false;
  58. });
  59. }
  60. });
  61. $('.checkbox').on('click',function(){
  62. if($('.checkbox:checked').length == $('.checkbox').length){
  63. $('#check_all').prop('checked',true);
  64. }else{
  65. $('#check_all').prop('checked',false);
  66. }
  67. });
  68. });
  69. </script>
  70. </body>
  71. </html>

In this step create a php file where we handle form data. when we click submit button then form will be submitted to their action page.

In this PHP file we will get all selected id of records that will be deleted from product table.

Once records are deleted then we store the message in session according to response to notify the user.

  1. <?php
  2. session_start();
  3. include_once('db_config.php');
  4. if (count($_POST["selected_id"]) > 0 ) {
  5.      $all = implode(",", $_POST["selected_id"]);
  6.      $query="DELETE FROM products WHERE 1 AND id IN($all)";
  7.      if(mysqli_query($conn,$query)){
  8.          $_SESSION['success'] = 'Products have been deleted successfully.';
  9.      }
  10.     }else{
  11.         $_SESSION['error'] = 'Select checkbox to delete product.';
  12.     }
  13. header("Location:index.php");
  14. ?>

Phone: (+91) 8800417876
Noida, 201301
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
antalya escort akseki escort aksu escort alanya escort bayan antalya escort antalya grup escort antalya masöz antalya rus escort antalya sınırsız escort antalya travesti escort bdsm escort beldibi escort bayan belek escort bayan çallı escort bayan çamyuva escort bayan demre escort bayan elmalı escort bayan fethiye escort bayan finike escort bayan göynük escort bayan güllük escort bayan ışıklar escort bayan kaş escort bayan kemer escort bayan konyaaltı altınkum escort bayan konyaaltı arapsuyu escort bayan konyaaltı escort bayan konyaaltı liman escort korkuteli escort bayan kültür escort kundu escort bayan lara escort bayan manavgat escort bayan muratpaşa escort bayan örnekköy escort bayan rus escort bayan serik escort bayan side escort bayan suriyeli escort tekirova escort bayan üniversiteli escort alanya escort mahmutlar escort alanya escort mahmutlar escort alanya escort mahmutlar escort istanbul escort avrupa yakası escort şişli escort topkapı escort istanbul vip escort maslak escort merter escort aksaray escort arnavutköy escort ataköy escort bağcılar escort bahçelievler escort bakırköy escort beşitaş escort beylikdüzü escort beyoğlu escort halkalı escort istanbul sınırsız escort kağıthane escort mecidiyeköy escort nişantaşı escort escort istanbul ataşehir escort beykoz escort çekmeköy escort kadıköy escort maltepe escort pendik escort sancaktepe escort şile escort sultanbeyli escort tuzla escort ümraniye escort üsküdar escort
beylikdüzü escort istanbul escort şişli escort beylikdüzü escort istanbul escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort alanya escort manavgat escort bursa escort eskişehir escort sakarya escort samsun escort malatya escort trabzon escort adıyaman escort afyon escort ağrı escort aksaray escort amasya escort ardahan escort artvin escort aydın escort balikesir escort bartın escort batman escort bayburt escort bilecik escort bingöl escort bitlis escort bolu escort burdur escort çanakkale escort çankırı escort çorum escort düzce escort edirne escort elazığ escort erzincan escort erzurum escort giresun escort gümüşhane escort hatay escort ığdır escort ısparta escort maraş escort karabük escort karaman escort kars escort kastamonu escort kilis escort kırıkkale escort kırklareli escort kırşehir escort kütahya escort manisa escort mardin escort muğla escort muş escort nevşehir escort niğde escort ordu escort osmaniye escort rize escort urfa escort siirt escort sinop escort şırnak escort sivas escort ordu escort tokat escort tunceli escort usak escort van escort yalova escort yozgat escort zonguldak escort kıbrıs escort
adana escort ankara escort bursa escort diyarbakir escort eskisehir escort kayseri escort kocaeli escort konya escort malatya escort maras escort mersin escort ordu escort sakarya escort samsun escort hatay escort mardin escort tekirdag escort antalya escort aydin escort nigde escort nevsehir escort elazig escort manisa escort osmaniye escort denizli escort mugla escort
bedava bahis
ankara escort - ankara escort
ataşehir escort ataşehir anadolu yakası escort ankara escort antalya escort antalya escort

flynta 5 mg

flynta 5 mg fiyat

orcafil 5 mg




combo 100 mg

degra 100 mg




lifta 20

vigrande 100 mg

lifta 20 mg

lifta 5 mg

sildegra 100 mg


flynta 20 mg

viagra 100 mg fiyat

orijinal viagra

viagra 100 mg

eczane viagra

viagra sipariş

viagra satın al

viagra nedir

viagra yorum

viagra fiyat

viagra fiyatı 2021


cialis fiyat

cialis 20 mg

cialis 100 mg

cialis nedir

cialis 5 mg fiyatı

cialis hap

cialis 5 mg

cialis 20 mg eczane fiyatı

cialis 100 mg fiyat

cialis satış

izmir escort
ataşehir escort
pendik aktar