Home >Backend Development >PHP Problem >How to delete data in batches in php

How to delete data in batches in php

Guanhui
GuanhuiOriginal
2020-05-11 15:08:352944browse

How to delete data in batches in php

How to implement batch deletion of data in php

First, submit the ID of the data to be deleted on the front end to the backend php in the form of an array; Then use "$_GET" in php to get the data ID to be deleted, and finally connect to the MySQL database to delete the data one by one or use "IN" to delete it all at once.

Sample code

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(&#39;all&#39;)"/>
        <input type="button" value="反选" style="background:url(images/cheall.jpg) no-repeat; width:60px; height:23px; border:none;" onClick="selectBox(&#39;reverse&#39;)"/>
        <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[&#39;btnSave&#39;]){
  if(empty($_POST[&#39;id&#39;])){
         echo"<script>alert(&#39;必须选择一个产品,才可以删除!&#39;);history.back(-1);</script>";
       exit;
     } else {
     /*如果要获取全部数值则使用下面代码*/
     $id= implode(",",$_POST[&#39;id&#39;]);
     $str="DELETE FROM `product` where id in ($id)";
     mysql_query($str);
     echo "<script>alert(&#39;删除成功!&#39;);window.location.href=&#39;product_list.php&#39;;</script>";
  }
}

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of How to delete data in batches in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn