我有以下脚本。
逻辑是它应该检查是否为每个sql查询返回任何结果,如果其中任何一个没有结果它应该触发最后的else子句,但是目前它没有。
我觉得逻辑上的某些东西是错的,但是无法弄清楚是什么。
<?php
// username and password sent from form
$MyUsername = $_POST['Username'];
$MyUsername = mysqli_real_escape_string($conn, $MyUsername);
$MyPassword = $_POST['Password'];
//check username in admin
$adminsql="SELECT * FROM Admin WHERE Email='$MyUsername'";
$adminresult=mysqli_query($conn, $adminsql);
echo mysqli_error($conn);
//check username in Companys
$companysql="SELECT * FROM Companys WHERE Email='$MyUsername'";
$companyresult=mysqli_query($conn, $companysql);
echo mysqli_error($conn);
//check username in drivers
$driversql="SELECT * FROM Drivers WHERE Email='$MyUsername'";
$driverresult=mysqli_query($conn, $driversql);
echo mysqli_error($conn);
//check username in recruitment
$recruitmentsql="SELECT * FROM Recruitment WHERE Email='$MyUsername'";
$recruitmentresult=mysqli_query($conn, $recruitmentsql);
echo mysqli_error($conn);
if ($adminresult){
//User found check password against hash
while($row = mysqli_fetch_array($adminresult)){
if(password_verify($MyPassword, $row['PassHash'])){
$_SESSION['user'] = $MyUsername;
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=admin.php">';
}else{
echo "Invalid login, please check you username and password and try again";
echo '<META HTTP-EQUIV="Refresh" Content="2; URL=index.php">';
}
}
}
if ($companyresult) {
//User found check password against hash
while($row = mysqli_fetch_array($companyresult)){
if(password_verify($MyPassword, $row['PassHash'])){
$_SESSION['user'] = $MyUsername;
$_SESSION['userid'] = $row['ID'];
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=company.php">';
}else{
echo "Invalid login, please check you username and password and try again";
echo '<META HTTP-EQUIV="Refresh" Content="2; URL=index.php">';
}
}
}
if ($driverresult) {
//User found check password against hash
while($row = mysqli_fetch_array($driverresult)){
if(password_verify($MyPassword, $row['PassHash'])){
$_SESSION['user'] = $MyUsername;
$_SESSION['userid'] = $row['ID'];
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=driver.php">';
}else{
echo "Invalid login, please check you username and password and try again";
echo '<META HTTP-EQUIV="Refresh" Content="2; URL=index.php">';
}
}
}
if ($recruitmentresult){
//User found check password against hash
while($row = mysqli_fetch_array($recruitmentresult)){
if(password_verify($MyPassword, $row['PassHash'])){
$_SESSION['user'] = $MyUsername;
$_SESSION['userid'] = $row['ID'];
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=recruitment.php">';
}else{
echo "Invalid login, please check you username and password and try again";
echo '<META HTTP-EQUIV="Refresh" Content="2; URL=index.php">';
}
}
}else{
echo "Invalid login, please check you username and password and try again";
echo '<META HTTP-EQUIV="Refresh" Content="2; URL=index.php">';
}
?>
答案 0 :(得分:0)
如果没有设置$ recruitmentresult,那么最后的其他内容只会触发,而不会设置任何内容。如果你得到一个带有无效密码的$ driverresult,它也可能会触发其他的$ recruitment。如果你想确保最后的else只触发,如果没有设置任何东西,将每个if更改为elseif(但保留第一个)。这是一个例子 - 我遗漏了其他代码来保持这个简短:
if ($adminresult){
} elseif ($companyresult) {
} elseif ($driverresult) {
} elseif ($recruitmentresult){
} else {
}
这可能不是唯一正在发生的事情,但这可能有助于解决问题的根源。
此外,在您进行测试时,您可以尝试更改元刷新代码以回显某些内容。使用//注释元刷新代码,以便稍后将其放回。这不是必需的,但我认为测试它并确定哪个刷新实际触发是非常困难的,因为在您有机会看到任何消息出现之前,刷新可能会让您离开当前页面。
答案 1 :(得分:0)
如果结果为空,mysqli_query
的结果不会返回false,只有在连接或查询中出现错误时才会返回false。
因此,如果之前没有工作,您需要检查mysql_num_rows($result)
或mysql_num_rows($result)>0
之类的内容
if (mysql_num_rows($adminresult)){ // you may have to add > 0
// do something
} elseif (mysql_num_rows($companyresult)) {
// do something
} elseif (mysql_num_rows($driverresult)) {
// do something
} elseif (mysql_num_rows($recruitmentresult)){
// do something
} else {
// do something else
}