Majid Ansari Majid Ansari - 2 years ago 71
SQL Question

How can I delete records using checkbox in php?

Need to delete records and Here is my code:

<input type="submit" name="delete" value="delete"/>
$query="DELETE FROM menu_detail WHERE vt_id=".$del_id;
// I draw checkboxes using this where as I m fetching data from table...
$col = "row0"; $i = 0; $a=1;
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
if ($i == 0) { $col = "row0"; $i = 1; } else { $col = ""; $i = 0; }
<tr class="<?php echo $col; ?>">
<input type="checkbox" name="chkbox" value="checkbox[<?php echo $row['vt_id']?>]" />

Look at the image will give you an idea where I am stuck

enter image description here

Answer Source

Take a look

       $tableRow = array("id","name","code"): // assume It must from Database

         <form action="your_action.php"  method="post" id="bulkDel" >

                      <th><input type="checkbox" id="checkAll" /></th>
                      <th>....</th> <!--and so on-->
                 foreach($tableRow as $row){
                           <td> <input tyep="checkbox"  name="check[]" /></td>
                           <td>.....</td> <!-- and so on --> 



      <a href="javascript:void(0);"id="bulkDeleteBut" >Delete selected</a>

  // jquery check all checkboxes


             var len = $('input[name="check[]"]')..serializeArray().length;
              alert("Check atleast one record");

 // server side php
       $ar = $_POST['check'];
        foreach($ar as $primary_key){
             // perform mysql delete query and return back to table page
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download