gpt4 book ai didi

PHP登录if/else条件不执行else条件

转载 作者:行者123 更新时间:2023-11-29 02:57:16 25 4
gpt4 key购买 nike

我有一个小脚本,它从我的客户端获取用户名和密码,如果他们的凭据正确,他们将被带到我的开发服务器上的站点目录。

if 条件完美运行,并响应与我的 MySQL 数据库中对应部分匹配的正确凭据。

但是,else 条件根本不会执行(即,如果用户的凭据不正确,它不会将用户定向到错误页面)。我认为 while 循环可能会破坏逻辑,但我需要 $row 变量从数据库中获取目录字段。

有什么想法吗?

$username = stripslashes($_POST['username']);
$password = stripslashes($_POST['password']);

$pass = md5($password);

$conn = mysqli_connect("localhost", "root", "", "digital_server");
$sql = "SELECT * FROM projects WHERE username = '$username' AND password = '$pass'";

$query = mysqli_query($conn, $sql);

while($row = mysqli_fetch_assoc($query)) {

if($query) {
header("Location: " . $row['directory'] . "/");
}

else {
header("Location: http://dev.myserver.com/error.php");
}
}

已解决非常感谢所有帮助过的人。

修改后的代码块删除了 while 并创建了一个 $row 变量来获取我需要的数据用于 if 逻辑

    $row = mysqli_fetch_assoc($query);

if(mysqli_num_rows($query) > 0) {
header("Location: " . $row['directory'] . "/");
}

else {
header("Location: http://dev.myserver.com/error.php");
}

最佳答案

// No need of while here
//while() {
if( mysqli_num_rows($query) > 0 ) {
$row = mysqli_fetch_assoc($query);
header("Location: " . $row['directory'] . "/");
} else {
header("Location: http://dev.myserver.com/error.php");
}

选项 2

$sql = "SELECT * FROM projects WHERE username = '$username' AND password = '$pass'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($query);

if(count($row ) > 0 ) {
header("Location: " . $row['directory'] . "/");
} else {
header("Location: http://dev.myserver.com/error.php");
}

关于PHP登录if/else条件不执行else条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29367281/

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