【问题标题】:Error help "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use"错误帮助“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册以获取正确的语法使用”
【发布时间】: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。不建议编写新代码,因为它会阻止您将来升级。相反,请使用 MySQLiPDObe a better PHP Developer
  • 我总是觉得输出整个 SQL 字符串并尝试在 MySql Workbench 中使用它很有用。
  • condition 是保留字
  • 如果任何值包含单引号或双引号,则在 php 中打印您的查询。使用 mysql_real_escape_string() 函数

标签: php mysql sql sql-server syntax


【解决方案1】:

“条件”是 SQL 中的关键字。

试试这个:

INSERT INTO book (`isbn`,`title`,`publisher`,`genre`,`availability`,`condition`,`location`,`authorname`) VALUES ('$isbn','$title','$出版商','$genre','$availability','$condition','$location','$authorname')

【讨论】:

    【解决方案2】:

    条件 不允许作为列名。尝试将该 mysql 列重命名为其他名称。

    【讨论】:

      【解决方案3】:

      这可能是由于您插入的 ANY 变量带有单引号 (')。要解决此问题,您需要清理从用户那里获取的变量针对 SQL 注入的输入。您还需要先验证表单。请继续阅读How to prevent SQL Injection?

      另外请注意,所有 mysql_* 函数现在都已弃用。请考虑使用 PDO 或 Mysqli

      【讨论】:

        猜你喜欢
        • 2022-01-14
        • 2020-02-05
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 2015-09-27
        • 2021-11-18
        • 1970-01-01
        • 2017-06-01
        相关资源
        最近更新 更多