Page 1 of 1

How could I modify this syntax error ?

Posted: Wed Feb 20, 2008 2:49 pm
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]

Re: How could I modify this syntax error ?

Posted: Wed Feb 20, 2008 3:26 pm
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

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 7:36 am
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

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 8:16 am
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");
    }
  }
}
?>

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 9:02 am
by omid020
:( My problem is not solved even with better configuration .
What should I do now ?

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 9:09 am
by Scrumpy.Gums
In the doCompare function, you need to use the elseif. An if statement can only have one else statement.

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 9:09 am
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

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 9:22 am
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

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 9:28 am
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 ?

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 9:35 am
by Zoxive
Please read the documentation.

http://us2.php.net/elseif

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 9:42 am
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

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 12:34 pm
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 ?

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 12:42 pm
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

Re: How could I modify this syntax error ?

Posted: Thu Feb 21, 2008 1:24 pm
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 }