php如何實現(xiàn)批量刪除數(shù)據(jù)
首先在前端將需要刪除的數(shù)據(jù)的ID,以數(shù)組形式提交到后端php;然后在php中通過“$_GET”來獲取要刪除的數(shù)據(jù)ID,最后進(jìn)行連接MySQL數(shù)據(jù)庫,將數(shù)據(jù)逐個刪除或使用“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('必須選擇一個產(chǎn)品,才可以刪除!');history.back(-1);</script>"; exit; } else { /*如果要獲取全部數(shù)值則使用下面代碼*/ $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教程》