Home > Article > Backend Development > php batch delete operation
Deleting multiple records in batches is very troublesome if there is no batch deletion function for relatively large amounts of information.
1. Take a table from the database and write a check box to select
You can add a select all check box
Connect to the database or something Don’t even write it
Code:
<form action="piliangshanchu.php" method="post" > <table border="1" cellspacing="0" cellpadding="0"> <tr> <td width="200"> <input type="checkbox" value="''" name="dx" onclick="checkall(this)" /> 编号</td> <td width="200">姓名</td> <td width="200">电话</td> <td width="200" >分组</td> <td width="200" >操作</td> </tr> <tr> <td> <input type='checkbox' value='{$attr[0]}' name='item[]' class='ck' /> {$attr[0]}</td> <td>{$str}</td> <td>{$attr[2]}</td> <td>{$nation}</td> </tr> </table> <input type="submit" value="批量删除"/> </form>
plus a batch delete button
Picture above:
If I click to select all, I can easily select all by using js click event
Code:
<script> function xxx(qx) { //全选多选的选中状态 var ck = document.getElementsByClassName("ck"); //让下面所有的多选选中状态改变 if(qx.checked) { for(i = 0;i < ck.length ; i++) { ck[i].setAttribute("checked","checked"); //状态改变为选中 } } else { for(var i = 0;i < ck.length;i++) { ck[i].removeAttribute("checked"); //移除选中 } } } </script>
2. Deletion processing page
Code:
<?php $arr = $_POST["item"]; $db = new mysqli("localhost","root","12345678","heiheihei"); //foreach($arr as $v) //{ // $sql = "delete from contacts WHERE id='{$v}'"; // $db->query($sql); //} $str = implode("','",$arr);//拼接字符, $sql = "delete from contacts WHERE id in('{$str}')"; //2','8','4 if($db->query($sql))//判断是否查询成功, { header("location:shouye.php"); //成功就跳转 } ?>
The data transmission using foreach is too slow, and the deletion traversal is too many, so it is judged directly ;
For more articles related to PHP batch deletion operations, please pay attention to the PHP Chinese website!