当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP 批量删除数据的方法分析

PHP
PHP新手总结的PHP基础知识
php实现gb2312和unicode间编码转换
用php语言实现数据库连接详细代码介绍
详细解析 PHP 向 MySQL 发送数据过程
利用PHP V5开发多任务应用程序
详细讲解PHP中缓存技术的应用
php escapeshellcmd多字节编码漏洞
《PHP设计模式介绍》导言
《PHP设计模式介绍》第一章 编程惯用法
《PHP设计模式介绍》第二章 值对象模式
《PHP设计模式介绍》第三章 工厂模式
《PHP设计模式介绍》第四章 单件模式
《PHP设计模式介绍》第五章 注册模式
《PHP设计模式介绍》第六章 伪对象模式
《PHP设计模式介绍》第七章 策略模式
《PHP设计模式介绍》第八章 迭代器模式
《PHP设计模式介绍》第九章 观测模式
《PHP设计模式介绍》第十章 规范模式
《PHP设计模式介绍》第十一章 代理模式
《PHP设计模式介绍》第十二章 装饰器模式

PHP 批量删除数据的方法分析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 163 ::
收藏到网摘: n/a

好多朋友在网站开发中,经常需要批量删除数据,尤其是习惯了asp的朋友,更是感觉asp下真方便了,php下什么都是数组有点麻烦。 大家可以参考下面的这篇文章http://www.ruanchen.com/"delete from `doing` where id in ('1,2,3,4')";
  数据用逗号隔开。
  表单:
复制代码 代码如下:

  <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']将会是一个数组,虽然说PHP是弱类型的,但这里可没ASP弱。
  ASP可以直接:
  SQL="delete from [doing] where id in ('"&ID_Dele&"')"进行删除。但PHP不能把$ID_Dele直接放进去。因为$ID_Dele可不是'1,2,3,4'哦,因为$ID_Dele是一个数组,具有键和值。
  好,PHP中也不难,刚好有个函数:implode(),对了。同split()explode()功能刚好相反的一个函数,后两者是用某字符(比如逗号)分割的,而前者则可以拼接为字符串。
  因此:
复制代码 代码如下:

  $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 "请提交";
}
?>
<form action="?action=doing" method="post">
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第1个"/>第1个
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第2个"/>第2个
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第3个"/>第3个
<input name="ID_Dele[]" type="checkbox" id="ID_Dele[]" value="第4个"/>第4个
<input type="submit"/>
</form>