php数据访问-批量删除(批量操作)

1.批量删除页面 piliangcaozuo.php

 1 <body>
 2 <form action="shanchu.php" method="post">
 3 <table width="100%" border="1" cellpadding="0" cellspacing="0">
 4     <tr>
 5         <td><input type="checkbox" name="qx" οnclick="quanxuan(this)"/>代号</td>
 6         <td>名称</td>      
 7     </tr>
 8     <?php
 9      require"DBDA.class1.php";
10      $db = new DBDA();
11      $sql = "select * from nation";
12      $arr = $db->query($sql);
13      foreach($arr as $v)
14     {
15         echo "<tr>
16                 <td><input type='checkbox' name='ck[]' class='ck' value='{$v[0]}'/>{$v[0]}</td>
17                 <td>{$v[1]}</td>      
18              </tr>";
19     }
20     ?>    
21 </table>
22 <input type="submit" value="批量删除" />
23 </form>
24 </body>
25 <script type="text/javascript">
26 function quanxuan(qx)
27 {
28     var ck=document.getElementsByClassName("ck");
29     if(qx.checked)
30     {
31         for(var i=0;i<ck.length;i++)
32         {
33             ck[i].setAttribute("checked","checked");
34         }
35     }
36     else
37     {
38         for(var i=0;i<ck.length;i++)
39         {
40             ck[i].removeAttribute("checked");
41         }
42     }
43 }
44 </script>
45 </html>

引用的封装类 DBDA.class1.php

 1 <?php
 2 class DBDA
 3 {
 4     public $host = "localhost";
 5     public $uid = "root";
 6     public $pwd = "123";
 7     public $dbname = "test_123";
 8     //执行SQL语句返回相应的结果
 9     //$sql 要执行的SQL语句
10     //$type 代表SQL语句的类型,0代表增删改,1代表查询
11     function query($sql,$type=1)
12     {
13         $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
14         
15         $result = $db->query($sql);
16         
17         if($type)
18         {
19             //如果是查询,显示数据
20             return $result->fetch_all();
21         }
22         else
23         {
24             //如果是增删改,返回true或者false
25             return $result;
26         }
27     }
28 }

 

2.删除处理界面 sanchu.php

 1 <?php
 2 $arr = $_POST["ck"];
 3 
 4 require"DBDA.class.php";
 5 $db = new DBDA();
 6 //delete from nation where code in('n001','n002','n003')
 7 
 8 $str = implode("','",$arr); 
 9 $sql = "delete from nation where code in('{$str}')";
10 /*echo $sql;*/
11 if($db->query($sql,0))
12 {
13     header("location:piliangcaozuo.php");
14 }

 

转载于:https://www.cnblogs.com/zhaohui123/p/6796892.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值