【发布时间】:2013-09-04 14:52:44
【问题描述】:
我的代码出现此错误“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在 'condition,location,authorname) VALUES ('' 附近使用的正确语法,'','','','','','','')' 在第 1 行"。我是新手,我想知道是否有人可以帮助我找出问题所在?代码:
<?PHP
include_once('header.php');
include_once('create.php');
$isbn=$_POST['isbn'];
$title=$_POST['title'];
$publisher=$_POST['publisher'];
$genre=$_POST['genre'];
$availability=$_POST['availability'];
$condition=$_POST['condition'];
$location=$_POST['location'];
$authorname=$_POST['authorname'];
$queryuser=mysql_query("SELECT * FROM book
WHERE Title='$title' ");
$checktitle=mysql_num_rows($queryuser);
if($checktitle != 0){
echo "Sorry ".$title." is already added.";
}
else {
$insert_book=mysql_query("INSERT INTO book (isbn,title,publisher,genre,availability,condition,location,authorname) VALUES ('$isbn','$title','$publisher','$genre','$availability','$condition','$location','$authorname')");
if($insert_book)
{ echo "<b>Addition successful.</b><br><b>You Added: </b>".$title."<br><b>By:
</b>".$authorname ; }
else{
echo "error in registration".mysql_error();
}
}
提前感谢您的帮助。
【问题讨论】:
-
我经常看到这种类型的问题,你应该阅读common database debugging for PHP and MySQL。
-
PSA:
mysql_*函数是 deprecated in PHP 5.5。不建议编写新代码,因为它会阻止您将来升级。相反,请使用 MySQLi 或 PDO 和 be a better PHP Developer。 -
我总是觉得输出整个 SQL 字符串并尝试在 MySql Workbench 中使用它很有用。
-
condition是保留字 -
如果任何值包含单引号或双引号,则在 php 中打印您的查询。使用 mysql_real_escape_string() 函数
标签: php mysql sql sql-server syntax