MySQL删除语句未删除行

3
以下是我从数据库中删除数据的代码。用户选择要删除的标题,然后查询应该删除这些行。不幸的是,这并没有起作用。
我已经确保DELETE查询在PhpMyAdmin和MySQL Workbench中运行正常,但由于某种原因,我的PHP代码不允许删除工作。
<?php
    session_start();
    require ("../login.php");

    if (!isset($_SESSION['user'])) 
        header('Location: ../index.php');

    include ("header.php");
    include ("subnav.php");
?>

    <h2>Current Subscriptions</h2>

    <?php

    $user_id = $_SESSION['user'];

    if (isset($_POST['submit'])) {
        foreach($_POST["titles"] as $title) {
            $stmt = $db->prepare("DELETE FROM subscriptions WHERE user=:user AND title=:title)");
            $stmt->bindValue(':user', $user_id, PDO::PARAM_INT);
            $stmt->bindValue(':title', $title, PDO::PARAM_INT);
            $stmt->execute();
        }
        echo '<p>Thank you! Your changes have been made!</p>';
    }
    else {
        $stmt = $db->prepare
            ("SELECT t.id, t.publisher, t.title
            FROM titles t
                JOIN subscriptions s
                    ON t.id = s.title
            WHERE s.user=:user
            ORDER BY t.id");

        $stmt->bindValue(':user', $user_id, PDO::PARAM_STR);
        $stmt->execute();
        $rows = $stmt->fetchAll();
        $num_rows = count($rows);
        if ($num_rows == 0)
            echo 'You are not currently subscribed to any titles.';
        else {
            ?>
            <form action="" method="post">
            <?php   
            foreach($rows as $row) 
                echo '<input type="checkbox" name="titles[]" value="' . $row['id'] . '">' . $row['publisher'] . ' - ' . $row['title'] . '<br>';
            ?>
            <input type="submit" value="Update" name ="submit" id="submit">
                </form>
        <?php
        }
    }
    ?>

<?php   
    include ("footer.php");
?>

有错误消息吗?假设错误报告已开启?您是否使用与 Workbench 相同的用户/权限连接到数据库? - undefined
是的,一切都一样 - 我没有收到任何错误信息。 - undefined
2个回答

3
我看到你的SQL语句中多了一个额外的),请尝试将其移除。
 ("DELETE FROM subscriptions WHERE user=:user AND title=:title)");

啊!我真是太笨了!就是这个! - undefined
这种情况发生在我们所有人身上。我花了几个小时试图修复某个问题,结果发现是一个括号、引号或者圆括号的问题。随着时间的推移,你会变得擅长发现它们。 - undefined

0

很难根据你的代码来判断,但是尝试将你的foreach语句包裹起来

try {
    foreach(...) {
        ...
    }
} catch (PDOException $ex) {
    $ex->getMessage();
}

这应该清楚地告诉你发生了什么。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接