我有一个登录脚本,我想插入最后一次登录到成员表中,并在每次成员登录时更新它,但我有一些问题。并非每次用户登录时都会插入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() where adminID = $adminID ");
$stmt->execute();
$stmt=null;
}
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>';
}
}发布于 2017-07-25 23:25:13
在SQL查询字符串中,您没有提供adminID变量,而是获取一个adminID等于字符串值"$AdminID“的用户,使用PDO将变量放入SQL查询中的正确方法是使用带有引用变量的标记的prepare,然后给出一个将每个标记与一个PHP变量相关联的数组,如下所示:
$stmt = $db->prepare("UPDATE admin SET login_date = now() where adminID = :adminID");
$stmt ->execute(array(':adminID' => $adminID));通过这种方式,您可以保护您的站点免受SQL注入。
https://stackoverflow.com/questions/45307391
复制相似问题