2 回答

TA贡献1852条经验 获得超1个赞
我相信你想检查单个用户。
“我想检查用户登录条目数据以查找它们是否存在。” - 好吧,我不熟悉您的应用程序逻辑,但要检查是否存在某些用户,您只需从 db 中获取一些参数。如果存在则获取单个用户,否则获取空行。对?
“我知道如果我想用数据库中的数据检查用户登录条目数据,我应该使用 while 循环。” - 如何?谁告诉你的。如果您获取其中的许多并对数据做一些事情,例如,您需要这样的循环。列出他们?
如果您确实有某些情况,您必须获取所有用户并检查您上面在代码中编写的内容,请不要重定向到那里,请将一些数据保存在数组中,例如 $array['passed'] 和 $array['failed'] 然后如果需要,编写另一个逻辑来重定向。
显然,在循环内多次重定向是不可能的。

TA贡献1803条经验 获得超6个赞
$select_query = "select `username`,`pass` from `register`"; // Fetching ALL user accounts from register table
$result= $db_connection->query($select_query); // Execute query
// Loop through all rows of query
while($row= $result->fetch_assoc()){
// If username and password matches the user's input, then set HTTP Response header to do a 302 Redirect to ../account.php
if($row["username"] === $username && $row["pass"] === $pass){
header("location:../account.php");
}
// If username and password doesn't match the user's input, then set HTTP Response header to do a 302 Redirect to ../login.php
else{
header("location:../login.php");
}
}
该代码有一个逻辑错误,让我们假设您的“注册表”是这样的:
username | pass
--------------------------
user1 | password1
user2 | password2
user3 | password3
user4 | password4
如果用户输入正确 $username="user3" $pass="password3" ,代码仍然会输出 header("location:../login.php") 因为它是从第一行一直循环到最后表(即当循环到达最后一行“user4”时,它将处理“else”语句)。
最好将您的代码更新为这样的内容(假设您使用的是 php mysqli 类):
session_start();
$select_query = " select `pass` from `register` where `username` = '".$db_connection->real_escape_string($username)."' ";
$result = $db_connection->query($select_query); // Execute query
$redirectPage = '../login.php';
if( $result->num_rows == 1 ) {
$dbPass = $result->fetch_object();
if( $pass == $dbPass->pass ) {
$redirectPage = '../account.php';
$_SESSION['loginSuccess'] = true; // This let's account.php know that the user is properly authenticated
}
}
header('Location: '.$redirectPage);
- 2 回答
- 0 关注
- 129 浏览
添加回答
举报