Page 1 of 1

Getting Error "unexpected T_ELSE" in line 39

Posted: Tue Dec 14, 2010 1:17 am
by jagannathg
Hello All,

When I am trying to insert the data into a table for the registration details I am getting the error two errors:

1. on line 39 Parse error: syntax error,
2. unexpected T_ELSE on line 39.

What my idea is to check the username which is already in the database and prompt the user that the username is already taken. else it should insert the data into my userdetails table in MySQL.

Kindly let me know where I am going wrong..

Please find my PHP files for register.php below is the code:

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Sanskar - Life with a difference</title>
	<link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />

<script language = "Javascript">

function Validate()
{
    if (document.form1.firstname.value == '')
    {
        alert('Please fill in your first name');
        return false;
    }

    if (document.form1.lastname.value == '')
    {
        alert('Please fill in your last name');
        return false;
    }

    if (document.form1.username.value == '')
    {
        alert('Please fill in your desired username');
        return false;
    }
        if (document.form1.password.value == '')
    {
       alert('Please fill in your desired password!');
      return false;
    }
    if (document.form1.cpassword.value == '')
    {
       alert('Please fill in your password again for confirmation!');
      return false;
    }
    if (document.form1.password.value != document.form1.cpassword.value)
    {
        alert("The two passwords are not identical! "+
        "Please enter the same password again for confirmation");
        return false;
    }
    if (document.form1.email.value == '')
    {
        alert('Please fill in your Email Address');
        return false;
    }
    if (document.form1.location.value == '')
    {
        alert('Please fill in your Email Address');
        return false;
    }
    return true;
}
</script>
</head>


</head>

<body>



<div id="outer">
	<div id="wrapper">
		<div id="nav">

		 <div class="clear"></div>
		</div>
		<div id="head">
			<div id="head-left"></div>
			<div id="head-right"></div>
			<div id="head-1"></div>
			<h1><span class="logo"><span class="top">Sanskar</span><span class="gadgets">life with a difference</span></span></h1>

		</div>
		<div id="head-2"></div>
		<div id="login">
			<div id="login-bot">
				<div id="login-box">
					<h2 class="login"><em>User</em>&nbsp;Registeration Page</h2>
			<form name='form1' method="post" action="insert_ac.php" onsubmit="return Validate();">
						<div id="login-username">
							<div><label for="firstname">First Name</label>: <input type="text" name="firstname" id="firstname"></div>
							<div><label for="lastname">Last Name</label>: <input type="text" name="lastname" id="lastname"></div>
							<div><label for="username">Username</label>: <input type="text" name="username" id="username"></div>
                                                        <div><label for="password">Password</label>: <input type="password" name="password" id="password"></div>
							<div><label for="cpassword">C Password</label>: <input type="password" name="cpassword" id="cpassword"></div>
							<div><label for="email">Email</label>: <input type="text" name="email" id="email"></div>
							<div><label for="location">Location</label>: <input type="text" name="location" id="location"></div>
						</div>

						<div class="clear">

						<div id="login-button">
							<input type="image" src="images/btn_login.gif" name="l" value="h"/>
						</div>
						</div>
					</form>
				</div>
				<div id="login-welcome">
					<div>
						<h2>Welcome</h2>
						<p>Don't forget to check <a href="http://www.freewebsitetemplates.com">free website templates</a> every day, because we add  a new free website template almost daily.</p>
					</div>
				</div>
				<div class="clear"></div>
			</div>
		</div>

</body>
</html>
And for inserting into database its insert_ac.php below is the code:

Code: Select all

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rajeshwari"; // Mysql password
$db_name="test"; // Database name
$tbl_name="userdetails"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


// Get values from form
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
$location=$_POST['location'];

echo "$firstname";

$query = mysql_query("SELECT * FROM userdetails WHERE username='$username'");

$numrows = mysql_num_rows($query);

if ($numrows!=0)
{

   while ($row = mysql_fetch_assoc($query))
   {
         $dbusername = $row['username'];
         if ($username==$dbusername)
         {
           echo "This username has been already taken";
         }
   }
    else
    {

        // Insert data into mysql
        $sql="INSERT INTO $tbl_name(id, firstname, lastname, username, password, email, location)VALUES(NULL, '$firstname', '$lastname','$username','$password','$email','$location')";
        $result=mysql_query($sql);

        // if successfully insert data into database, displays message "Successful".
        if($result)
        {
           echo "Your information has been Successful added in the Database!!!";
           echo "<BR>";
           echo "<a href='index.php'>Back to main page</a>";
           echo "<a href='logout.php'>Logout Account</a>";
        }
    }
}
else {
echo "Error";
}

// close connection
mysql_close();
?>

Re: Getting Error "unexpected T_ELSE" in line 39

Posted: Tue Dec 14, 2010 1:32 am
by Darhazer
There are several errors in your code
First of all, in PHP, while() (and other loops) have no else statements.
Second, you have selected userdetails for specific username. There is no point to check if the username retrieved from the database is the same as the username provided, as it will be always true.
So, your code should be:

Code: Select all

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rajeshwari"; // Mysql password
$db_name="test"; // Database name
$tbl_name="userdetails"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


// Get values from form
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
$location=$_POST['location'];

echo "$firstname";

$query = mysql_query("SELECT * FROM userdetails WHERE username='$username'");

if ($query) { // query was successful
	$numrows = mysql_num_rows($query);

	if ($numrows!=0) // there is at least one record in userdetails table for the given username
	{
          echo "This username has been already taken";
	} else {
        // Insert data into mysql
        $sql="INSERT INTO $tbl_name(id, firstname, lastname, username, password, email, location)VALUES(NULL, '$firstname', '$lastname','$username','$password','$email','$location')";
        $result=mysql_query($sql);

        // if successfully insert data into database, displays message "Successful".
        if($result)
        {
           echo "Your information has been Successful added in the Database!!!";
           echo "<BR>";
           echo "<a href='index.php'>Back to main page</a>";
           echo "<a href='logout.php'>Logout Account</a>";
        }
    }
} else {
echo "Error";
}

// close connection
mysql_close();
?>

Re: Getting Error "unexpected T_ELSE" in line 39

Posted: Tue Dec 14, 2010 2:14 am
by jagannathg
Darhazer wrote:There are several errors in your code
First of all, in PHP, while() (and other loops) have no else statements.
Second, you have selected userdetails for specific username. There is no point to check if the username retrieved from the database is the same as the username provided, as it will be always true.
So, your code should be:

Code: Select all

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rajeshwari"; // Mysql password
$db_name="test"; // Database name
$tbl_name="userdetails"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


// Get values from form
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
$location=$_POST['location'];

echo "$firstname";

$query = mysql_query("SELECT * FROM userdetails WHERE username='$username'");

if ($query) { // query was successful
	$numrows = mysql_num_rows($query);

	if ($numrows!=0) // there is at least one record in userdetails table for the given username
	{
          echo "This username has been already taken";
	} else {
        // Insert data into mysql
        $sql="INSERT INTO $tbl_name(id, firstname, lastname, username, password, email, location)VALUES(NULL, '$firstname', '$lastname','$username','$password','$email','$location')";
        $result=mysql_query($sql);

        // if successfully insert data into database, displays message "Successful".
        if($result)
        {
           echo "Your information has been Successful added in the Database!!!";
           echo "<BR>";
           echo "<a href='index.php'>Back to main page</a>";
           echo "<a href='logout.php'>Logout Account</a>";
        }
    }
} else {
echo "Error";
}

// close connection
mysql_close();
?>
Hello Darhazer,

The code you provide worked perfectly. Its been a week I am working with PHP and learning the language. I am so thankful to you that you have pointed out my mistakes and guided me with a correct solution. My Logic was worng which actually i was applying. Thanks again for your solution and help. Looking forward from you to help me and teach me PHP more.

Regards,
Jagannath

Re: Getting Error "unexpected T_ELSE" in line 39

Posted: Tue Dec 14, 2010 3:44 am
by jagannathg
Hello Darhazer,

I have any problem in displaying the First Name and Last Name of the user who has loggedin in the members page. I can successfully display the username using the session variable. But how can we display first and last name of a user who has logged in.

below is my members page code take a look, please help me with this:

Code: Select all

<?php

session_start();

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rajeshwari"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password)
{

$connect = mysql_connect("localhost","root","rajeshwari") or die ("Couldn't connect!");
mysql_select_db("test") or die ("Couldn't find db");

$query = mysql_query("SELECT * FROM userdetails WHERE username='$username'");
$query2 = mysql_query("SELECT firstname,lastname FROM userdetails WHERE username='$username'");
$result = mysql_query($query2);

$numrows = mysql_num_rows($query);




if ($numrows!=0)
{

   while ($row = mysql_fetch_assoc($query))
   {
         $dbusername = $row['username'];
         $dbpassword = $row['password'];
   }

   // check to see if they match!
   if ($username==$dbusername&&$password==$dbpassword)
   {
       //$row = mysql_fetch_array($result, MYSQL_ASSOC)
       //echo "{$row['firstname']}";
       $_SESSION['username']=$dbusername;
       echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
       echo "<b>You are logged in as:</b>&nbsp;&nbsp;&nbsp;<b>".$_SESSION['username']."</b>&nbsp;&nbsp;&nbsp;&nbsp;<a href='logout2.php'>[Logout]</a>";
       $dt1 = time();
       $dt2 = time();
       $mysql_datetime1 = strftime("%d %B %Y", $dt1);
       $mysql_datetime2 = strftime("%H:%M %p", $dt2);
       echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
       echo "<b>Current Login Date: $mysql_datetime1</b>";
       echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
       echo "<b>$mysql_datetime2</b>";
   }
   else
        die ("Incorrect password!");

}

else
   die("That user doesn't exist!");

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


?>


<head>




	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title>Sanskar - Life with a difference</title>
	<link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />

</head>

<body>

<div id="outer">
	<div id="wrapper">
		<div id="nav">

		<div id="nav-left">
		    <div id="nav-right">
    			<ul>
    			  <li><a href="http://www.freewebsitetemplates.com">ABOUT US</a></li>
    			  <li><a href="http://www.freewebsitetemplates.com">PRODUCTS</a></li>
    			  <li><a href="http://www.freewebsitetemplates.com">SERVICES</a></li>
    			  <li><a href="http://www.freewebsitetemplates.com">SHOPPING CART</a></li>
    			  <li><a href="http://www.freewebsitetemplates.com">NEW GADGETS</a></li>
    			  <li><a href="http://www.freewebsitetemplates.com">REGISTER</a></li>
    			</ul>
		    </div>
		  </div>


		  	<div class="clear"></div>
		</div>
		<div id="head">


			<div id="head-left"></div>
			<div id="head-right"></div>
			<div id="head-1"></div>

			<h1><span class="logo"><span class="top">Sanskar</span><span class="gadgets">life with a difference</span></span></h1>

		</div>
		<div id="head-2"></div>

</body>
</html>
Regards,
Jagannath

Re: Getting Error "unexpected T_ELSE" in line 39

Posted: Tue Dec 14, 2010 11:52 am
by Celauran
Whoops. Read that wrong. Nevermind.

Re: Getting Error "unexpected T_ELSE" in line 39

Posted: Tue Dec 14, 2010 3:03 pm
by Darhazer
You can fetch the first and last name exactly the same as you are storing the username in the session variable.
You can remove parts of the code that are not relevant to the problem when you are posting, for example your database credentials.

You HAVE TO sanitize your input (the variables that come from $_POST in the case) before passing them to mysql_query (use mysql_escape_string or mysql_real_escape_string)

You should follow some naming conventions. $query is a good name for holding string with the SQL query, but for the result of mysql_query() better use $res or $result. It makes code more readable and mistakes avoidable

Code: Select all

$result = mysql_query($query2);
This line is wrong as $query2 actually do not hold an SQL query, but a result of the execution of a query

King regards,
Darhazer

Re: Getting Error "unexpected T_ELSE" in line 39

Posted: Tue Dec 14, 2010 10:27 pm
by jagannathg
Darhazer wrote:You can fetch the first and last name exactly the same as you are storing the username in the session variable.
You can remove parts of the code that are not relevant to the problem when you are posting, for example your database credentials.

You HAVE TO sanitize your input (the variables that come from $_POST in the case) before passing them to mysql_query (use mysql_escape_string or mysql_real_escape_string)

You should follow some naming conventions. $query is a good name for holding string with the SQL query, but for the result of mysql_query() better use $res or $result. It makes code more readable and mistakes avoidable

Code: Select all

$result = mysql_query($query2);
This line is wrong as $query2 actually do not hold an SQL query, but a result of the execution of a query

King regards,
Darhazer
Hello Darhazer,

That worked too!!! Thanks for the advice. Now I am trying to write a code to update the userdetails table. My goal is any user who logs in can edit there details. So I am on to that.. if I am stuck then will surely take your help on it. Thanks again for guiding me through the convention part and pointing out my mistake.

Regards,
Jagannath

Re: Getting Error "unexpected T_ELSE" in line 39

Posted: Tue Dec 14, 2010 11:53 pm
by McInfo
Just say "no" to abusing &nbsp; for padding.

Re: Getting Error "unexpected T_ELSE" in line 39

Posted: Wed Dec 15, 2010 2:44 am
by jagannathg
McInfo wrote:Just say "no" to abusing &nbsp; for padding.
Hello McInfo,

Thanks for your advice. How can I overcome it. I am very new to PHP.

Regards,
Jagannath

Re: Getting Error "unexpected T_ELSE" in line 39

Posted: Wed Dec 15, 2010 3:45 am
by Darhazer
jagannathg wrote:
McInfo wrote:Just say "no" to abusing &nbsp; for padding.
Hello McInfo,

Thanks for your advice. How can I overcome it. I am very new to PHP.

Regards,
Jagannath
Actually this is HTML/CSS issue. Learn some basics - w3schools is a good place to start