PHP 批量刪除數(shù)據(jù)的方法分析
更新時(shí)間:2009年10月30日 19:44:12 作者:
好多朋友在網(wǎng)站開發(fā)中,經(jīng)常需要批量刪除數(shù)據(jù),尤其是習(xí)慣了asp的朋友,更是感覺asp下真方便了,php下什么都是數(shù)組有點(diǎn)麻煩。
大家可以參考下面的這篇文章http://www.dbjr.com.cn/article/6488.htm
SQL:$SQL="delete from `doing` where id in ('1,2,3,4')";
數(shù)據(jù)用逗號隔開。
表單:
<form action="?action=doing" method="post">
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="1"/>
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="2"/>
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="3"/>
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="4"/>
<input type="submit"/>
</form>
好$ID_Dele=$_POST['ID_Dele']將會(huì)是一個(gè)數(shù)組,雖然說PHP是弱類型的,但這里可沒ASP弱。
ASP可以直接:
SQL="delete from [doing] where id in ('"&ID_Dele&"')"進(jìn)行刪除。但PHP不能把$ID_Dele直接放進(jìn)去。因?yàn)?ID_Dele可不是'1,2,3,4'哦,因?yàn)?ID_Dele是一個(gè)數(shù)組,具有鍵和值。
好,PHP中也不難,剛好有個(gè)函數(shù):implode(),對了。同split()explode()功能剛好相反的一個(gè)函數(shù),后兩者是用某字符(比如逗號)分割的,而前者則可以拼接為字符串。
因此:
$ID_Dele= implode(",",$_POST['ID_Dele']);
$SQL="delete from `doing` where id in ($ID_Dele)";
腳本之家提供測試代碼:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<?php
if ($_POST["action"]="doing"){
$del_id=$_POST["ID_Dele"];
$ID_Dele= implode(",",$_POST['ID_Dele']);
echo "合并后:".$ID_Dele."<br />合并前:";
if($del_id!=""){
$del_num=count($del_id);
for($i=0;$i<$del_num;$i++){
echo $del_id[$i];
}
}
}else{
echo "請?zhí)峤?;
}
?>
<form action="?action=doing" method="post">
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第1個(gè)"/>第1個(gè)
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第2個(gè)"/>第2個(gè)
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第3個(gè)"/>第3個(gè)
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第4個(gè)"/>第4個(gè)
<input type="submit"/>
</form>
SQL:$SQL="delete from `doing` where id in ('1,2,3,4')";
數(shù)據(jù)用逗號隔開。
表單:
復(fù)制代碼 代碼如下:
<form action="?action=doing" method="post">
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="1"/>
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="2"/>
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="3"/>
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="4"/>
<input type="submit"/>
</form>
好$ID_Dele=$_POST['ID_Dele']將會(huì)是一個(gè)數(shù)組,雖然說PHP是弱類型的,但這里可沒ASP弱。
ASP可以直接:
SQL="delete from [doing] where id in ('"&ID_Dele&"')"進(jìn)行刪除。但PHP不能把$ID_Dele直接放進(jìn)去。因?yàn)?ID_Dele可不是'1,2,3,4'哦,因?yàn)?ID_Dele是一個(gè)數(shù)組,具有鍵和值。
好,PHP中也不難,剛好有個(gè)函數(shù):implode(),對了。同split()explode()功能剛好相反的一個(gè)函數(shù),后兩者是用某字符(比如逗號)分割的,而前者則可以拼接為字符串。
因此:
復(fù)制代碼 代碼如下:
$ID_Dele= implode(",",$_POST['ID_Dele']);
$SQL="delete from `doing` where id in ($ID_Dele)";
腳本之家提供測試代碼:
復(fù)制代碼 代碼如下:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<?php
if ($_POST["action"]="doing"){
$del_id=$_POST["ID_Dele"];
$ID_Dele= implode(",",$_POST['ID_Dele']);
echo "合并后:".$ID_Dele."<br />合并前:";
if($del_id!=""){
$del_num=count($del_id);
for($i=0;$i<$del_num;$i++){
echo $del_id[$i];
}
}
}else{
echo "請?zhí)峤?;
}
?>
<form action="?action=doing" method="post">
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第1個(gè)"/>第1個(gè)
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第2個(gè)"/>第2個(gè)
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第3個(gè)"/>第3個(gè)
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第4個(gè)"/>第4個(gè)
<input type="submit"/>
</form>
相關(guān)文章
php利用scws實(shí)現(xiàn)mysql全文搜索功能的方法
這篇文章主要介紹了php利用scws實(shí)現(xiàn)mysql全文搜索功能的方法,可通過scws分詞插件的擴(kuò)展來實(shí)現(xiàn)MySQL全文搜索功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12php警告Creating default object from empty value 問題的解決方法
PHP 提示 Creating default object from empty value 的問題,一般是由于PHP版升級的原因,PHP 5.4 以上的版本一般會(huì)報(bào)這個(gè)錯(cuò)誤2014-04-04深入eAccelerator與memcached的區(qū)別詳解
本篇文章是對eAccelerator與memcached的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06thinkphp實(shí)現(xiàn)把數(shù)據(jù)庫中的列的值存到下拉框中的方法
本文主要介紹了thinkphp把數(shù)據(jù)庫中的列的值存到下拉框中的方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01PHP數(shù)據(jù)集構(gòu)建JSON格式及新數(shù)組的方法
自己寫了個(gè)PHP結(jié)果集轉(zhuǎn)換成JSON格式的函數(shù),可以直接調(diào)用;需要的朋友可以參考下2012-11-11