《PHP學習:php批量刪除操作(數據訪問)》要點:
本文介紹了PHP學習:php批量刪除操作(數據訪問),希望對您有用。如果有疑問,可以聯系我們。
本文實例為大家分享了php批量刪除操作的具體代碼,供大家參考,具體內容如下PHP教程
PHP教程
1.批量刪除頁面 piliangcaozuo.phpPHP教程
<body> <form action="shanchu.php" method="post"> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td><input type="checkbox" name="qx" onclick="quanxuan(this)"/>代號</td> <td>名稱</td> </tr> <?php require"DBDA.class1.php"; $db = new DBDA(); $sql = "select * from nation"; $arr = $db->query($sql); foreach($arr as $v) { echo "<tr> <td><input type='checkbox' name='ck[]' class='ck' value='{$v[0]}'/>{$v[0]}</td> <td>{$v[1]}</td> </tr>"; } ?> </table> <input type="submit" value="批量刪除" /> </form> </body> <script type="text/javascript"> function quanxuan(qx) { var ck=document.getElementsByClassName("ck"); if(qx.checked) { for(var i=0;i<ck.length;i++) { ck[i].setAttribute("checked","checked"); } } else { for(var i=0;i<ck.length;i++) { ck[i].removeAttribute("checked"); } } } </script> </html>
引用的封裝類 DBDA.class1.phpPHP教程
<?php class DBDA { public $host = "localhost"; public $uid = "root"; public $pwd = "123"; public $dbname = "test_123"; //執行SQL語句返回相應的結果 //$sql 要執行的SQL語句 //$type 代表SQL語句的類型,0代表增刪改,1代表查詢 function query($sql,$type=1) { $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname); $result = $db->query($sql); if($type) { //如果是查詢,顯示數據 return $result->fetch_all(); } else { //如果是增刪改,返回true或者false return $result; } } }
2.刪除處理界面 sanchu.phpPHP教程
<?php $arr = $_POST["ck"]; require"DBDA.class.php"; $db = new DBDA(); //delete from nation where code in('n001','n002','n003') $str = implode("','",$arr); $sql = "delete from nation where code in('{$str}')"; /*echo $sql;*/ if($db->query($sql,0)) { header("location:piliangcaozuo.php"); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持維易PHP.PHP教程
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/744.html