php批量删除

这里不止用于批量删除,批量选择也是一样,主要就是用复选框进行批量选择,结合JS完成批量删除的效果。

最终结果图:

1、代码如下,分两个页面,下面是main1的代码

<body>
<h1>汽车信息</h1>
<form action="delete.php" method="post">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td><input type="checkbox" name="qx" onclick="quanxuan(this)" />代号</td><!--设置复选框并添加点击事件-->
        <td>名称</td>
    </tr>
<?php
require "DBDA.class.php";
$db=new DBDA();

$sql="select * from car";
$arr=$db->query($sql);

foreach($arr as $v)
{
    echo "<tr>
        <td><input type='checkbox' name='ck[]' class='ck' value='{$v[0]}'>{$v[0]}</td>
        <td>{$v[1]}</td>
    </tr>";    
}
?>
</table><br />

<input type="submit" value="批量删除" />
</form>
</body>
<script>
function quanxuan(qx)
{
    var ck=document.getElementsByClassName("ck");//提取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>
</html>

 2、delete页面代码:

<?php
$arr = $_POST["ck"];//提取数据,取到数组

require "DBDA.class.php";
$db = new DBDA();

$str = implode("','",$arr);//数组组成字符串

$sql = "delete from car where code in('{$str}')";
echo $sql;
if($db->query($sql,0))
{
    header("location:main1.php");
}

 

posted @ 2017-05-03 14:23  梦深深处  阅读(222)  评论(0编辑  收藏  举报