How could I modify this syntax error ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
omid020
Forum Newbie
Posts: 7
Joined: Wed Jan 16, 2008 5:22 am
Contact:

How could I modify this syntax error ?

Post by omid020 »

~pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi
I`m preparing this php class for using with amfphp and making an authentication interface with php and flash .
But I receive this error :
Parse error: syntax error, unexpected T_ELSE in C:\AppServ\www\amfphp\services\prev2~.php on line 69

Code: Select all

<?php
//Include database info
include ('db_connect.inc.php');
 
// Create new service for PHP Remoting as Class.
class Login1 {
            function Login1() {
        //Define the methodTable
        $this->methodTable = array(
            "doPost" => array(
                "description" => "Send a query to mySQL",
                "access" => "remote"
            ),
            "doCompare" => array(
                "description" => "Comparison of user input and mySQL value",
                "access" => "remote"
            )
        );
 
        //Connect to MySQL and select database
        $link = mysql_connect(HOST, DBUSER, PASS);
        $db = mysql_select_db(DB);
 
}
 
 
//Function for login
            function doPost($_POST) {
if (!session_is_registered('loginid') || !session_is_registered('username'))
{
    // user is not logged in.
    if (isset($_POST['cmdlogin']))
    {
        // retrieve the username and password sent from login form
        // First we remove all HTML-tags and PHP-tags, then we create a md5-hash
        // This step will make sure the script is not vurnable to sql injections.
        $u = strip_tags($_POST['username']);
        $p = md5(strip_tags($_POST['password']));
        //Now let us look for the user in the database.
        $query = sprintf("SELECT loginid FROM login WHERE username = '%s' AND password = '%s' LIMIT 1;",
            mysql_real_escape_string($u), mysql_real_escape_string($p));
        $result = mysql_query($query);
        }
    }
}
 
//Function for compare
        function doCompare ($result) {
        // If the database returns a 0 as result we know the login information is incorrect.
        // If the database returns a 1 as result we know  the login was correct and we proceed.
        // If the database returns a result > 1 there are multple users
        // with the same username and password, so the login will fail.
        if (mysql_num_rows($result) != 1)
        {
            // invalid login information
        return array("status" => "fail");
        } else {
            // Login was successfull
            $row = mysql_fetch_array($result);
            // Save the user ID for use later
            $_SESSION['loginid'] = $row['loginid'];
              // Save the username for use later
            $_SESSION['username'] = $u;
              // Now we show the userbox
            return array("status" => "success");
 
}
 
 else {
         // User is not logged in and has not pressed the login button
         // so we show him the loginform
       return array("status" => "fail");
 
} else {
     // The user is already loggedin, so we show the userbox.
    return array("status" => "success");
}
}
}
?>
I`m beginner with php and I don`t know how could I debug it ?
Please help me .


~pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How could I modify this syntax error ?

Post by Christopher »

omid020 wrote:I`m beginner with php and I don`t know how could I debug it ?
From the error message, look at line 69 and see if you can find and unexpected ELSE in that code. Read the manual page on the if/else statement to understand what is expected.

http://www.php.net/manual/en/control-st ... s.else.php
(#10850)
User avatar
omid020
Forum Newbie
Posts: 7
Joined: Wed Jan 16, 2008 5:22 am
Contact:

Re: How could I modify this syntax error ?

Post by omid020 »

arborint wrote:
omid020 wrote:I`m beginner with php and I don`t know how could I debug it ?
From the error message, look at line 69 and see if you can find and unexpected ELSE in that code. Read the manual page on the if/else statement to understand what is expected.

http://www.php.net/manual/en/control-st ... s.else.php
PHP.net example is very simple and couldn`t guide me to debug my code . My code have multiple 'else' statements and make me confused . Please tell me what`s my code problem .
Regards
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: How could I modify this syntax error ?

Post by Zoxive »

omid020 wrote:Please tell me what`s my code problem.
Formatting, very difficult to read. Which makes it difficult to see what brackets open and close what.

Keep the way you format your code uniform. In your code you open functions on the same line, but your if statements are on a different line. Either is fine, but try keeping it consistent throughout your code.

Code: Select all

function asdf() {
  ///
}
if(/* ... */)
{
  //
}
With it formatted a little better, now take a look and tell me if you can see your problem.

Code: Select all

<?php
//Include database info
include ('db_connect.inc.php');
 
// Create new service for PHP Remoting as Class.
class Login1{
  function Login1(){
    //Define the methodTable
    $this->methodTable = array(
        "doPost" => array(
            "description" => "Send a query to mySQL",
            "access" => "remote"
        ),
        "doCompare" => array(
            "description" => "Comparison of user input and mySQL value",
            "access" => "remote"
        )
    );
 
    //Connect to MySQL and select database
    $link = mysql_connect(HOST, DBUSER, PASS);
    $db = mysql_select_db(DB);
  }
 
  //Function for login
  function doPost($_POST){
    if (!session_is_registered('loginid') || !session_is_registered('username')){
      // user is not logged in.
      if (isset($_POST['cmdlogin'])){
        // retrieve the username and password sent from login form
        // First we remove all HTML-tags and PHP-tags, then we create a md5-hash
        // This step will make sure the script is not vurnable to sql injections.
        $u = strip_tags($_POST['username']);
        $p = md5(strip_tags($_POST['password']));
        //Now let us look for the user in the database.
        $query = sprintf("SELECT loginid FROM login WHERE username = '%s' AND password = '%s' LIMIT 1;",
            mysql_real_escape_string($u), mysql_real_escape_string($p));
        $result = mysql_query($query);
      }
    }
  }
 
  //Function for compare
  function doCompare ($result){
    // If the database returns a 0 as result we know the login information is incorrect.
    // If the database returns a 1 as result we know  the login was correct and we proceed.
    // If the database returns a result > 1 there are multple users
    // with the same username and password, so the login will fail.
    if (mysql_num_rows($result) != 1){
      // invalid login information
      return array("status" => "fail");
    }else{
      // Login was successfull
      $row = mysql_fetch_array($result);
      // Save the user ID for use later
      $_SESSION['loginid'] = $row['loginid'];
      // Save the username for use later
      $_SESSION['username'] = $u;
      // Now we show the userbox
      return array("status" => "success");
    }else{
      // User is not logged in and has not pressed the login button
      // so we show him the loginform
      return array("status" => "fail");
    }else{
      // The user is already loggedin, so we show the userbox.
      return array("status" => "success");
    }
  }
}
?>
User avatar
omid020
Forum Newbie
Posts: 7
Joined: Wed Jan 16, 2008 5:22 am
Contact:

Re: How could I modify this syntax error ?

Post by omid020 »

:( My problem is not solved even with better configuration .
What should I do now ?
Scrumpy.Gums
Forum Commoner
Posts: 71
Joined: Thu Aug 30, 2007 2:57 pm
Location: Bristol, UK

Re: How could I modify this syntax error ?

Post by Scrumpy.Gums »

In the doCompare function, you need to use the elseif. An if statement can only have one else statement.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: How could I modify this syntax error ?

Post by Zoxive »

omid020 wrote::( My problem is not solved even with better configuration .
What should I do now ?
The different formating was to help read it better.

Your problem is the the 3 else statments in a row. You can only have 1 else statement in an If statement. Unless its an elseif(somethingelseistrue). http://php.net/manual/en/control-structures.else.php
anutosh
Forum Newbie
Posts: 1
Joined: Thu Feb 21, 2008 9:06 am

Re: How could I modify this syntax error ?

Post by anutosh »

Your Problem is in Basics of Coding Logic

There are more than one "else" condition, instead of one "else" condition permittable by any programming language.

This is very common error, but be careful of it, because this type of Logical error will not be acceptable by any Programmer. 8O
User avatar
omid020
Forum Newbie
Posts: 7
Joined: Wed Jan 16, 2008 5:22 am
Contact:

Re: How could I modify this syntax error ?

Post by omid020 »

Zoxive wrote:
omid020 wrote::( My problem is not solved even with better configuration .
What should I do now ?
The different formating was to help read it better.

Your problem is the the 3 else statments in a row. You can only have 1 else statement in an If statement. Unless its an elseif(somethingelseistrue). http://php.net/manual/en/control-structures.else.php
Now I modified my code :

Code: Select all

<?php
//Include database info
include ('db_connect.inc.php');
 
// Create new service for PHP Remoting as Class.
class Login1{
  function Login1(){
    //Define the methodTable
    $this->methodTable = array(
        "doPost" => array(
            "description" => "Send a query to mySQL",
            "access" => "remote"
        ),
        "doCompare" => array(
            "description" => "Comparison of user input and mySQL value",
            "access" => "remote"
        )
    );
 
    //Connect to MySQL and select database
    $link = mysql_connect(HOST, DBUSER, PASS);
    $db = mysql_select_db(DB);
  }
 
  //Function for login
  function doPost($_POST){
    if (!session_is_registered('loginid') || !session_is_registered('username')){
      // user is not logged in.
      if (isset($_POST['cmdlogin'])){
        // retrieve the username and password sent from login form
        // First we remove all HTML-tags and PHP-tags, then we create a md5-hash
        // This step will make sure the script is not vurnable to sql injections.
        $u = strip_tags($_POST['username']);
        $p = md5(strip_tags($_POST['password']));
        //Now let us look for the user in the database.
        $query = sprintf("SELECT loginid FROM login WHERE username = '%s' AND password = '%s' LIMIT 1;",
            mysql_real_escape_string($u), mysql_real_escape_string($p));
        $result = mysql_query($query);
      }
    }
  }
 
  //Function for compare
  function doCompare ($result){
    // If the database returns a 0 as result we know the login information is incorrect.
    // If the database returns a 1 as result we know  the login was correct and we proceed.
    // If the database returns a result > 1 there are multple users
    // with the same username and password, so the login will fail.
    if (mysql_num_rows($result) != 1){
      // invalid login information
      return array("status" => "fail");
    }elseif{
      // Login was successfull
      $row = mysql_fetch_array($result);
      // Save the user ID for use later
      $_SESSION['loginid'] = $row['loginid'];
      // Save the username for use later
      $_SESSION['username'] = $u;
      // Now we show the userbox
      return array("status" => "success");
    }elseif{
      // User is not logged in and has not pressed the login button
      // so we show him the loginform
      return array("status" => "fail");
    }else{
      // The user is already loggedin, so we show the userbox.
      return array("status" => "success");
    }
  }
}
?>
 
But I receive this error :
Parse error: syntax error, unexpected T_ELSEIF in C:\AppServ\www\amfphp\services\prev1~.php on line 61

Did I change my code correctly ?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: How could I modify this syntax error ?

Post by Zoxive »

Please read the documentation.

http://us2.php.net/elseif
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: How could I modify this syntax error ?

Post by kryles »

elseif needs a condition

Code: Select all

 
$var = 4;
 
if($var == 1)
{
//do something
}
elseif($var == 2)
{
//do something else
}
elseif($var == 3)
{
//do something else
}
else
{
//var is anything else
}
 
Your elseif were missing condition to them
User avatar
omid020
Forum Newbie
Posts: 7
Joined: Wed Jan 16, 2008 5:22 am
Contact:

Re: How could I modify this syntax error ?

Post by omid020 »

kryles wrote:
Your elseif were missing condition to them
You mean my code doesn`t have

Code: Select all

$var = 4;
, am I true ?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: How could I modify this syntax error ?

Post by Zoxive »

omid020 wrote: You mean my code doesn`t have

Code: Select all

$var = 4;
, am I true ?
False.

I am not sure how you can miss that an elseif statements need a condition.

Code: Select all

// All you have
}elseif{
// What you need
}elseif(/* Condition HERE */){
// this is what ever you chose just like a normal if statment
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: How could I modify this syntax error ?

Post by kryles »

Wow sorry...I thought my example was pretty descriptive in the problem.......

Code: Select all

 
 if (mysql_num_rows($result) != 1){
       // invalid login information
       return array("status" => "fail");
     }elseif[color=#0000FF](PUT CONDITION HERE!!!!!)[/color]{
        // Login was successfull
       $row = mysql_fetch_array($result);
       // Save the user ID for use later
       $_SESSION['loginid'] = $row['loginid'];
       // Save the username for use later
       $_SESSION['username'] = $u;
       // Now we show the userbox
       return array("status" => "success");
     }elseif[color=#0000FF](PUT CONDITION HERE!!!!)[/color]{
       // User is not logged in and has not pressed the login button
       // so we show him the loginform
       return array("status" => "fail");
     }else{
       // The user is already loggedin, so we show the userbox.
       return array("status" => "success");
     }
 
If you still don't get it think of them as this:

Code: Select all

 
 if (mysql_num_rows($result) != 1){
       // invalid login information
       return array("status" => "fail");
     }
        else
         {
            if[color=#0000FF](PUT CONDITION HERE!!!!!)[/color]
            {
             // Login was successfull
             $row = mysql_fetch_array($result);
            // Save the user ID for use later
            $_SESSION['loginid'] = $row['loginid'];
            // Save the username for use later
             $_SESSION['username'] = $u;
             // Now we show the userbox
             return array("status" => "success");
             }
     }
       else
       {
          if[color=#0000FF](PUT CONDITION HERE!!!!)[/color]
          {
          // User is not logged in and has not pressed the login button
          // so we show him the loginform
          return array("status" => "fail");
         }
     }
        else
        {
       // The user is already loggedin, so we show the userbox.
       return array("status" => "success");
     }
 
It is saying IF(this is true) { do this } ELSE IF(this is true) { do this } ELSE {none of the others were true so do this }
Post Reply