将登录时间存储在 mysql 数据库中 [英] Storing the login time in a mysql database
问题描述
我正在尝试将登录时间存储在 mysql 数据库中.我想要的是..当用户登录时..脚本应该将当前时间存储在具有last_seen"列的user"表中.last_seen 是 DATETIME 数据类型.
I'm trying to store login time in a mysql database. What I want is..that when a user logs in..the script should store the current time in the table "user" having a column "last_seen". last_seen is of DATETIME datatype.
这是我写的代码(它是登录脚本的一小部分......它在用户输入用户名和密码后工作,并且它们在数据库中匹配.:-
Here's the code I've written(it's a little part of a login script..it works after the user has enter the username and password, and they are matched in the database. :-
$_SESSION['username']=$username;
$_SESSION['logged']=1;
$date = date('Y-m-d H:i:s');
$query = "INSERT INTO user
(last_seen)
VALUES
('" . $date . "')
WHERE user_name = '" . $username . "'";
mysql_query($query, $db) or die (mysql_error($db));
header('Refresh: 5; URL = main.php');
echo "Login successful, redirecting...";
die();
这给我带来了错误:-您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 5 行的WHERE user_name = 'test'"附近使用的正确语法(我登录的帐户的用户名是:- test)
This throws me the error :- 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 'WHERE user_name = 'test'' at line 5 (The username of the account from which I'm logging in is :- test)
我要更改什么才能使代码有效?代码将用户的登录时间存储在数据库中(稍后将在后端页面访问).
What do I change so the code works ?The code would store the login time of the user in the db(which would be later accessed at a backend page).
推荐答案
不要在 PHP 中进行日期生成.拥有更容易
Don't do the date generation in PHP. It's much easier to just have
UPDATE .... SET last_seen=now() WHERE ...
^^^^^
在mysql中.
除此之外,INSERT
绝不接受 WHERE
子句.
Beyond that, an INSERT
in no way shape or form accepts a WHERE
clause.
这篇关于将登录时间存储在 mysql 数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!