php批量刪除操作代碼分享
更新時間:2017年02月26日 11:55:22 作者:我之姓冠你之名
本文給大家分享的是使用php實現(xiàn)批量刪除MySQL數(shù)據(jù)庫內的數(shù)據(jù),非常的簡單,有需要的小伙伴可以參考下
批量刪除多條記錄,對于比較多的信息,如果沒有批量刪除功能是非常麻煩的。
1.從數(shù)據(jù)庫中拿一張表過來,寫個復選框進行選擇
可以加全選復選框
連接數(shù)據(jù)庫什么的都不寫啦
代碼:
<form action="piliangshanchu.php" method="post" >
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="200">
<input type="checkbox" value="''" name="dx" onclick="checkall(this)" />
編號</td>
<td width="200">姓名</td>
<td width="200">電話</td>
<td width="200" >分組</td>
<td width="200" >操作</td>
</tr>
<tr>
<td>
<input type='checkbox' value='{$attr[0]}' name='item[]' class='ck' />
{$attr[0]}</td>
<td>{$str}</td>
<td>{$attr[2]}</td>
<td>{$nation}</td>
</tr>
</table>
<input type="submit" value="批量刪除"/>
</form>
外加一個批量刪除按鈕
上圖:

我如果點擊全選,利用js點擊事件就可以輕松實現(xiàn)全選
代碼:
<script>
function xxx(qx)
{
//全選多選的選中狀態(tài)
var ck = document.getElementsByClassName("ck");
//讓下面所有的多選選中狀態(tài)改變
if(qx.checked)
{
for(i = 0;i < ck.length ; i++)
{
ck[i].setAttribute("checked","checked");
//狀態(tài)改變?yōu)檫x中
}
}
else
{
for(var i = 0;i < ck.length;i++)
{
ck[i].removeAttribute("checked");
//移除選中
}
}
}
</script>
2.刪除的處理頁面
代碼:
<?php
$arr = $_POST["item"];
$db = new mysqli("localhost","root","12345678","heiheihei");
//foreach($arr as $v)
//{
// $sql = "delete from contacts WHERE id='{$v}'";
// $db->query($sql);
//}
$str = implode("','",$arr);//拼接字符,
$sql = "delete from contacts WHERE id in('{$str}')";
//2','8','4
if($db->query($sql))//判斷是否查詢成功,
{
header("location:shouye.php");
//成功就跳轉
}
?>
用foreach數(shù)據(jù)傳輸過慢,刪除遍歷繁多,因此直接判斷;
相關文章
tp5使用layui實現(xiàn)多個圖片上傳(帶附件選擇)的方法實例
在以前項目中遇到有上傳圖片都是用 web Uploader插件上傳,因為我現(xiàn)在項目都在使用 layui 所以今天趁著有時間寫一個tp5結合layui上傳圖片的示例,感興趣的可以了解一下2021-10-10
PHP CURL或file_get_contents獲取網(wǎng)頁標題的代碼及兩者效率的穩(wěn)定性問題
PHP CURL與file_get_contents函數(shù)都可以獲取遠程服務器上的文件保存到本地,但在性能上面兩者完全不在同一個級別,下面通過一個例子給大家介紹PHP CURL或file_get_contents獲取網(wǎng)頁標題的代碼及兩者效率的穩(wěn)定性問題,需要的朋友參考下2015-11-11

