首页 > 代码库 > php 批量删除

php 批量删除

主页面

  1 <form action="main.php" method="post">
  2 <div>姓名:
  3 <input type="text" name="xm" /> 
  4 <input type="submit" value="http://www.mamicode.com/查询" />
  5 </div>
  6 </form>
  7 <br />
  8 
  9 <form action="piliangshanchu.php" method="post">
 10 <table width="100%" border="1" cellpadding="0" cellspacing="0">
 11     <tr>
 12         <td><input type="checkbox" onclick="quanxuan(this)" />代号</td>
 13         <td>姓名</td>
 14         <td>性别</td>
 15         <td>民族</td>
 16         <td>生日</td>
 17         <td>操作</td>
 18     </tr>
 19     
 20     <?php
 21     
 22     //先判断有没有提交值
 23     $xxm = "";
 24     $tj = " 1=1 ";
 25     if(!empty($_POST["xm"]) && $_POST["xm"]!="")
 26     {
 27         $xxm = $_POST["xm"];
 28         $tj = " name like ‘%{$xxm}%‘ ";
 29     }
 30     
 31     //造连接对象
 32     $db = new  MySQLi("localhost","root","123","mydb");
 33     //写SQL语句
 34     $sql = "select * from info where ".$tj;
 35     echo $sql;
 36     //执行SQL语句
 37     $result = $db->query($sql);
 38     //读数据
 39     $attr = $result->fetch_all();
 40     
 41     foreach($attr as $v)
 42     {
 43         echo "<tr>";
 44         
 45         $sex = $v[2]?"男":"女";
 46         
 47         //根据名族代号查询名族名称
 48         $name = NationName($v[3]);
 49         
 50         //替换关键字
 51         $newname = str_replace($xxm,"<mark>{$xxm}</mark>",$v[1]);
 52         
 53         echo "<td><input type=‘checkbox‘ value=http://www.mamicode.com/‘{$v[0]}‘ name=‘sc[]‘ class=‘qx‘ />{$v[0]}</td><td>{$newname}</td><td>{$sex}</td><td>{$name}</td><td>{$v[4]}</td><td><a href=http://www.mamicode.com/‘shanchu.php?c={$v[0]}‘ onclick=\"return confirm(‘确定删除么?‘)\">删除</a><a href=http://www.mamicode.com/‘xiugai.php?c={$v[0]}‘>修改</a></td>";
 54         
 55         /*foreach($v as $v1)
 56         {
 57             echo "<td>{$v1}</td>";
 58         }*/
 59         
 60         echo "</tr>";
 61     }
 62     
 63     //给一个民族代号,返回民族名称
 64     function NationName($code)
 65     {
 66         //造连接对象
 67         $db = new  MySQLi("localhost","root","123","mydb");
 68         //写SQL语句
 69         $sql = "select name from nation where code=‘{$code}‘";
 70         //执行SQL语句
 71         $result = $db->query($sql);
 72         
 73         $attr = $result->fetch_row();
 74         
 75         return $attr[0];
 76     }
 77     
 78     ?>
 79     
 80     
 81 </table>
 82 <a href="http://www.mamicode.com/add.php"><input type="button" value="http://www.mamicode.com/添加数据" /></a>
 83 
 84 <input type="submit" value="http://www.mamicode.com/批量删除" onclick="return confirm(‘确定删除么‘)" />
 85 </form>
 86 
 87 <script type="text/javascript">
 88 
 89 function quanxuan(a)
 90 {
 91     //找到下面所有的复选框
 92     var ck =document.getElementsByClassName("qx");
 93     
 94     //遍历所有复选框,设置选中状态
 95     for(var i=0;i<ck.length;i++)
 96     {
 97         if(a.checked)
 98         {
 99             ck[i].setAttribute("checked","checked");
100         }
101         else
102         {
103             ck[i].removeAttribute("checked");
104         }
105     }
106     
107 }
108 </script>

删除处理页面

 1 <?php
 2 $attr = array();
 3 if(!empty($_POST["sc"]))
 4 {
 5     $attr = $_POST["sc"];
 6 }
 7 
 8 $db = new  MySQLi("localhost","root","123","mydb");
 9 
10 /*foreach($attr as $v)
11 {
12     $sql = "delete from info where code=‘{$v}‘";
13     
14     $db->query($sql);
15 }
16 */
17 
18 $str = implode("‘,‘",$attr);
19 
20 //echo $str;
21 
22 $sql = "delete from info where code in(‘{$str}‘)";
23 $db->query($sql);
24 
25 ?>

 

php 批量删除