PHP脚本给出了意想不到的$ end

时间:2013-05-23 12:28:25

标签: php html login dreamweaver

我的登录脚本出现了一个奇怪的错误。虽然通过研究我无法确定问题。我想知道是否有人可以帮助我谢谢。这是错误消息,

解析错误:语法错误,第43行意外$结束。

<?php
    session_start();

    if(!isset($_SESSION["user_id"])){
        header("location:../../login.html");
    }


?>
<?php
    $username = $_POST['txt_username'];
    $password = $_POST['txt_password'];
    if ($username&&$password){

        $connect = mysql_connect("server", "username", "password") or die("No Server Found");

        mysql_select_db("databse") or die("No Connection");

        $query = mysql_query("SELECT * FROM account WHERE username='$username'");
        $numrows = mysql_num_rows($query); 

        if($numrows !=0){
            while ($rows = mysql_fetch_assoc($query)){

                $dbusername = $row['username'];
                $dbpassword = $row['password'];
            }
            if ($username == $dbusername && $password == dbpassword) {
                echo "Login Successful. <a href='homepage.html'>Click Here to go to the home page</a>";
                $_SESSION['username'] = $dbusername;
            } elseif ($username == '' || $password == '') {

                die("Please enter a username and password");

            } elseif (empty($dbusername)) {

                die("This account does not exsist");

            } else {
                die("Please enter a username and password");
            }
        }

?>

3 个答案:

答案 0 :(得分:2)

你需要在最后一行使用右括号来关闭这个if语句:

if ($username&&$password){

一个不错的IDE应该选择这个。

您在dbpassword第28行和第2行上缺少$需要清理$username

答案 1 :(得分:1)

如果条件不正确,你会错过最顶端的结束括号。你的代码应该是这样的:

<?php
    session_start();

    if(!isset($_SESSION["user_id"])){
        header("location:../../login.html");
    }


?>
<?php
    $username = $_POST['txt_username'];
    $password = $_POST['txt_password'];
if ($username&&$password){

    $connect = mysql_connect("server", "username", "password") or die("No Server Found");

    mysql_select_db("databse") or die("No Connection");

    $query = mysql_query("SELECT * FROM account WHERE username='$username'");
    $numrows = mysql_num_rows($query); 

    if($numrows !=0){
        while ($rows = mysql_fetch_assoc($query)){

            $dbusername = $row['username'];
            $dbpassword = $row['password'];
        }
        if ($username == $dbusername && $password == dbpassword) {
        echo "Login Successful. <a href='homepage.html'>Click Here to go to the home page</a>";
        $_SESSION['username'] = $dbusername;
             } elseif ($username == '' || $password == '') {

             die("Please enter a username and password");

             } elseif (empty($dbusername)) {

               die("This account does not exsist");

             } else {
                die("Please enter a username and password");
        }
    }
}

?>

答案 2 :(得分:0)

你没有关闭其中一个if语句。

if ($username&&$password){