gpt4 book ai didi

php - 正确获取和删除帖子

转载 作者:行者123 更新时间:2023-11-30 21:41:39 26 4
gpt4 key购买 nike

我能得到一些帮助吗?

索引.php:

<form class="delete" id="delete" action="../functions/delete.php" method="POST">

<table class="table">

<?php
include('../../configs/dbconfig.php');

$sql = "SELECT id, title FROM posts";
$posts = $connection->query($sql);
while($row = $posts->fetch_assoc()) {
$id = $row["id"];
$title = $row["title"];
?>

<thead class="thead-light">
<tr>
<th scope="col">#</th>
<th scope="col">Headline</th>
<th scole="col">Action</th>
</tr>
</thead>
<tbody>
<tr>
<th><?php echo $id; ?></th>
<td><?php echo $title; ?></td>
<td><button type="submit" id="btn-delete" name="submit" class="btn btn-light"><i class="fas fa-minus-circle"></i> Delete</button></td>
</tr>
</tbody>
<?php } ?>
</table>
</form>

删除.php:

<?php
include('../../configs/dbconfig.php');

$id = $_POST['id']; // Can I get some help?
$sql = "DELETE FROM posts WHERE id=?";
$stmt = $connection->prepare($sql);
$stmt->bind_param("i", $id);
if($stmt->execute()) {
echo "deleted";
} else {
echo "error";
}
?>

它生成每个帖子的 ID 和标题,并且有一个按钮,单击它后,它应该删除正确的 ID,但它总是使用以下代码删除最后一个:

<input type="hidden" name="id" value="<?php echo $id; ?>">

最佳答案

给你的按钮一个并使用$id:

<td><button type="submit" name="submit" value="<?php echo $id; ?>" id="btn-delete" class="btn btn-light"><i class="fas fa-minus-circle"></i> Delete</button></td>

然后使用名称获取:

$id = $_POST['submit'];

关于php - 正确获取和删除帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com