pdo exec更新语句出错?

时间:2013-11-17 10:43:39

标签: mysql pdo

获取以下错误 ...以非pdo 格式运行良好:

  

解析错误:语法错误,意外''(T_ENCAPSED_AND_WHITESPACE),期待标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)

// update user's current ip and current login date in db (prepared not required as no user input)
$db->exec("UPDATE test_users SET currIP = $_SERVER['REMOTE_ADDR'], dtCurrLogin = UTC_TIMESTAMP() WHERE user = $user");

1 个答案:

答案 0 :(得分:1)

将数组索引调用更改为:

$db->exec("UPDATE test_users 
    SET currIP = '" . $_SERVER['REMOTE_ADDR'] . "', 
        dtCurrLogin = UTC_TIMESTAMP() 
    WHERE user = $user");

我仍然建议使用prepare来电。

$stm = $db->prepare("UPDATE test_users 
        SET currIP = :ip, 
            dtCurrLogin = UTC_TIMESTAMP() 
        WHERE user = :usr");
$stm->bindParam( ':ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR );
$stm->bindParam( ':usr', $user, PDO::PARAM_INT );
$stm->execute();