Php,MySQl删除查询错误查询数据库

时间:2016-03-16 18:27:37

标签: php mysql

我在删除mySQL和php中的代码时遇到问题。我无法删除mySql中的数据,当我尝试删除它时,我得到以下内容。

查询数据库时出错。You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''email_list' WHERE book=the deletiom' at line 1

我已经坚持了一段时间,有人可以帮助我弄清楚错误在哪里以及如何修复它的问题?我已经尝试将qoutes放在delete语句中的变量$ delete_id周围,还有表名,email_list。

谢谢。任何帮助将不胜感激。

 <body style="background-color:black; color:#00FF00;font-family:"Courier New", Courier, monospace";>
<h1> Book loan 9000</h1>

  <p>Please select the Entries to delete from the email list and click Remove.</p>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php
  $dbc = mysqli_connect('localhost', 'asubrama_user', 'user1234', 'asubrama_final')
    or die('Error connecting to MySQL server.');

  // Delete the customer rows (only if the form has been submitted)
  if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) {
      $query = "DELETE FROM 'email_list' WHERE book=" .$delete_id;
      mysqli_query($dbc, $query)
        or die('Error querying database.'.mysqli_error($dbc) );
    } 

    echo 'Customer(s) removed.</br>';

}
  // Display the customer rows with checkboxes for deleting
  $query = "SELECT * FROM email_list";
  $result = mysqli_query($dbc, $query);
  echo'<table border="1px" style="border: #00FF00 dotted 2px; Padding:10px;font-size:15px;">';
  echo"<tr><th>Select</th><th>First Name</th><th>Last Name</th><th>Book</th><th>Due Date</th><th>Email</th> </tr>";
  while ($row = mysqli_fetch_array($result)) {
    echo '<tr><td>  <input type="checkbox" value="' . $row['book'] . '" name="todelete[]" /> </td><td>'. $row['first_name'] . '</td><td>' . $row['last_name'] . '</td> <td>'. $row['book'] . '</td><td>'. $row['date'] . '</td> <td>'. $row['email'] . '</td></tr>';
    echo '<br />';
  }

  mysqli_close($dbc);
?>
    </table>
    <input type="submit" name="submit" value="Remove" />
  </form>
</body>
</html>

在这里输入代码

1 个答案:

答案 0 :(得分:3)

应使用反引号而非实际引号引用表和列。报价仅适用于价值观。

$query = "DELETE FROM `email_list` WHERE book=" .$delete_id;