PHP后台批量删除数据

html

<form action="" method="post">
<div><input type="submit" value="批量删除" /></div>
<table border="1" cellpadding="0" cellspacing="0"  style="width:100%;border-color:#ccc;text-align:center;">
    <tr height='30'>
        <td width="2%"><input type="checkbox" value="''" name="qx" onclick="checkall(this)" /></td>
        <td width="2%">ID</td>
        <td width="5%">账户</td>
    </tr>

<?php while($row1=mysql_fetch_assoc($res1)){ ?>
    <tr height='30'>
        <td><input type='checkbox' name='ids[]' value=<?php echo $row1['username']; ?> class='ck'/></td>
        <td><?php echo $row1['id']; ?> </td>
        <td><?php echo $row1['username']; ?></td>
    </tr>
<?php } ?>
</form>

利用js点击事件就可以轻松实现全选

<script>  
function checkall(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>

php

<?php 
header('Content-Type: text/html; charset=utf-8'); 
    ob_start();
    include('../config/db.php');

    if(!empty($_POST)){
        $arr = $_POST["ids"];
        $str = implode("','",$arr);//拼接字符,

        $sql = "delete from consumer WHERE username in ('{$str}')";
        $res = mysql_query($sql);

        if($res){
            echo "<script>alert('删除成功')</script>";
            header('refresh:0;url=./alldel.php');
            die;
        }else{
            echo "<script>alert('删除失败')</script>";
            header('refresh:0;url=./alldel.php');
            die;            
        }
    }

?>

 

转载于:https://www.cnblogs.com/php-qiuwei/p/9083911.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值