使用PDO从MySQL表中删除条目

问题描述 投票:-3回答:1

我想在用户按下删除按钮时从桌子中删除游戏。

我的项目的想法是,当您单击游戏的图像或标题时,您会在details.php上重定向,在这里您可以查看有关游戏的信息,并且还有一个删除按钮。我想获取游戏的ID并在表格中找到它,然后删除所有内容的游戏。

index.html

<a class="details" href="templates/details.php?id=<?php echo $game['id_game'] ?>">
    <img class="thumbnails card-img-top" src="images/cyberpunk2077.jpg">
</a>
<div class="card-body">
    <a class="details" href="templates/details.php?id=<?php echo $game['id_game'] ?>">
        <h6 class="card-title"><?php echo htmlspecialchars($game['title']); ?></h6>
    </a>

我不确定echo是否正确,这就是我以前使用mysqli的方式。

我的details.php具有以下形式:

<form action="details.php" method="POST">
    <input type="hidden" name="id" value="<?php echo $game['id_game']; ?>">
    <input type="submit" name="delete" value="Delete" class="btn">
</form>

并且在第二行中,我得到一个错误,指出$game未定义。我需要先获取整个表吗?这是我要删除游戏的PHP代码:

if (isset($_POST['delete'])) {
    $id = $_POST['id'];
    $stmt = $conn->prepare("DELETE FROM games WHERE id_game = :id");
    $stmt->bindParam(':id', $id);
    $stmt->execute();
    if ($stmt->rowCount()) {
        echo 'success';
    } else {
        echo 'error';
    }
}

它回显'错误'。

php mysql pdo
1个回答
0
投票

尝试

<form action="details.php" method="POST">
    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
    <input type="submit" name="delete" value="Delete" class="btn">
</form>

details.php

© www.soinside.com 2019 - 2024. All rights reserved.