简体   繁体   中英

sqlserver error for connection

Ok so this my Script to attempt to connect to a sqlserver to log in using credentials previously stored in the database but when i try to access the page i am given the error

"Parse error: syntax error, unexpected T_ELSE in login.php on line 32"

i have looked up on various websites and cant seem to find out what is wrong with this script

Thanks in Advance.

  <?php
        session_start();

        if(!isset($_SESSION["user_id"])){
            header("location");
        }

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

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

        mysql_select_db("hnd_1213_marwick") 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.php'>Click Here to go to the home page</a>";
            $_SESSION['username']=$dbusername;

            else
            echo "Incorrect Login";


        else
        die ("This account does not exsist");

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

    ?>

The error you got isn't related to sql, it's about php syntax.

In order to prevent those kind of errors , consider using {} for your if and else statements as well. Also make sure you have indents . For instance:

if( conditionA )
{
    if( conditionB )
    {

    }
    else
    {

    }
}
else
{

}

So it will be easier for you to track those missing braces.

           if ($username==$dbusername&&$password==dbpassword){

            echo "Login Successful. <a href='homepage.php'>Click Here to go to the home page</a>";
            $_SESSION['username']=$dbusername;

            ///Add this brace below
}
            else
            echo "Incorrect Login";

You are missing a closing curly braces here

        if ($username==$dbusername&&$password==dbpassword){

        echo "Login Successful. <a href='homepage.php'>Click Here to go to the home page</a>";
        $_SESSION['username']=$dbusername;
        }// here is missing

This is a causing the T_ELSE syntax error wich is relate to php syntax and not sql. It is agood pratice to use curly braces in if statments

if(something) { 
  //do stuff 
} else { 
  //do somthing else
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM