How to achieve batch deletion of check boxes in php

藏色散人
Release: 2023-03-08 11:12:01
Original
2944 people have browsed it

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.

How to achieve batch deletion of check boxes in php

#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.

1. Take a table from the database and write a check box to select

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="&#39;&#39;" name="dx" onclick="checkall(this)" />
            编号</td>
        <td width="200">姓名</td>
        <td width="200">电话</td>
        <td width="200" >分组</td>
        <td width="200" >操作</td>
    </tr>
Copy after login
<tr>
        <td>
        <input type=&#39;checkbox&#39; value=&#39;{$attr[0]}&#39; name=&#39;item[]&#39; class=&#39;ck&#39; />
        {$attr[0]}</td> 

        <td>{$str}</td>
        <td>{$attr[2]}</td>
        <td>{$nation}</td>
</tr>


</table>

    <input type="submit" value="批量删除"/>
    </form>
Copy after login

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>
Copy after login

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=&#39;{$v}&#39;";
//    $db->query($sql);
//}
$str = implode("&#39;,&#39;",$arr);//拼接字符,
$sql = "delete from contacts WHERE id in(&#39;{$str}&#39;)";
//2&#39;,&#39;8&#39;,&#39;4
if($db->query($sql))//判断是否查询成功,
{
    header("location:shouye.php");
    //成功就跳转
}



?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template