php批量刪除可以實現多條或者全部數據一起刪除
新建php文件 顯示數據庫中內容:
<table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td><input type="checkbox" id="qx" onclick="xuanzhong()" />全選</td> <td>代號</td> <td>名稱</td> </tr> <?php include("DBDA.class.php"); $db = new DBDA(); $sql = "select areacode,areaname from nation"; $attr = $db->Query($sql); foreach($attr as $v) { echo "<tr> <td><input type='checkbox' name='ck[]' class='ck' value='{$v[0]}' /></td> <td>{$v[0]}</td> <td>{$v[1]}</td> </tr>"; } ?> </table>
DBDA.class.php文件為數據庫查詢的類文件:
<?php class DBDA { public $host="localhost"; public $uid = "root"; public $pwd = ""; public $dbname = "12345"; //成員方法 public function Query($sql,$type=1) { $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); $r = $db->query($sql); if($type==1) { return $r->fetch_all(); } else { return $r; } } }
在表格 中加入選擇復選框:
<td><input type="checkbox" id="qx" onclick="xuanzhong()" />全選</td>
<td><input type='checkbox' name='ck[]' class='ck' value='{$v[0]}' /></td>
顯示:
用js控制復選框的全選和取消全選:
<script type="text/javascript"> function xuanzhong() { //取全選按鈕的選中狀態 var zt = document.getElementById("qx").checked; //讓下面所有的checkbox選中狀態改變 var ck = document.getElementsByClassName("ck"); for(var i=0;i<ck.length;i++) { if(zt) { ck[i].setAttribute("checked","checked"); } else { ck[i].removeAttribute("checked"); } } } </script>
表格外側追加form表單和提交按鈕,並且用js控制點擊刪除時顯示詳細的提示信息完整php代碼:
<!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> <form action="shanchu.php" method="post"> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td><input type="checkbox" id="qx" onclick="xuanzhong()" />全選</td> <td>代號</td> <td>名稱</td> </tr> <?php include("DBDA.class.php"); $db = new DBDA(); $sql = "select areacode,areaname from chinastates"; $attr = $db->Query($sql); foreach($attr as $v) { echo "<tr> <td><input type='checkbox' name='ck[]' class='ck' value='{$v[0]}' /></td> <td>{$v[0]}</td> <td>{$v[1]}</td> </tr>"; } ?> </table> <input type="submit" value="刪除" onclick="return tishi()" /> </form> </body> <script type="text/javascript"> function xuanzhong() { //取全選按鈕的選中狀態 var zt = document.getElementById("qx").checked; //讓下面所有的checkbox選中狀態改變 var ck = document.getElementsByClassName("ck"); for(var i=0;i<ck.length;i++) { if(zt) { ck[i].setAttribute("checked","checked"); } else { ck[i].removeAttribute("checked"); } } } function tishi() { //找所有選中項 var ck = document.getElementsByClassName("ck"); var str = ""; for(var i=0;i<ck.length;i++) { if(ck[i].checked) { str += ck[i].value+","; } } return confirm("確定要刪除以下數據么:"+str+""); } </script> </html>
最后新建刪除處理的php文件;
<?php $ck = $_POST["ck"]; include("DBDA.class.php"); $db = new DBDA(); //第一種方式 /*foreach($ck as $v) { $sql = "delete from nation where code='{$v}'"; $db->Query($sql,0); }*/ //第二種方式 //in ('','','','','') $str = implode("','",$ck); $str = "('{$str}')"; $sql = "delete from nation where code in {$str}"; $db->Query($sql,0); header("location:main.php");
點擊確定:
批量刪除成功!