0

我的查询是:

   $select_userid_query = execute_pdo_query("SELECT user_id FROM users WHERE username='" . $_COOKIE['username'] . "'");

   if ($select_userid_query == 0){

    execute_pdo_query("INSERT INTO user_status
    (user_id,last_activity_time) VALUES
    ((SELECT user_id FROM users WHERE username='" . $_COOKIE['username'] . "'),NOW())"
     , array($user_id, $last_activity_time));

   }
    else{
        execute_pdo_query("UPDATE user_status SET user_id = (SELECT user_id FROM users WHERE username='" . $_COOKIE['username'] . "'),last_activity_time= NOW()", array($user_id, $last_activity_time));
        }

我想验证数据库中是否有记录,如果没有插入,是否只是用新的更新时间NOW()

任何想法我的语法错误在哪里?

4

2 回答 2

2

如果user_id是,PRIMARY KEY您可以使用以下内容:

INSERT INTO ...... ON DUPLICATE KEY UPDATE last_activity_time=NOW()
于 2012-08-06T11:28:35.747 回答
2

如果你有一个唯一的,你可以使用这个单一的查询而不是这个user_id

INSERT INTO user_status
(
 user_id, last_activity_time
)
(
    SELECT user_id, NOW()
    FROM users
    WHERE username='" . $_COOKIE['username'] . "'"
) ON DUPLICATE KEY UPDATE last_activity_time = NOW();
于 2012-08-06T11:31:04.823 回答