我正在让一个系统在一个表格中显示系统中的页面,而其中一个列是操作列给予人们删除页面的选项。我这样做:PHP表单当在表格中显示时删除mySQLi中的记录然后在表格中
listpages.php
<?php
$sql = "SELECT * FROM Pages";
$result = $conn->query($sql);
if ($result) {
while($row = $result->fetch_assoc()) {
$pages[] = array_map('htmlspecialchars', $row);
}
}
// PHP is finished now, here's the HTML
?>
<form method="post" action="utils/delpage.php">
<table>
<thead>
<tr>
<th>Page Name</th>
<th>Page Type</th>
<th>Registered on</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($pages as $page):?>
<tr>
<input type="hidden" name="targetid" value="<?= $page['id'] ?>"></input>
<td><?= $page['pagename'] ?></td>
<td><?= $page['pagetype'] ?></td>
<td><?= $page['regdate'] ?></td>
<td><input type="submit" value="delete" class="red-text material-icons tooltipped" data-tooltip="Delete"><td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</form>
delpage.php
<?php
include ('../../php/db.php');
$id = $_POST['targetid'];
$target = htmlentities($id);
$stmt = $conn->prepare("DELETE FROM Pages WHERE id = (?)");
$stmt->bind_param("i", $page);
$page = $target;
$stmt->execute();
?>
这部分的工作:我得到的垃圾桶图标我设置为删除记录。问题是,当我点击它时,它总是删除最后一条记录,而不是它被删除的记录。这是一个问题,而不是它真的应该如何工作。我不明白为什么它会这样做,因为ID正在以与其他信息相同的方式“显示”。我搜索了一个答案,但没有任何工作,我没有看到许多选择,但创建一个问题。任何帮助?
在此先感谢
刚刚看过的代码@ user2860957 – MucaP