Home > Backend Development > PHP Problem > What is the idea of ​​deleting codes in batches in php

What is the idea of ​​deleting codes in batches in php

藏色散人
Release: 2023-03-17 15:14:01
Original
970 people have browsed it

php batch deletion code ideas: 1. Set the class file for database query; 2. Add selection check boxes to the table; 3. Use js to control the selection and deselection of all check boxes; 4. Add a form form and submit button outside the form, and use js to control the detailed prompt information when clicking delete; 5. Create a new php file for deletion processing.

What is the idea of ​​deleting codes in batches in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

php What is the idea of ​​deleting codes in batches?

php batch deletion can delete multiple or all data at the same time

What is the idea of ​​deleting codes in batches in php

Create a new php file to display the content in the database:

<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td><input type="checkbox" id="qx" onclick="xuanzhong()" />全选</td>
        <td>代号</td>
        <td>名称</td>
    </tr>
     
    <?php
    include("DBDA.class.php");
    $db = new DBDA();
     
    $sql = "select areacode,areaname from nation";
    $attr = $db->Query($sql);
     
    foreach($attr as $v)
    {
        echo "<tr>
        <td><input type=&#39;checkbox&#39; name=&#39;ck[]&#39; class=&#39;ck&#39; value=&#39;{$v[0]}&#39; /></td>
        <td>{$v[0]}</td>
        <td>{$v[1]}</td>
    </tr>";
    }
     
    ?>
     
</table>
Copy after login

DBDA.class.php file is the class file for database query:

<?php
class DBDA
{
    public $host="localhost";
    public $uid = "root";
    public $pwd = "";
    public $dbname = "12345";
     
    //成员方法
    public function Query($sql,$type=1)
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
        $r = $db->query($sql);
         
        if($type==1)
        {
            return $r->fetch_all();
        }
        else
        {
            return $r;
        }
    }
}
Copy after login

What is the idea of ​​deleting codes in batches in php

Add a selection check box to the table:

<td><input type="checkbox" id="qx" onclick="xuanzhong()" />全选</td>
Copy after login
<td><input type=&#39;checkbox&#39; name=&#39;ck[]&#39; class=&#39;ck&#39; value=&#39;{$v[0]}&#39; /></td>
Copy after login

Display:

What is the idea of ​​deleting codes in batches in php

Use js to control the selection and deselection of all check boxes:

<script type="text/javascript">
    function xuanzhong()
    {
        //取全选按钮的选中状态
        var zt = document.getElementById("qx").checked;
         
        //让下面所有的checkbox选中状态改变
        var ck = document.getElementsByClassName("ck");
         
        for(var i=0;i<ck.length;i++)
        {
            if(zt)
            {
                ck[i].setAttribute("checked","checked");
            }
            else
            {
                ck[i].removeAttribute("checked");
            }
        }
    }
</script>
Copy after login

Add form form and submit button outside the form, and Use js control to display detailed prompt information when you click delete. Complete php code:





无标题文档


<td><input type="checkbox" id="qx" onclick="xuanzhong()" />全选</td> Query($sql); foreach($attr as $v) { echo " <td><input type=&#39;checkbox&#39; name=&#39;ck[]&#39; class=&#39;ck&#39; value=&#39;{$v[0]}&#39; /></td> "; } ?>
代号 名称
{$v[0]} {$v[1]}
Copy after login

What is the idea of ​​deleting codes in batches in php

Finally create a new php file for deletion processing;

<?php
$ck = $_POST["ck"];
include("DBDA.class.php");
$db = new DBDA();
//第一种方式
/*foreach($ck as $v)
{
    $sql = "delete from nation where code=&#39;{$v}&#39;";
    $db->Query($sql,0);
}*/
//第二种方式
//in (&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;)
$str = implode("&#39;,&#39;",$ck);
$str = "(&#39;{$str}&#39;)";
$sql = "delete from nation where code in {$str}";
$db->Query($sql,0);
header("location:main.php");
Copy after login

What is the idea of ​​deleting codes in batches in php

Click OK:

What is the idea of ​​deleting codes in batches in php

Batch deletion successful!

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the idea of ​​deleting codes in batches 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