0

我正在使用 POST 将 JSOn 发送到 PHP Web 服务

JSON:

{"mDel":"NULL","mType":"text","mUserId":4,"lLong":"(null)","mPrivacy":1,"lLat":"(null)","mDate":"2012-13-25 13:13:25","mHeight":"NULL","mDescription":"Test","mPhoneUniqueKey":"425062012131325","mMedia":"4_2_20121325011325.png","lName":"Apple Store, San Francisco"}

PHP:

$request = Slim::getInstance()->request();
$moment = json_decode($request->getBody());
$sql = "INSERT INTO mymo_moment (profile_id, description, media, locationName, lat, long, mDate, privacy, type, iPhoneUniqueID, deleted, height) 
        VALUES (:mUserId, :mDescription, :mMedia, :lName, :lLat, :lLong, :mDate, :mPrivacy, :mType, :mPhoneUniqueKey, :mDel, :mHeight)";
try {
    $db = getConnection();
    $stmt = $db->prepare($sql);
    $stmt->bindParam(":mUserId", $moment->mUserId);
    $stmt->bindParam(":mDescription", $moment->mDescription);
    $stmt->bindParam(":mMedia", $moment->mMedia);
    $stmt->bindParam(":lName", $moment->lName);
    $stmt->bindParam(":lLat", $moment->lLat);
    $stmt->bindParam(":lLong", $moment->lLong);
    $stmt->bindParam(":mDate", $moment->mDate);
    $stmt->bindParam(":mType", $moment->mType);
    $stmt->bindParam(":mPrivacy", $moment->mPrivacy);
    $stmt->bindParam(":mPhoneUniqueKey", $moment->mPhoneUniqueKey);
    $stmt->bindParam(":mDel", $moment->mDel);
    $stmt->bindParam(":mHeight", $moment->mHeight);
    $stmt->execute();

    echo($stmt);

    $moment->id = $db->lastInsertId();
    $db = null;
    echo json_encode($moment);
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}';
}

而且我收到以下错误,我已经看过这个,但我无法弄清楚我做错了什么,任何帮助都会很棒!

错误:

{"error":{"text":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 'long, mDate, privacy, type, iPhoneUniqueID, deleted, height) 
            VALUES ('4',' at line 1}}
4

1 回答 1

1

long, type- 根据您使用的 RDBMS,这些名称可能被视为关键字。更改列名或转义它们的名称,即在 MSSQL 类型中"long"而不是long在 MySQL 类型中{grave-accent}long{grave-accent}*

您也可以使用以下语法来准备语句和绑定参数:

$stmt = $db->prepare($sql);
$stmt->execute(array(
    ':mType'   => $moment->mType,
    ...
    ':mHeight' => $moment->mHeight
));
  • 重音是一个“`”字符。
于 2012-06-25T12:36:22.667 回答