Home >Backend Development >PHP Problem >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('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>"; } }
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!