PHP语句重定向到"else"不管用户输入


PHP statement redirecting to "else" regardless of user input

我试图使一个简单的登录脚本与煎饼工作。(网上发票)

无论你在表单中输入什么,它都会重定向到"else"语句。而不是尝试重定向到成功页面。

<?php
$con=mysqli_connect("example.com","example","example","example");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM mypanda_clients
                              WHERE unique_id='$_POST[username]'");
while ($row = mysqli_fetch_array($result))
{
    if ($_POST[password] == $ROW[passphrase])
    {
        header('Location: http://www.green-panda.com/my_panda/Clients/'.$_POST[username]); // concatenate the string
    }
    else
    {
        echo "The username and or password was incorrect. Please try again."; // missing semi colon
    }
}
?>

变量是$row,而不是$ROW,您需要在索引周围加上引号

if ($_POST[password] == $ROW[passphrase])

if( $_POST[ 'password' ] == $row[ 'passphrase' ] )

我还建议使用mysqli_fetch_assoc而不是mysqli_fetch_array

变量$ROW没有定义。

if ($_POST[password] == $ROW[passphrase])

if ($_POST[password] == $row[passphrase])

此外,如果你在数组键周围使用引号,PHP会更快地处理你的语句:

if ($_POST['password'] == $row['passphrase'])

这是因为php解释你代码的方式。它必须到达文本"passphrase"的末尾,才能意识到这不是一个基于整数的索引。然后,它尝试在关联数组中查找它作为键。这意味着它在成功之前有效地尝试查找$row['passphrase']两次。

编辑:

我在这里有点偏离-我添加了菲利普的评论来澄清为什么这是错误的:

*"实际上更糟。如果打开了E_NOTICE,它会警告您正在将未知常量转换为字符串值。这是错误的,PHP只是试图帮助"*