我有一个登录脚本,我想将上次登录插入成员表并在每次成员登录时更新它,但我遇到了一些问题。每次用户登录时都不会插入 laSTLogin。这是我的代码
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>';
}
}
最佳答案
您的上述代码似乎有两个错误。
UPDATE
没有 where 子句,因此它将更新admin
表中所有行的login_date
。您通过在
$db
上调用prepare()
创建一个 PDOStatement 对象,但从不执行该语句。您需要在声明$stmt
之后调用$stmt->execute()
来执行该语句。
您的代码应该类似于:
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>';
}
}
关于php - 在mysql中插入上次登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45305203/