登录页面不会重定向到主页

时间:2015-10-17 20:41:47

标签: php mysql sql

我有以下脚本:

//Possible solution for logging a user in below. 
if($result = mysqli_query($db,"SELECT password FROM USERS WHERE password = '". $password ."' AND username = '". $username ."'")){
    $count = $result->num_rows;
    $accounttype = mysqli_query($db, "SELECT AccountType FROM USERS WHERE username ='". $username."'");
    if($count>=1){
        if($accounttype == "tradesmen"){
            include('../html/WelcomeCustomer.html');
        }
        else if($accounttype == "customer"){
            include('../html/WelcomeTrade.html');
        }
    }
    else{
        echo "The login credentials were incorrect";
    }
}

我知道这个实现在安全性方面存在一些问题。我计划在登录工作后修复此问题。

现在我只想让用户登录。当我运行这个时,我得到一个空白页面,使用以下帐户:

username, password, AccountType: test, test, tradesmen. 
username, password, AccountType: work, work, customer.

我已启用错误报告,但我什么都没得到。脚本不能重定向用户的原因是什么?

1 个答案:

答案 0 :(得分:0)

好吧,你不是redirecting控件。此声明include('../html/WelcomeCustomer.html');只会include HTML个文件,而不是redirecting

所以你需要做以下事情。你也不需要双重查询数据库

$mysqli = new mysqli("localhost", "my_user", "my_password", "db_name");
$result = $mysqli->query($db,"SELECT * FROM USERS WHERE password = '". $password ."' AND username = '". $username ."'");
$count = $result->num_rows;
if($count){
    $row = $result->fetch_assoc();
    if($row['AccountType'] == "tradesmen"){
        header('Location: ../html/WelcomeCustomer.html');
    }
    else if($row['AccountType'] == "customer"){
        header('Location: ../html/WelcomeTrade.html');
    }
}
else{
    echo "The login credentials were incorrect";
}

}

只需更改include语句即可 我发现您直接匹配object内部if条件,您需要先获得fetch个数据,然后再进行比较。

重要

在某些地方,您使用 面向对象的风格 ,例如 $ result-> num_rows; 并且在某些地方您使用 程序样式 ,例如 mysqli_query 。所以我建议你使用其中一个,我用 面向对象的风格

更新了我的答案