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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向