php批量删除会员信息
php批量删除会员信息
在网站开发中,数据的增删改查都为基本的原子操作。对于列表的信息操作尤为常见。在一些应用中,一般都会提供批量删除功能。这样做,可以方便用户批量选取和删除数据。提高了应用的灵活性。也增加了用户体验度。在本例中,用户通过多选框选取完毕后。单击批量删除案例。可以批量删除数据中存储的会员信息。
核心关键点:
checkbox复选框name属性定义为member_id[] ,这样可以以数组组合的形式发送checkbox的value值。
实例代码:
<?php $conn = mysqli_connect('127.0.0.1','root','root'); mysqli_select_db($conn,'jichu'); mysqli_query($conn,"set names utf8"); $sql = "select * from user"; $rs = mysqli_query($conn,$sql); if(!empty($_POST['btn']) && !empty($_POST['member_id'])){ $arr = $_POST['member_id']; $str_key =""; foreach ($arr as $k=>$v){ $sql = "delete from user where id=".$v; mysqli_query($conn,$sql); $str_key .=$v.','; //拼接删除提示信息字符串 } $new_str = substr($str_key,0,strlen($str_key)-1); echo "<script>alert(\"删除编号为:{$new_str}的信息成功!\");location='37.php';</script>"; } ?>
<!doctype html> <html lang="en"> <body> <div style="max-width: 500px;"> <form action="" method="post"> <table border="1" style="min-width: 500px;"> <tr> <th>id</th> <th>用户名</th> <th>邮箱</th> <th>注册时间</th> <th>选择</th> </tr> <?php while($rows=mysqli_fetch_assoc($rs)) {?> <tr> <td><?php echo $rows['id']; ?></td> <td><?php echo $rows['username']; ?></td> <td><?php echo $rows['emial']; ?></td> <td><?php echo date("Y-m-d H:i:s",strtotime($rows['date'])); ?></td> <td><input type="checkbox" name="member_id[]" value="<?php echo $rows['id'];?>"></td> </tr> <?php }?> </table> <div style="margin-left: 335px;margin-top: 10px;"> <input type="submit" value="批量删除" name="btn"> </div> </form> </div> </body> </html>
运行结果:
上一篇:php实现表单计算器
下一篇:php计算文本字符串中单词数量