douleijiang8111 2013-04-10 22:41
浏览 236
已采纳

MySQL DELETE语句不删除行

The code below is how I am removing data from my database. The user selects titles he wants to remove and then the query should delete those rows. Unfortunately, that isn't working.

I have made sure the DELETE query works by using it in PhpMyAdmin and MySQL Workbench, but for some reason the PHP code I'm coding is not allowing the deletes to work.

<?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");
?>
  • 写回答

2条回答 默认 最新

  • dqqpf32897 2013-04-10 22:44
    关注

    I do see an extra ) in your SQL, try removing it.

     ("DELETE FROM subscriptions WHERE user=:user AND title=:title)");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度