每当页面加载时更新 sql 表 [英] Update sql table whenever the page loads
问题描述
我想制作在线/离线状态,我制作了一个表 user_status
并且我想添加标题 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 表是:user_id |last_activity_time ;user_id
来自表 users
;
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 ");
?>
我怎样才能添加用户?如果他已登录,请检查他的 user_id
并使用 user_id
和 last_activity_time
更新表?正确吗?
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).
谢谢大家!
推荐答案
在用户登录时将用户 ID 存储在用户的 $_SESSION
中.然后将其拉出并插入到您的查询中.
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'] . "')";
但是不要再使用mysql_*
函数.它们正在被弃用.而是使用 PDO 或 mysqli.如果您不确定使用哪个,请阅读这篇文章来自 SO.
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.
这篇关于每当页面加载时更新 sql 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!