duanjing4667 2013-07-31 16:31
浏览 144
已采纳

将登录时间存储在mysql数据库中

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();

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).

  • 写回答

2条回答 默认 最新

  • douyao1994 2013-07-31 16:36
    关注

    Don't do the date generation in PHP. It's much easier to just have

    UPDATE .... SET last_seen=now() WHERE ...
                              ^^^^^
    

    in mysql.

    Beyond that, an INSERT in no way shape or form accepts a WHERE clause.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)