douyong1850 2013-11-04 03:03
浏览 118
已采纳

MySQL - 更新数据库中的登录时间

I am currently working on a project where I need to store information in a database for member login page. Currently, I am working on storing the time the user enters their credentials. It keeps popping up with "Duplicate entry '0000-00-00 00:00:00' for key 'PRIMARY'", which I understand everything except for why my time will not process as an actual time... Below the section of my code that I am working with... maybe something is wrong? Or is there a better way to call the current timestamp than what I am doing.

    $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE)
or die("Failed to connect");
if(isset($_POST['submit'])) {
    date_default_timezone_set('America/Phoenix');
    $username = mysql_real_escape_string($_POST['userlogin']); 
    $password = mysql_real_escape_string($_POST['userpassword']);
    $today = date("F j, Y, h:i:s A"); 
    $sql = "SELECT * FROM Member WHERE loginName = '".$username."' AND password = '".$password."'"; //checks for user in database
    //--password = SHA2(CONCAT('bar','paz'),512)";
    $result = $mysqli->query($sql) or die($mysqli->error); //shoots an error if something is wrong
    if($result->num_rows < 1) // if the username/password is incorrect then an error message appears
       {
        echo "Invalid username/password combination. Please try again.";
       }
    else
       {
        echo "Welcome."; //Successful
        $today = date("F j, Y, h:i:s A"); 
        header('Location: thankspage.html');
     $sql = " INSERT INTO Login (loginName,loginTime) 
       VALUES ('$username', '$today' ) "; // creates the login time.
    $result = $mysqli->query($sql) or die ($mysqli->error); // shoots an error if i did something wrong.
   }
}   
  • 写回答

2条回答 默认 最新

  • dongping4461 2013-11-04 04:05
    关注

    Either change the datetime format

    $today = date("F j, Y, h:i:s A");
    

    to a proper one

    $today = date("Y-m-d H:i:s");
    

    Here is Codepad demo

    or ditch $today altogether and use NOW() on MySQL side (if the timezone setting is the same as on the web server)

    INSERT INTO Login (loginName, loginTime) VALUES ('$username', NOW())
                                                                  ^^^^^
    

    On a side note: since you already using mysqli consider to learn and use prepared statements instead of interpolating query strings, which leaves you wide open to sql injections.

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

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊