【发布时间】:2018-05-20 10:58:05
【问题描述】:
我正在尝试在 while 循环中删除数据库记录。我在带有 while 循环的表中显示我的用户列表。我有一个按钮,引导模式打开一个模式窗口。在那个窗口中,我有提交删除按钮。带有while循环。 问题是,我正在尝试删除此记录,但它正在删除随机记录。你能检查一下有没有问题?
已经谢谢了。 这是我的代码:
<table class="table table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>*</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users";
$r = mysqli_query($dbc,$q);
while($list = mysqli_fetch_assoc($r)){
if(isset($_POST['del_submit'])){
$q = "DELETE FROM users WHERE id = '$list[id]' ";
$r = mysqli_query($dbc, $q);
header('Location: index.php?page=7');
}
echo '<tr>';
echo '<td>'.$list['id'].'</td>';
echo '<td>'.$list['name'].'</td>';
echo '<td>'.$list['surname'].'</td>';
echo '<td>'.$list['email'].'</td>';
echo '<td>'.$list['password'].'</td>';
echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> ';
echo '</tr><form method="post" action="#">';
echo '<div class="modal fade delete'.$list['id'].'">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4>
</div>
<div class="modal-body">
<strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br>
Are you Sure?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" name="del_submit" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
</div>
</div>
</div>
</div></form>';
}
?>
</tbody>
</table>
【问题讨论】:
-
您检查过错误日志吗?您正在假设查询正在运行。在您打开
<?php标记error_reporting(E_ALL); ini_set('display_errors', 1);后立即在文件顶部添加错误报告 -
我认为它删除了最后一条记录,或者没有?将项目删除到选择循环中很奇怪。使用条件
where id = (int)$_POST['id']删除记录,而不是像$list['id']这样的废话 - 不要忘记在 HTML 中添加带有 ID 的隐藏输入。 -
它删除列表中的第一个 id。例如第一个 id = 1 第二个 =2 ...第四个:4 例如:如果我点击第三个删除按钮,它会删除第一条记录。
标签: php mysql twitter-bootstrap mysqli while-loop