我试图从 Workbench 中的表中删除一些数据,但是当我按下删除按钮时,我得到了这个错误;
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 'Name='Stephenie Meyer' and Year_of_birth='1985'' at line 1.
这是代码:
$author=$_POST['AuthorID'];
$name=$_POST['Name'];
$year=$_POST['Year_of_birth'];
$author=htmlspecialchars($author);
$name=htmlspecialchars($name);
$year=htmlspecialchars($year);
if($db_found)
{
$SQL="SELECT*from books WHERE AuthorID='$author' Name='$name' and Year_of_birth='$year'";
$result=mysql_query($SQL) or die(mysql_error());
while($row=mysql_fetch_assoc($result))
{
echo "<tr><td>".$row["AuthorID"]."</td><td>".$row["Name"]."</td><td>".$row["Year_of_birth"]."</td></tr>" ;
}
echo "</table><br>";
$DQL="DELETE from books WHERE AuthorID='$author' , Name='$name' and Year_of_birth='$year'";
if ($db_handle->query($DQL) === TRUE)
{
echo "Record(s) deleted successfully";
}
else
{
echo "Error deleting record: " . $db_handle->error;
}
}
else
{
echo"Record not found";
}
你能帮忙吗?
最佳答案
你有几个语法错误。
- 您遗漏了一个
AND
- 添加一些空格
改变:
$SQL="SELECT*from books WHERE AuthorID='$author' Name='$name' and Year_of_birth='$year'";
到
$SQL="SELECT * FROM books WHERE AuthorID='$author' AND Name='$name' AND Year_of_birth='$year'";
另外,请研究用 PHP 而不是 mysql_query
执行 MySQL 查询的更现代的方法。它已被弃用。查看文档:http://php.net/manual/en/function.mysql-query.php
关于mysql - 尝试删除数据时查询出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34687950/