2017-02-27 111 views
0

所以我不能解决PHP导致这个问题的最新错误。正如你从代码中看到的那样,我使用if语句来回显。如果细节与查询相同,那么它将回显Test2。但为什么不重定向?我无法弄清楚什么是错的。PHP不会重定向

<?php 
include "../inc/db.php"; 
include "../inc/functions.php"; 

$tip = ""; 

if(isset($_POST["submit"])) { 
    $username = $_POST["username"]; 
    $password = $_POST["password"]; 

    $username = mysqli_real_escape_string($connection, $username); 
    $password = mysqli_real_escape_string($connection, $password); 

    $query = "SELECT * FROM users WHERE username = '{$username}' "; 
    $select_user_query = mysqli_query($connection, $query); 

    if(!$select_user_query) { 
     die("Query failed. Reason: " . mysqli_error($connection)); 
    } 

    while($row = mysqli_fetch_array($select_user_query)) { 
     $db_user_id = $row['id']; 
     $db_username = $row['username']; 
     $db_password = $row['password']; 
    } 

    if($username !== $db_username && $password !== $db_password) { 
     header("Location index.php"); 
     $tip = "Incorrect details!"; 
     echo "Test1"; 
    } elseif ($username == $db_username && $password == $db_password) { 
     header("Location "); 
     echo "Test2"; 
    } else { 
     header("Location index.php"); 
     echo "Test3"; 
    } 
} 

?> 

结果提交正确的细节: form01

,并证明dashboard.php确实存在 help02

,如果我进入这并不在MySQL数据库中存在,那么我得到这样的: help3

但我在while循环中定义了变量

+0

试图加入'出口();'重定向后。另外,如果你离开页面,为什么你会在重定向后回应一些东西?还有其他的东西,你的第一个if应该是*或*'||'而不是*和*'&&' – eeetee

+0

对于第三个图像(第二个问题?),这是因为当没有结果时,没有行要迭代这样就不会声明$ db_username和$ db_password。 –

+0

您在'header'上的'location'之后缺少冒号。你应该哈希你的密码。 – chris85

回答

0
header("Location dashboard.php"); 

分号失踪

header("Location: dashboard.php"); 

我傻......

+0

不要忘记在while语句之外声明您的用户名和密码变量,以避免在mysql没有匹配查询的情况下发生声明错误。 –

+0

这不会解释您包含的错误消息。那是不相干的?你可能会删除这个问题,因为它看起来更像是一个错字而不是编程问题。 – chris85