2013-04-10 75 views
1

下面的代码是我如何从数据库中删除数据。用户选择他想删除的标题,然后查询应删除这些行。不幸的是,这是行不通的。MySQL DELETE语句不删除行

我已经确定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"); 
?> 
+0

是否有错误信息?假设有错误报告?你是否使用与工作台上相同的用户/权限连接到数据库? – michi 2013-04-10 22:43:02

+0

是的,一切都是一样的 - 即时获取没有错误消息 – JimRomeFan 2013-04-10 22:43:42

+0

错误报告? – michi 2013-04-10 22:44:29

回答

3

我在您的SQL中看到额外的),请尝试删除它。

("DELETE FROM subscriptions WHERE user=:user AND title=:title)"); 
+0

<拍拍额头>就是这样! – JimRomeFan 2013-04-10 22:45:23

+0

它发生在我们所有人身上。我花了几个小时试图解决某些问题,括号,引号或括号是问题所在。随着时间的推移,你会很好地发现它们。 – Rujikin 2013-04-10 22:47:14

0

很难告诉你的代码,但尝试

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

包裹你的foreach这应该明确地告诉你这是怎么回事。