php批量刪除操作(數據訪問)
更新時間:2017年05月23日 11:47:13 作者:ChrissZhao
這篇文章主要為大家詳細介紹了php批量刪除操作,批量刪除頁面和.除處理界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了php批量刪除操作的具體代碼,供大家參考,具體內容如下

1.批量刪除頁面 piliangcaozuo.php
<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.php
<?php
class DBDA
{
public $host = "localhost";
public $uid = "root";
public $pwd = "123";
public $dbname = "test_123";
//執(zhí)行SQL語句返回相應的結果
//$sql 要執(zhí)行的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.php
<?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");
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Windows下部署Apache+PHP+MySQL運行環(huán)境實戰(zhàn)
本來嘛,部署PHP沒什么復雜,找各種版本著實頭疼了一下。2012-08-08

