PHP 批量删除
1.在主页面上添加批量删除有关代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body>
<?php //先判断有没有提交值 $xxm = ""; $tj = " 1=1 "; $tj2 = " 1=1 ";//再做一个条件 if(!empty($_POST["xm"]) && $_POST["xm"]!="")//这个条件是判断xm的,判断模糊查询。 { $xxm = $_POST['xm']; $tj = " name like '%{$_POST['xm']}%' "; } if(!empty($_POST["mz"]) && $_POST["mz"]!="")//再做一个条件判断另一个条件 { $mz = $_POST['mz'];//取民族的代号 $tj2 = " nation = '{$_POST['mz']}' "; } ?>
<br /> <form action="main.php" method="post"><!--点击查询时会将用户输入的值传递到本页面--> <div>姓名: <input type="text" name="xm" value="<?php echo $xxm ?>" /><!--输入查询的关键字--> 民族代号: <input type="text" name="mz" /> <input type="submit" value="查询" /><!--提交按钮,需要一个表单--> </div> </form> <br /> <form action="piliangshanchu.php" method="post"> <!--增加一个form表单--> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr>
<?php $db = new MySQLi("localhost","root","666","text1"); $sql = "select * from info where ".$tj." and ".$tj2; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { echo"<tr>"; $sex = $v[2]?"男":"女"; $name = NationName($v[3]); $newname = str_replace($xxm,"<mark>{$xxm}</mark>",$v[1]);//替换关键字 echo"<td><input type='checkbox' value='{$v[0]}' name='sc[]' />{$v[0]}</td><td>{$newname}</td><td>{$sex}</td><td>{$name}</td><td>{$v[4]}</td><td><a href='shanchu.php?c={$v[0]}' onclick=\"return confirm('确定删除吗?')\">删除</a><a href='xiugai.php?c={$v[0]}'>修改</a></td"; //在第一列里面做复选框 //name='sc[]'表示提交时提交的是一个数组
//批量删除时增加提示信息“确认删除吗?”。
/*foreach($v as $v1) { echo "<td>{$v1}</td>"; }*/ echo"</tr>"; } function NationName($code) { $db = new MySQLi("localhost","root","666","text1"); $sql = "select name from nation where code = '{$code}'"; $result = $db->query($sql); $attr = $result->fetch_row(); return $attr[0]; } ?>
</table> <a href="add.php"><input type="button" value="添加数据" /></a> <input type="submit" value="批量删除" onclick="return confirm('确定删除吗?')" /> </form> </body> </html>
2.批量删除处理页面
<?php $attr = array();//定义一个空的数组 if(!empty($_POST["sc"]))//如果不为空 { $attr = ($_POST["sc"]);//去选定的值 } $db = new MySQLi("localhost","root","666","text1"); $str = implode("','",$attr); $sql = "delete from info where code in('{$str}')"; $db->query($sql); /*foreach($attr as $v)//这种遍历方法会增加运算量,不如上面的方法简单。 { $db = new MySQLi("localhost","root","666","text1"); $sql = "delete from info where code='{$v}'"; $db->query($sql);//调用$sql }*/ header("location:main.php");
3.做一个批量删除的按钮,点击可以让所有的为选中状态或者非选中状态,用JS做,处理页面不变。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body>
<?php //先判断有没有提交值 $xxm = ""; $tj = " 1=1 "; $tj2 = " 1=1 ";//再做一个条件 if(!empty($_POST["xm"]) && $_POST["xm"]!="")//这个条件是判断xm的,判断模糊查询。 { $xxm = $_POST['xm']; $tj = " name like '%{$_POST['xm']}%' "; } if(!empty($_POST["mz"]) && $_POST["mz"]!="")//再做一个条件判断另一个条件 { $mz = $_POST['mz'];//取民族的代号 $tj2 = " nation = '{$_POST['mz']}' "; } ?>
<br /> <form action="main.php" method="post"> <div>姓名: <input type="text" name="xm" value="<?php echo $xxm ?>" /> 民族代号: <input type="text" name="mz" /> <input type="submit" value="查询" /> </div> </form> <br /> <form action="piliangshanchu.php" method="post"> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td><input type="checkbox" onclick="quanxuan(this)" />代号</td><!--在代号里面做一个全选按钮,再做一个quanxuan方法,传一个this过去--> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr>
<?php $db = new MySQLi("localhost","root","666","text1"); $sql = "select * from info where ".$tj." and ".$tj2; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { echo"<tr>"; $sex = $v[2]?"男":"女"; $name = NationName($v[3]); $newname = str_replace($xxm,"<mark>{$xxm}</mark>",$v[1]);//替换关键字 echo"<td><input type='checkbox' value='{$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='shanchu.php?c={$v[0]}' onclick=\"return confirm('确定删除吗?')\">删除</a><a href='xiugai.php?c={$v[0]}'>修改</a></td"; //加一个class='qx'; /*foreach($v as $v1) { echo "<td>{$v1}</td>"; }*/ echo"</tr>"; } function NationName($code) { $db = new MySQLi("localhost","root","666","text1"); $sql = "select name from nation where code = '{$code}'"; $result = $db->query($sql); $attr = $result->fetch_row(); return $attr[0]; } ?>
</table> <a href="add.php"><input type="button" value="添加数据" /></a> <input type="submit" value="批量删除" onclick="return confirm('确定删除吗?')" /> </form> <script type="text/javascript"> function quanxuan(a) { //找到下面所有的复选框 var ck =document.getElementsByClassName("qx"); //遍历所有复选框,设置选中状态。 for(var i=0;i<ck.length;i++) { if(a.checked)//判断全选按钮的状态是不是选中的 { ck[i].setAttribute("checked","checked");//如果是选中的,就让所有的状态为选中。 } else { ck[i].removeAttribute("checked");//如果不是选中的,就移除所有的状态是checked的选项。 } } } </script> </body> </html>