我的 php 从 SQL 查询中获取没有返回 ID



我的返回值$FoundAcc的登录函数

function Login($Username,$Password){
global $ConnectingDB;
$sql = "SELECT admin FROM admin WHERE admin=:admin 
AND password=:password LIMIT 1";
$stmt = $ConnectingDB->prepare($sql);
$stmt->bindValue(':admin',$Username);
$stmt->bindValue(':password',$Password);
$stmt->execute();
$Result = $stmt->rowcount();
if ($Result==1){
return $FoundAcc=$stmt->fetch();
}else{
return null;
}
}

我的PHP登录页面应该保存从我的DB提取行,并将其保存在一个会话变量(UserID),但它没有,但它确实保存(用户名)变量从行…怎么了?

if(isset($_POST["submit"])){
$Username=$_POST["username"];
$Password=$_POST["password"];
//if username/password are empty show error//
if(empty($Username)||empty($Password)){
$_SESSION["ErrorMessage"] = "All Fields Must Be Filled Out!";
Redirect_to("Login.php");
}else{
$FoundAcc=Login($Username,$Password);
// if account and pw found in DB complete following
if ($FoundAcc){
//save variables from DB row to session variables
$_SESSION["UserID"]=$FoundAcc["id"];
$_SESSION["Username"]=$FoundAcc["admin"];
//show toast
$_SESSION["SuccessMessage"] = $_SESSION["UserID"]." Has Successfully Logged In ";
Redirect_to("Login.php");
}else{
$_SESSION["ErrorMessage"] = "Incorrect Username or Password!";
Redirect_to("Login.php");
}
}
}

sql查询应该是这样的:

$sql = "SELECT * FROM admin WHERE admin=:admin AND password=:password LIMIT 1";

最新更新