donglu8779 2014-07-22 04:24
浏览 15
已采纳

如何使用php中的复选框删除记录?

Need to delete records and Here is my code:

<input type="submit" name="delete" value="delete"/>  
<?php
    if(isset($_POST['delete'])) 
    {  
        $cnt=array(); 
        $cnt=count($_POST['chkbox']);  
        for($i=0;$i<$cnt;$i++)   
        {
            $del_id=$_POST['chkbox'][$i];
            $query="DELETE FROM menu_detail WHERE vt_id=".$del_id;
            mysql_query($query);   
        } 
    } 
?>  
// I draw checkboxes using this where as I m fetching data from table...
<?php  
   $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; ?>">
<td>
    <input type="checkbox" name="chkbox" value="checkbox[<?php echo $row['vt_id']?>]" />
</td>

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

enter image description here

  • 写回答

1条回答 默认 最新

  • drhzc64482 2014-07-22 04:42
    关注

    Take a look

           $tableRow = array("id","name","code"): // assume It must from Database
    
             <form action="your_action.php"  method="post" id="bulkDel" >
    
           <table>
                   <thead>
                          <th><input type="checkbox" id="checkAll" /></th>
                          <th>....</th> <!--and so on-->
                  </thead>
               <?php
                     foreach($tableRow as $row){
                           ?>
                         <tr>
                               <td> <input tyep="checkbox"  name="check[]" /></td>
                               <td>.....</td> <!-- and so on --> 
                         </tr>
    
                      <?php
    
                     }
                ?>
    
          </table>
          <a href="javascript:void(0);"id="bulkDeleteBut" >Delete selected</a>
       </form>
    
      // jquery check all checkboxes
    
      $(document).on('click','#checkAll',function(){
                        $('input[name="check[]"]').prop('checked',$(this).is(":checked"));
      });
    
    
     $(document).on('click','#bulkDeleteBut',function(){
                 var len = $('input[name="check[]"]')..serializeArray().length;
                 if(len>0){
                         $('#bulkDel').submit();
                 }else{
                  alert("Check atleast one record");
                } 
     });
    
    
     // server side php
       if(isset($_POST['check'])){
           $ar = $_POST['check'];
            foreach($ar as $primary_key){
                 // perform mysql delete query and return back to table page
            }
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法