PHP PDO的未知SQL语法错误

时间:2016-09-24 12:18:51

标签: php mysql 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]:语法错误或访问冲突:1064您有   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本为正确的语法使用附近       &#39;顺序(           用户身份,         departure_street,&#39;在第1行

我不明白语法可能有什么问题。我已多次使用此查询,但它始终有效。现在的问题是什么?我已尝试echo $sql变量来查看生成的查询的外观,但它并未向我显示正在插入的值。任何人都可以看到SQL语法错误吗?

1 个答案:

答案 0 :(得分:1)

ORDER是mysql关键字.it应该用反引号括起来。如果你的表名或列名是关键词,只需使用反引号 order 反引号``

here you can find the mysql keywords list