mySQL语法中的错误

时间:2013-12-15 09:54:39

标签: php mysql

这个SQL查询出了什么问题?!

if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $author = $_POST['author'];
    $pub = $_POST['pub'];
    $sibn = $_POST['sibn'];
    $year = $_POST['year'];
    $version = $_POST['version'];
    $desc = $_POST['desc'];

    $selected_db = mysql_select_db("bookstore",$con);
    $query = "INSERT INTO introducebook (name, author, pub, sibn, year, version, desc) VALUES ('{$name}', '{$author}', '{$pub}', '{$sibn}', {$year}, {$version}, '{$desc}');" ;
    $result = mysql_query($query,$con);
    if(!$result){die('could not perform query'.mysql_error());}
    echo mysql_affected_rows();
}
?>

错误是(我通过所有输入测试):

  

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在第1行使用“desc”VALUES('test','test','test','test',test,test,'test')附近的正确语法

2 个答案:

答案 0 :(得分:2)

您需要使用反引号{/ 1>来reserved words in MySQL转义desc

INSERT INTO introducebook (name, ..., `desc`) VALUES ...

答案 1 :(得分:0)

传递纯文本你需要括号,这是正确的

INSERT INTO introducebook (`name`, `author`, `pub`, `sibn`, `year`, `version`, `desc`) VALUES ('{$name}', '{$author}', '{$pub}', '{$sibn}', '{$year}', '{$version}', '{$desc}')

是的,你还需要反叛。