-2

Getting error in php code. The if and else statement are properly opened and closed still it throws error as show below:

Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\myfiles\register.php on line 30

I had seen many solution for the same, but no one is helpful.

<?php
require('config.php');

if(isset($_POST['submit']))
{       // first curly brace
        $email1=$_POST['email1'];
        $email2=$_POST['email2'];
        $pass1=$_POST['pass1'];
        $pass2=$_POST['pass2'];
        
        if($email1 == $email2)
        {//second curly brace
            if($pass1 == $pass2)
            {//third curly brace
        //I have to add this function here so i can't take else condition here
            $name = mysql_escape_string($_POST['$name']);
            $lname = mysql_escape_string($_POST['$lname']);
            $uname = mysql_escape_string($_POST['$uname']);
            $email1 = mysql_escape_string($_POST['$email1']);
            $email2 = mysql_escape_string($_POST['$email2']);
            $pass1 = mysql_escape_string($_POST['$pass1']);
            $pass2 = mysql_escape_string($_POST['$pass2']);
            
            }//third closed
            else
            {//fourth curly brace
                echo "Your passwords do not match<br/>";
                exit();//Working properly upto here
            }//fourth closed
            else //It show me the error in this line
            {   //fifth curly brace
                echo "Your email do not match<br/>";
                exit();
            }//fifth closed
        }//second closed
}//first closed
else
{//sixth curly brace

$form = '       
<form action="register.php" method="POST">

First Name:<input type="text" name="name"/><br/>
Last Name:<input type="text" name="lname"/><br/>
Username:<input type="text" name="uname"/><br/>
Email:<input type="text" name="email1"/><br/>
Confirm Email:<input type="text" name="email2"/><br/>
Password:<input type="password" name"pass1"/><br/>
Confirm Password:<input type="password" name="pass2"/><br/>
<input type="submit" value="Register" name="submit"/><br/>
</form>
';//The problem of EOT is solved    
echo $form;
}//sixth closed
?>
Bhatt Akshay
  • 159
  • 1
  • 14
  • because, `else` exists only if `if` exists ! – viral Jun 06 '15 at 07:28
  • I have mentioned it see properly still it's not working – Bhatt Akshay Jun 06 '15 at 07:31
  • If you correctly indented code, and if you sticked to brace on same/new line, you would have easily found the problem... – idmean Jun 06 '15 at 07:44
  • Welcome to stackoverflow @BhattAkshay. Be sure to check out the [tour here](http://stackoverflow.com/tour) to earn a badge, and learn how to use basic functions of the website. You should accept answers that help you so that you can get rep to gain more privileges on the website, as well as support the users who have helped you. – Blue Jun 06 '15 at 07:47

3 Answers3

2

In your php you have 2 else statements and you didn't close the first else statement. try this.

<?php
require('config.php');

if(isset($_POST['submit']))
{       
        $email1=$_POST['email1'];
        $email2=$_POST['email2'];
        $pass1=$_POST['pass1'];
        $pass2=$_POST['pass2'];

        if($email1 == $email2){
            if($pass1 == $pass2){
            }
            else
            {   echo "Your passwords do not match<br/>";
                exit();
            }//you missed it
        }else{   }
1

Looks like you've added an extra else statement without closing the first else statement. Although, because you're exiting, you have a ton of unneeded extra clutter. This should be functionality identical to your code. Here is a much easier way to handle this form:

if( isset($_POST['submit']) )
{       
    $email1 = $_POST['email1'];
    $email2 = $_POST['email2'];
    $pass1  = $_POST['pass1'];
    $pass2  = $_POST['pass2'];

    if ( $email1 != $email2 )
    {
        echo "Your email does not match<br/>";
        exit;
    }

    if ( $pass1 != $pass2 )
    {
        echo "Your passwords do not match<br/>";
        exit;
    }
}
else
{
    echo '      
    <form action="register.php" method="POST">

    First Name:<input type="text" name="name"/><br/>
    Last Name:<input type="text" name="lname"/><br/>
    Username:<input type="text" name="uname"/><br/>
    Email:<input type="text" name="email1"/><br/>
    Confirm Email:<input type="text" name="email2"/><br/>
    Password:<input type="password" name"pass1"/><br/>
    Confirm Password:<input type="password" name="pass2"/><br/>
    <input type="submit" value="Register" name="submit"/><br/>
    </form>
    ';
}
?>

From a readability standpoint, this is much better to follow, and will make things easier to understand.

Blue
  • 22,608
  • 7
  • 62
  • 92
0

you didn't close first if statement so use it like below

 <?php
if ($email1 == $email2) {
    if ($pass1 != $pass2) {
        echo "Your passwords do not match";
    } else {
        exit();
    }
} else {
}
?>
Vivek Singh
  • 2,453
  • 1
  • 14
  • 27