在mysql中插入上次登录 [英] inserting last login in mysql

查看:68
本文介绍了在mysql中插入上次登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录脚本,我想在成员表中插入最后一个登录名,并在每次成员登录时进行更新,但是我遇到了一些问题.用户每次登录时都不会插入lastlogin.这是我的代码

I have a login script and I want to insert last login into members table and update it everytime a member logins in but I am having some issues. The lastlogin is not being inserted everytime a user logins in. Here is my code

if(isset($_POST['submit'])){

    $username = $_POST['username'];
    $password = $_POST['password'];

    if($user->login($username,$password)){
        $_SESSION['username'] = $username;

        try{
            $stmt = $db->prepare('UPDATE admin  SET login_date = now()');
        }

        catch (PDOExeception $e){
            $error[] = $e->getMessage();
        }
        header('Location: index.php');
        exit;
    } 
  else {      
           $error[] =              
                      '<div class="alert alert-danger fade in text-center">
                           <a href="#" class="close" data-dismiss="alert">&times;</a>
                           <strong>Error!</strong> 
                            Wrong username or password or your account has not been activated.
                      </div>';
    }

}

推荐答案

您的上述代码似乎有两个错误.

There appears to be two errors with your above code.

  1. UPDATE没有where子句,因此它将为admin表中的所有行更新login_date.

  1. The UPDATE has no where clause so it will update login_date for all rows in the admin table.

您可以通过在$db上调用prepare()来创建PDOStatement对象,但是从不执行该语句.声明$stmt后,需要通过调用$stmt->execute()执行该语句.

You create a PDOStatement object by calling prepare() on $db but never execute the statement. You need to execute the statement by calling $stmt->execute() after you declare $stmt.

您的代码应类似于:

if(isset($_POST['submit'])){

    $username = $_POST['username'];
    $password = $_POST['password'];

    if($user->login($username,$password)){
        $_SESSION['username'] = $username;

        try{
            $stmt = $db->prepare('UPDATE admin  SET login_date = now() WHERE x = y');
            $stmt->execute();
        }

        catch (PDOExeception $e){
            $error[] = $e->getMessage();
        }
        header('Location: index.php');
        exit;
    } else {      
           $error[] =              
                      '<div class="alert alert-danger fade in text-center">
                           <a href="#" class="close" data-dismiss="alert">&times;</a>
                           <strong>Error!</strong> 
                            Wrong username or password or your account has not been activated.
                      </div>';
    }

}

这篇关于在mysql中插入上次登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆