• 批量删除的PHP


    第一个页面shanchu.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    
    <h1>批量删除</h1>
    
    <form action="pldel.php" method="post">
    
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    
        <tr>
            <td>
                <input type="checkbox" name="qx" onclick="checkall(this)" />
                代号
            </td>
            <td>名称</td>
        </tr>
        
        <?php
        $db = new MySQLi("localhost","root","root","dbname");
        $sql = "select * from nation";
        $result = $db->query($sql);
        while($arr = $result->fetch_row())
        {
            echo "<tr>
            <td>
                <input type='checkbox' value='{$arr[0]}' name='item[]' class='ck' />
                {$arr[0]}
            </td>
            <td>{$arr[1]}</td>
        </tr>";
        }
        
        ?>
        
    </table>
    
    <input type="submit" value="批量删除" />
    
    </form>
    <script type="text/javascript">
    
    function checkall(qx)
    {
        var ck = document.getElementsByClassName("ck");
        
        if(qx.checked)
        {
            for(var 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>
    </body>
    </html>

    1、item的那里加入[]代表数组

    2、注意js的写法

    第二个页面pldel.php

    <?php
    
    $arr = $_POST["item"];
    
    $db = new MySQLi("localhost","root","root","dbname");
    
    /*foreach($arr as $v)                //不建议使用这种方式,对数据库操作过多
    {
        $sql = "delete from nation where code='{$v}'";
        $db->query($sql);
    }*/
    
    $str = implode("','",$arr);//注意需要替换的内容
    
    $sql = "delete from nation where code in('{$str}')";//注意拼写字符串
    
    if($db->query($sql))
    {
        header("location:shanchu.php");
    }
  • 相关阅读:
    jmeter教程索引
    JMeter 中_time 函数的使用(时间戳、当前时间)
    通用分页存储过程
    如何才算掌握Java(J2SE篇) 转载
    Java 外企面试若干题
    Java 有用的网址 转载
    JDBC链接基本步骤
    java基础学习 视频学习 数据类型以及运算符
    Java基础 构造对象初始化变量的顺序浅见
    全面解析《嵌入式程序员应该知道的16个问题》 转载
  • 原文地址:https://www.cnblogs.com/gaobint/p/6434028.html
Copyright © 2020-2023  润新知