我正在尝试从Workbench中的表中删除一些数据,但是当我按下删除按钮时出现此错误;
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近' Name =' Stephenie Meyer'和Year_of_birth =' 1985''在第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";
}
你能帮帮忙吗?
答案 0 :(得分:3)
您有几个语法错误。
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查询的更现代的方法。它被弃用了。查看文档:{{3}}
答案 1 :(得分:2)
您需要在所有WHERE
条件之间设置逻辑运算符。
SELECT*from books WHERE AuthorID='$author' Name='$name' ...
应该是
SELECT * FROM books WHERE AuthorID='$author' AND Name='$name' ...
以及
DELETE from books WHERE AuthorID='$author' , Name='$name' ...
应该是
DELETE FROM books WHERE AuthorID='$author' AND Name='$name' ...
很明显,不推荐使用mysql_
个函数。您应该使用MySQLi或PDO。另外,您的查询对SQL注入是开放的;阅读http://php.net/manual/en/function.mysql-query.php