php如何实现批量删除数据-PHP问题

资源魔 21 0

php若何完成批量删除了数据

起首正在前端将需求删除了的数据的ID,以数组方式提交到后端php;而后正在php中经过“$_GET”来猎取要删除了的数据ID,最初进行衔接MySQL数据库,将数据一一删除了或应用“IN”一次性删除了便可。

示例代码

HTML:

<form id="form2" name="form2" method="post" action="del_product.php">
    <label>
        <input type="checkbox" name="id[]" value="1" style="background:none; border:none;" />
            1
    </label>
    <label>
        <input type="checkbox" name="id[]" value="2" style="background:none; border:none;" />
            2
    </label>
    <label>
        <input type="checkbox" name="id[]" value="3" style="background:none; border:none;" />
            3
    </label>
    <label>
        <input type="checkbox" name="id[]" value="4" style="background:none; border:none;" />
            4
    </label>
    <label>
        <input type="checkbox" name="id[]" value="5" style="background:none; border:none;" />
            5
    </label>
    <div style="padding-left:20px;">
        <input type="button" value="全选" style="background:url(images/cheall.jpg) no-repeat; width:60px; height:23px; border:none;" onClick="selectBox('all')"/>
        <input type="button" value="反选" style="background:url(images/cheall.jpg) no-repeat; width:60px; height:23px; border:none;" onClick="selectBox('reverse')"/>
        <input type="submit" name="btnSave" style="background:url(images/cheall.jpg) no-repeat; width:60px; height:23px; border:none;" value="删除了"/>
    </div>
</form>

PHP:

if($_POST['btnSave']){
  if(empty($_POST['id'])){
         echo"<script>alert('必需抉择一个产物,才能够删除了!');history.back(-1);</script>";
       exit;
     } else {
     /*假如要猎取全副数值则应用上面代码*/
     $id= implode(",",$_POST['id']);
     $str="DELETE FROM `product` where id in ($id)";
     mysql_query($str);
     echo "<script>alert('删除了胜利!');window.location.href='product_list.php';</script>";
  }
}

保举教程:《PHP教程》

以上就是php若何完成批量删除了数据的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题 批量删除

抱歉,评论功能暂时关闭!