PHP如果语句没有被触发

时间:2013-09-04 22:45:17

标签: php if-statement conditional

我目前正在为足球联赛建立一个系统。目前正在处理脚本文件以添加结果。大多数脚本都可以工作,结果总是成功添加到数据库中。但是,身份验证部分似乎失败了。第12行的if语句似乎没有触发,我无法理解为什么。

我的代码可以在这里的pastebin链接中找到:http://pastebin.com/ty4pdGgn

<?PHP

include 'functions.php';
dbConnect();

//$userEmail = mysql_real_escape_string($_POST["userEmailText"]);
$userCode = mysql_real_escape_string($_POST["userPasscodeText"]);

$authenticated = false;

$userEmail = "info@example.com";
if ($userEmail == "info@example.com") {
        header('Location: ../results.php?error=authentication');
}

$allUsers = mysql_query("SELECT * FROM accounts WHERE email = '$userEmail'");
while ($thisUser = mysql_fetch_assoc($allUsers)){
        if ($userCode != $thisUser['passCode']) {
                header('Location: ../results.php?error=authentication2');
        }
        echo $thisUser['passCode'];
        $authenticated = true;
        $userID = $thisUser['userID'];
}

if (!$authenticated) {
        header('Location: ../results.php?error=authentication3');
}

$dateSubmitted = $_POST['submissionDate'];
$homeTeam = $_POST['homeTeam'];
$awayTeam = $_POST['awayTeam'];
$homeGoals = $_POST['homeGoals'];
$awayGoals = $_POST['awayGoals'];

if ($homeTeam == $awayTeam) {
        header("Location: ../results.php?error=team");
}

if (getTeamLeague($homeTeam) != getTeamLeague($awayTeam)) {
        header("Location: ../results.php?error=league");
} else {
        $leagueID = getTeamLeague($homeTeam);
}

if ($homeGoals > $awayGoals) {
        $winnerID = $homeTeam;
} else if ($homeGoals < $awayGoals) {
        $winnerID = $awayTeam;
} else if ($homeGoals == $awayGoals) {
        $winnerID = -1;
}

$cQuery = mysql_query("INSERT INTO results VALUES ('', $userID, '$dateSubmitted', $leagueID, $homeTeam, $homeGoals, $awayTeam, $awayGoals, $winnerID, 0)");

if ($cQuery){
        header('Location: ../results.php');
} else {
                echo mysql_error();
}


?>

对此事的任何帮助将不胜感激。 functions.php不包含任何错误,因为这与数据库条目有关,而不是身份验证。

2 个答案:

答案 0 :(得分:1)

die();

之后加header("Location:...");

答案 1 :(得分:0)

由于您粘贴的比较代码(第12行的“if”部分)必须有效,我有两点建议:

  1. 放一个骰子();或退出();在header()部分之后。
  2. 尝试查看here,因为我不确定header()是否有效,而您设置的位置路径是相对的。基本建议是始终使用重定向的基本路径,例如“http://your.site.com/script.php”。