gpt4 book ai didi

php - 用户 ID 的 SQL 更新

转载 作者:行者123 更新时间:2023-11-28 23:53:22 33 4
gpt4 key购买 nike

我正在尝试更新表中的一个字段,以获取上次登录的 IP。

我所能做的就是在登录时创建一个新的用户行(仅限 ip 插入),但无法让它更新特定用户 ID 的字段。

登录函数:

    public function login($username,$email,$password,$lastLogIp){

try{
$stmt = $this->db->prepare("SELECT * FROM users WHERE username=:username OR email=:email LIMIT 1");
$stmt->execute(array(':username'=>$username, ':email'=>$email));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0){
if(password_verify($password, $userRow['password'])){
$_SESSION['session'] = $userRow['id'];
$stmt = $this->db->prepare("UPDATE users SET lastlogip=:lastlogip WHERE id=:id");
$stmt->bindParam(":lastlogip", $lastLogIp);
$stmt->bindParam(":id", $id);
$stmt->execute(array(":lastlogip"=>$lastLogIp, ":id"=>$id));
return true;
}else{
return false;
}
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}

登录页面:

<?php
ini_set('display_errors', '1');
require_once '../includes/conn.php';

if($user->is_loggedin()!=""){
$user->redirect('../admin/index.php');
}

if(isset($_POST['login'])){
$username = $_POST['username_email'];
$email = $_POST['username_email'];
$password = $_POST['password'];

if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARTDED_FOR'] != '') {
$lastLogIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$lastLogIp = $_SERVER['REMOTE_ADDR'];
}

if($user->login($username,$email,$password,$lastLogIp)){
$user->redirect('../admin/index.php');
}else{
$error = "Login details provided do not match out records.<br /><br />";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>EpicOwl UK | CMS Admin Panel Login</title>
<meta charset="utf-8">
<link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body>
<div id="header">
<a href="../index.php"><img id="logo" src="../images/logo.png" /></a>
<div id="navigation">
<img src="../images/home.png" class="nav" /><a href="../index.php" class="navigation">Home</a>
</div>
</div>
<div id="content">
<br /><br /><h1>Administrator Login</h1><br />
<div id="spacer1"></div>
<div id="spacer2"></div><br />
<form method="post"><br /><br />
<div id="newsborderouter">
<div id="newsborder">
<?php
if(isset($error)){
?>
<em><?php echo $error; ?></em>
<?php
}
?>
<input class="input" type="text" name="username_email" placeholder="Username/Email" autocomplete="off" /><br /><br />
<input class="input" type="password" name="password" placeholder="Password" autocomplete="off" /><br /><br />
<button class="login" type="submit" name="login">Login</button><br /><br /><br />
<label>Don't have an account?<br />Why not register one by clicking <a href="./register.php">HERE</a><br /><br /></label>
</form>
</div>
</div><br /><br /><br /><br />
</div>
</body>
</html>

最佳答案

确保您在 UPDATE 语句的 WHERE 子句中搜索的 ID 有效。当数据库没有命中 WHERE 子句时,不会更新任何内容。

因此,正如您在评论部分建议的那样,您可以使用 $userRow['id'] 而不是未定义的 $id(我假设)。

您是否还注意到您绑定(bind)了两次值?我认为这不会造成任何伤害,只是提到 c:

关于php - 用户 ID 的 SQL 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32202484/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com