PHP實現(xiàn)批量刪除(封裝)
更新時間:2017年04月28日 14:54:14 作者:下頁、再停留
本篇文章主要介紹了PHP實現(xiàn)批量刪除(封裝)的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧
前臺
<!DOCTYPE html>
<html>
<head>
<title>批量刪除</title>
</head>
<body>
<script type="text/javascript">
//復選框
function checkall(all)
{
var ck = document.getElementsByClassName("ck");
if(all.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>
<form action="test.php" method="post">
<table border="1">
<tr><th><input type="checkbox" name="all" onclick="checkall(this)"/>id</th><th>名字</th></tr>
<!-- 此處調用顯示列表函數(shù) -->
<?php show() ?>
<tr><td colspan="3"><input type="submit" value="批量刪除"></td></tr>
</table>
</form>
</body>
<?php
//顯示列表
function show()
{
//連接數(shù)據(jù)庫
@mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names utf8');
$sql = "select id,name from test";
$res = mysql_query($sql);
//循環(huán)取出數(shù)據(jù)
while($row = mysql_fetch_row($res))
{
echo "<tr>
<td>
<input type='checkbox' value='{$row[0]}' name='item[]' class='ck' />
{$row[0]}
</td>
<td>{$row[1]}</td>
</tr>";
}
}
?>
</html>
后臺
<?php
//接收post傳來的數(shù)組
$arr = $_POST["item"];
/**
* 批量刪除
* 思路:把前臺批量選擇的數(shù)據(jù)放在數(shù)組里,刪除該數(shù)組即可
* @param $arr <array()>
* @return $res 成功or失敗
*/
function batch_del($arr)
{
@mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names utf8');
//把數(shù)組元素組合為字符串:
$str = implode("','",$arr);
//in 表示多個
$sql = "delete from test where id in('{$str}')";
$res = mysql_query($sql);
if (!$res){
echo "刪除失敗";
}else {
if (mysql_affected_rows()>0){
echo "刪除成功";
}else {
echo "沒有行受到影響";
}
}
}
//調用批量刪除函數(shù)
batch_del($arr);
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
php mb_substr()函數(shù)截取中文字符串應用示例
substr()函數(shù)用來截取字符串,但是對于中文字符會出現(xiàn)問題,而mb_substr()和mb_strcut這兩個函數(shù)可以,下面為大家介紹其具體用法2014-07-07
php str_getcsv把字符串解析為數(shù)組的實現(xiàn)方法
下面小編就為大家?guī)硪黄猵hp str_getcsv把字符串解析為數(shù)組的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
解決163/sohu/sina不能夠收到PHP MAIL函數(shù)發(fā)出郵件的問題
最近準備改改自己的個人主頁,加上發(fā)郵件到功能,發(fā)現(xiàn)居然不是那么好用。gmail和msn可以收到郵件,但是163/sina/sohu的郵箱居然都收不到,非常郁悶。其實代碼也很簡單。 php manual 已經說得相當清楚了。2009-03-03

