2013-12-15 92 views
0

這個SQL查詢有什麼問題?mySQL語法錯誤

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

你需要逃避reserved words in MySQLdesc與反引號

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

是的,這是正確的...非常感謝你... –

0

傳給你需要括號純文本,這將是正確的

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

,是的,你還需要反引號。