donglaoe2979 2012-08-02 13:51
浏览 11
已采纳

每当页面加载时更新sql表

I want to make the online/offline status, i made a table user_status and i want to add in the header sql query so every time the user loads a page the table will be updated with that time;

user_status table is : user_id | last_activity_time ; user_id is from the table users;

i added this in the header:

//check update last_time_active
$date = date("Y-m-d H:i:s");

$query = mysql_query("update user_status SET last_activity_time=$date ");
?>

how can i add also the user? if he's logged in, check his user_id and update the table with the user_id and the last_activity_time ? is it correct?

after that i will want to add in specific pages a text next to the username (online/offline).

Thanks all!

  • 写回答

3条回答 默认 最新

  • drwkqwa82140 2012-08-02 13:57
    关注

    Store the userid in the user's $_SESSION when they log in. Then pull it and insert into your query.

    <?php
    //confirmed login, set $userid to the user's ID
    session_start();
    $_SESSION['userid'] = $userid;
    

    Then your query would look like this (UPDATED):

    // Since, according to your comment, $username = $_COOKIE['username']
    
    "UPDATE user_status SET last_activity_time = NOW() where user_id = (SELECT userid FROM users WHERE username='" . $_COOKIE['username'] . "')";
    

    However DO NOT use mysql_* functions anymore. They are being deprecated. Instead use PDO or mysqli. If you're not sure which one to use, read this article from SO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比