问题描述
我有一个登录脚本,我想在成员表中插入最后一个登录名,并在每次成员登录时进行更新,但是我遇到了一些问题.用户每次登录时都不会插入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">×</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.
-
UPDATE
没有where子句,因此它将为admin
表中的所有行更新login_date
.
The
UPDATE
has no where clause so it will updatelogin_date
for all rows in theadmin
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">×</a>
<strong>Error!</strong>
Wrong username or password or your account has not been activated.
</div>';
}
}
这篇关于在mysql中插入上次登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!