2016-09-24 93 views
-1

我有這樣的代碼:未知的SQL語法錯誤PHP PDO

try { 

    $sql = "INSERT INTO order(
       user_id, 
       departure_street, 
       departure_housenr, 
       departure_postalcode, 
       departure_city, 
       arrival_street, 
       arrival_housenr, 
       arrival_postalcode, 
       arrival_city, 
       order_date, 
       lifter, 
       order_status 
      ) 
      VALUES(
       :user_id, 
       :departure_street, 
       :departure_housenr, 
       :departure_postalcode, 
       :departure_city, 
       :arrival_street, 
       :arrival_housenr, 
       :arrival_postalcode, 
       :arrival_city, 
       :order_date, 
       :lifter, 
       :order_status 
      )"; 

    $stmt = $dbh -> get_instance() -> prepare($sql); 

    $stmt -> execute(array(':user_id' => $_SESSION[ 'user_id' ], 
          ':departure_street' => $street1_parsed, 
          ':departure_housenr' => $streetnumber1, 
          ':departure_postalcode' => $postcode1, 
          ':departure_city' => $city1_parsed, 
          ':arrival_street' => $street2_parsed, 
          ':arrival_housenr' => $streetnumber2, 
          ':arrival_postalcode' => $postcode2, 
          ':arrival_city' => $city1_parsed, 
          ':order_date' => $datetime, 
          ':lifter' => $lifter, 
          ':order_status' => $order_status)); 

} 
catch(PDOException $e) { 

    echo $e -> getMessage() . '<br />' . $sql; 

} 

此代碼一直給我這個錯誤:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order( user_id, departure_street, ' at line 1

我不明白什麼可以是錯誤的語法。我已經使用了這個查詢很多次了,它總是有效的。現在有什麼問題?我試圖通過echo變量$sql來查看結果查詢的結果,但它並未顯示正在插入的值。任何人都可以看到SQL語法錯誤?

+4

訂單是保留字MySQL如此包裝內''(反引號) –

+0

你打印你的'查詢'??? – Karthi

+0

@AbhikChakraborty該死的,這是第二次一個保留的SQL關鍵字把我搞砸了。謝謝:) –

回答

1

ORDER是MySQL的關鍵字。它應該由反引號。如果你的表名或列名括起來的關鍵字只是使用反引號order反引號``

here you can find the mysql keywords list

+0

謝謝我會在8分鐘內接受你的回答(SO邏輯):/ –