php method to implement batch deletion of check boxes: first connect to the database and obtain a table; then create a form and define a check box; then add a batch delete button; and finally create a PHP process for deletion page.
#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
php batch deletion, batch operation
Delete multiple records in batches. For relatively large amounts of information, it is very troublesome if there is no batch deletion function.
You can add a check box to select all Selection box
Nothing to connect to the database is written
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
Above picture:
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>
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 there are many deletions and traversals, so it is judged directly. [Recommended learning: "PHP Video Tutorial"]
The above is the detailed content of How to achieve batch deletion of check boxes in php. For more information, please follow other related articles on the PHP Chinese website!