批量删除

<body>
<form action="./pldelete.php" method="post"> //提交到其他页面处理
<table class="table table-striped">
  <caption>人员表</caption>
  <thead>
    <tr>
      <th><input type="checkbox" id="ckall"/>代号</th>
      <th>姓名</th>
      <th>性别</th>
      <th>民族</th>
      <th>生日</th>
      <th>操作</th>
    </tr>
  </thead>
  <tbody>
  
  <?php 
  $db= new MySQLi("localhost","root","123","text1"); 
  $sql = "select info.code,info.name,info.sex, nation.name,birthday from info,nation where info.nation=nation.code";
  $result = $db->query($sql);
  if($result){
      $arr = $result->fetch_all();
      foreach($arr as $v){
          $sex = $v[2]?"男":"女";
          echo "<tr>
      <td><input type='checkbox' class='ck' name='ck[]' value='{$v[0]}'/>{$v[0]}</td>
      <td>{$v[1]}</td>
      <td>{$sex}</td>
      <td>{$v[3]}</td>
      <td>{$v[4]}</td>
      <td><a href='./delete.php?code={$v[0]}' onclick="return confirm('确认删除么?')"><button type='button' class='btn btn-default btn-sm'>删除</button></a>
      <a href='./xiugai.php?code={$v[0]}' ><button type='button' class='btn btn-default btn-sm'>修改</button></a>
      </td>
    </tr>";
          }
      }
  
  ?>
    </tbody>
    </table>
    <div><input type="submit" value="批量删除" /><a href="./tianjia.php">添加数据</a></div>
   </form>
   <script type="text/javascript">
   var ckall = document.getElementById("ckall");
   ckall.onclick = function(){
       var xz = ckall.checked;
       var ck = document.getElementsByClassName("ck");
       for(var i=0;i<ck.length;i++){
           ck[i].checked= xz;
           }
       }
   </script>
</body>

批量删除操作:

<?php
$arr = $_POST["ck"];//获取ck  $_POST可以获取name? 疑问?为什么不是ck[]?

$str = implode("','",$arr);//拼接字符串 $sql = "delete from info where code in ('{$str}')"; $db = new MySQLi("localhost","root","123","text1"); $result = $db->query($sql); if($result){ header("location:main.php"); }else{ echo "删除失败!"; }
原文地址:https://www.cnblogs.com/niushuangmeng/p/8295423.html