-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
?>
4
  • because, else exists only if if exists ! Commented Jun 6, 2015 at 7:28
  • I have mentioned it see properly still it's not working Commented Jun 6, 2015 at 7:31
  • If you correctly indented code, and if you sticked to brace on same/new line, you would have easily found the problem... Commented Jun 6, 2015 at 7:44
  • Welcome to stackoverflow @BhattAkshay. Be sure to check out the tour here 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. Commented Jun 6, 2015 at 7:47

3 Answers 3

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{   }
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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 {
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.