<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> //使用自引用表单,$_SERVER['PHP_SELF']变量显示PHP文件自身文件名 <?php //连接MySQL数据库 $dbc=mysqli_connect('localhost','root','root','test_store') or die('Error conneting to MySQL server.'); //判断表单是否提交过 if(isset($_POST['submit'])){ foreach($_POST['todelete'] as $delete_id){
//$_POST['todelete']就是todelete[]数组,被选中的复选框的值都会存在这个数组里面 $query="delete from email_list where id=$delete_id"; mysqli_query($dbc,$query) or die('error delete data.'); } echo "email is removed.<br />"; } //查询email_list数据表 $query="select * from email_list"; $result=mysqli_query($dbc,$query) or die('error select database.'); //生成复选框 while($row=mysqli_fetch_array($result)){ //$result存储的是查询到的整张数据表;while每执行一次,$row数组中存储的是数据表的一行数据。 echo '<input type="checkbox" value="'.$row['id'].'" name="todelete[]" />'; //注意name属性中方括号 echo $row['email']; echo '<br />'; } //关闭与数据库的连接 mysqli_close($dbc); ?> <br /> <input type="submit" name="submit" value="submit"> </form>