我的代码中出现意外的T_ELSE错误,有人可以看到吗?

时间:2012-07-13 17:39:37

标签: php mysql sql database

我找不到我的错误,我可以帮忙吗?我正在尝试使用cookie登录代码。先感谢您!我似乎无法看到我的错误。我希望有人能看到我失踪的东西。错误发生在第47行,但我知道这并不意味着它就在那里。

<?php
    if(isset($_POST['sent']) && $_POST['sent'] == "yes")
    {
        foreach($_POST as $field => $value)
        {
            if($value == "")
            {
                $blank_array[$field]= $value;
            }
            else
            {
                $good_data[$field]=strip_tags(trim($value));
            }
        }
    }

    if(@sizeof($blank_array) > 0)
    {
        $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'> Error.</p>";
        extract($blank_array);
        extract($good_data);
        include("form_log.php");
        exit();
    }

    include("dbstuff.php");
    $cxn = mysqli_connect($host,$user,$password,$database) or die ("coulnt connect");
    $query = "SELECT first_name FROM customer WHERE user_name='$_POST[user_name]' AND       password=md5('$_POST[password]')";
    $result = mysqli_query($cxn,$query) or die ("couldnt query");
    $n_row = mysqli_num_rows($result);

    if($n_row < 1)
    {
        $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'> Not found.     </p>";
        extract($_POST);
        include("form_log.php");
        exit();
    }
    else
    {
        $row=mysqli_fetch_assoc($result);
        setcookie("first_name",$row['first_name']);
        setcookie("auth","yes");
        header("Location: secret_page_cookie.php");
    }

    else
    {
        $user_name = "";
        $password = "";
        include("form_log.php");
    }
?>

很抱歉没有缩进但这很难缩进。第二个是if(@sizeof)..

3 个答案:

答案 0 :(得分:7)

if ($n_row < 1) {
    $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'> Not found.     </p>";
    extract($_POST);
    include("form_log.php");
    exit();
} else {
    $row=mysqli_fetch_assoc($result);
    setcookie("first_name",$row['first_name']);
    setcookie("auth","yes");
    header("Location: secret_page_cookie.php");
} else {
    $user_name = "";
    $password = "";
    include("form_log.php");
}

这里有两个else语句。您需要弄清楚它属于哪个if语句或将其转换为elseif条件。

答案 1 :(得分:4)

第47行:

else
{
$user_name = "";
$password = "";
include("form_log.php");
}

每个else只能有一个if这是第二个else

答案 2 :(得分:2)

你到底有两个其他的街区,应该只有一个。并且您的代码对SQL注入攻击持开放态度。不要通过串联构造SQL字符串,而是使用参数化查询。