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
            }
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题