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" onclick="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 }

 

posted @ 2017-05-02 16:48  ChrissZhao  阅读(735)  评论(0编辑  收藏  举报