php 批量删除数据

php 批量删除数据 :比如我们在看邮箱文件的时候,积攒了一段时间以后,看到有些文件没有用了 这时候我们就会想到把这些

没用的文件删除,这时候就用到了批量删除数据的功能,这里我是用了数据库原有的一个表格简单的做了一下示例:

数据库名称:test  表格名称是  teacher

// 首先需要建一个表格把所需要的数据库表格中内容展现出来
<body>
<form  action="plshanchu" method="post">
<table>
     <tr>
             <td><input type="checkbox" name="qx" onclic=checkall(this)/>工号</td>//我们这里的复选框是点击后全选 所以设置了一个JS的点击事件
             <td>姓名</td>
             <td>性别</td>
             <td>职称</td>

     </tr>
  <?php

 $db=new mysqli("localhost","root","root","test");
 $sql="select * from student";
 $result=$db->query($sql);
while($arr=$result->fetch_row())
{
echo"<tr>
             <td><input type='checkbox' name='item[]' value='{$arr[0]}' class='dx'/>{$arr[0]}</td> //这里的name值是一个数组的形式
             <td>{$arr[1]}</td>
             <td>{$arr[2]}</td>
             <td>{$arr[4]}</td>

     </tr>";
}


  ?>
<table>
<input type="submit" onclick="return confirm('您确定要删除吗')" value="批量删除"/> </form> </body> <script> function checkall(qx) //这里是全选的点击事件 { var dx=document.getelementsbyclassname("dx") if(qx.checked) //qx.checkde 是指点击工号前面的复选框的时候全选 { for( var i=0; i<dx.length;i++) { dx[i].setAttribute("checked","checkde") //让其全选 } } else { dx[i].removeAttribute("checkde") // 把全选移除 } } </script>

下面是我们来做删除的操作了 

<?php

//首先要根据前面我们提到的复选框是一个数组,来渠道这个数组
$att=$_post["item"];

$db=new mysqli("localhost","root","root","test");

$str=implode(" ','  ",$att); //这个地方用的','来拼接字符串的  //n003','n004','n005','n006 拼接之后是这样子的 

$sql=delete from student where tno in ('{$str}');

if($db->query($sql))
{
header("location:piliang.php"); //回到上面开始的页面
}
else
{
echo"批量删除失败";
}

?>
原文地址:https://www.cnblogs.com/xiaodouding/p/6434360.html