...

View Full Version : Parse error: syntax error, unexpected '{' in



gsness
02-01-2012, 12:53 AM
Hello!

I am pretty new to PHP coding and have hit a stumbling block (by new I mean trial by fire).

I have a login page which give me an error:
Parse error: syntax error, unexpected '{' in index5.php on line 7

I have checked and I have closed all of my {'s but still get the error. The chunk of code is below:


if(isset($_POST['user'])
{
$user = sanitizeString($_POST['user'])
$pass = sanitizeString($_POST['user'])
if ($user == "" | $pass == "")
{
$error = "Not all fields were entered<br />"
}
else
{
$query = "SELECT user,pass FROM users
WHERE user='$user AND pass=$pass"

if (mysql_num_rows(queryMysql($query)) == 0
{
$error = "Username/Password Invalid<br />";
}
else
{
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
die("You are now logged in.")
}
}
}

Any ideas would really be appreciated. I have a feeling it is probably something simple and stupid, but I'll take the chance.

tracknut
02-01-2012, 01:01 AM
You've missed the semicolons at the ends of several statements, like lines 3,4,7,12,22

Dave

BluePanther
02-01-2012, 01:06 AM
You've missed the semicolons at the ends of several statements, like lines 3,4,7,12,22

Dave

This. Just in case you don't understand, and end of line has to be defined in PHP using the ;. Otherwise, it just counts the next line as part of the same statement.

gsness
02-01-2012, 01:30 AM
I fixed the semicolons (I caught that right after I posted). But still doesn't take care of the Parse error: syntax error, unexpected '{' errors. I counted and recounted and it seems that they match up perfectly. So far that seems to have worked on that error but now I am getting:

Parse error: syntax error, unexpected T_STRING in index5.php on line 16


if(isset($_POST['user']))
{
$user = sanitizeString($POST['user']);
$pass = sanitizeString($POST['pass']);
if ($user == "" | $pass == "")
{
$error = "Not all fields entered<br />;
}
else
{
$query = "SELECT user, pass FROM users
WHERE user='$user' and pass='$pass'";

if (mysql_num_rows(queryMysql($query)) == 0
{
$error = "Username/Password invalid<br />";
}
else
{
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
die("You are now logged in");
}
}
}

Line 16 is the select statement: $query = "SELECT user, pass FROM users
WHERE user='$user' and pass='$pass'";

Like I said I am a fish out of water here, but trying to muddle my way through.

Inigoesdr
02-01-2012, 01:33 AM
You're missing a close quote now. Use an IDE that has a syntax check and you will spot syntax errors before you even load the file.

Also, please remember to read the stickies for this forum. In particular the one about using
tags (http://www.codingforums.com/showthread.php?t=68462) when posting code.

tracknut
02-01-2012, 01:34 AM
You're missing a quote at the end of:


$error = "Not all fields entered<br />";

BluePanther
02-01-2012, 01:35 AM
I fixed the semicolons (I caught that right after I posted). But still doesn't take care of the Parse error: syntax error, unexpected '{' errors. I counted and recounted and it seems that they match up perfectly. So far that seems to have worked on that error but now I am getting:

Parse error: syntax error, unexpected T_STRING in index5.php on line 16


if(isset($_POST['user']))
{
$user = sanitizeString($POST['user']);
$pass = sanitizeString($POST['pass']);
if ($user == "" | $pass == "")
{
$error = "Not all fields entered<br />;
}
else
{
$query = "SELECT user, pass FROM users
WHERE user='$user' and pass='$pass'";

if (mysql_num_rows(queryMysql($query)) == 0
{
$error = "Username/Password invalid<br />";
}
else
{
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
die("You are now logged in");
}
}
}

Line 16 is the select statement: $query = "SELECT user, pass FROM users
WHERE user='$user' and pass='$pass'";

Like I said I am a fish out of water here, but trying to muddle my way through.

You should learn more about the errors PHP gives you. It's saying that it didn't expect a string on line 16. This means that before or on line 16, you've missed something.

With the PHP code highlighting on this forum, I'm sure you can notice your missing " ;). I would suggest using an IDE like Netbeans which will highlight and hint code for you.

gsness
02-01-2012, 01:41 AM
I have the close code, but it is further down the script. I didn't include it because I thought it would be better to give the segment of script instead of the whole page.

Here is the entire script:


<?php //srlogin.php
include_once 'srheader.php';
echo "<h3>User Log In</h3>";
$error = $user = $pass = "";

if(isset($_POST['user']))
{
$user = sanitizeString($POST['user']);
$pass = sanitizeString($POST['pass']);
if ($user == "" | $pass == "")
{
$error = "Not all fields entered<br />;
}
else
{
$query = "SELECT user,pass FROM users
WHERE user='$user' and pass='$pass'";

if (mysql_num_rows(queryMysql($query)) == 0
{
$error = "Username/Password invalid<br />";
}
else
{
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
die("You are now logged in");
}
}
}

echo <<<_END

<form method='post' action='srlogin.php'>$error
Username <input type='text' maxlength='50' name='user'
value='$user' /><br />
Password <input type= 'password' maxlength='20' name='pass'
value='$pass' /><br />
&nbsp$ &nbsp$ &nbsp$ &nbsp$ &nbsp$ &nbsp$ &nbsp$
<input type='submit' value='Login' />
</form>
_END;
?>

BluePanther
02-01-2012, 01:43 AM
I have the close code, but it is further down the script. I didn't include it because I thought it would be better to give the segment of script instead of the whole page.

Here is the entire script:

<?php //srlogin.php
include_once 'srheader.php';
echo "<h3>User Log In</h3>";
$error = $user = $pass = "";

if(isset($_POST['user']))
{
$user = sanitizeString($POST['user']);
$pass = sanitizeString($POST['pass']);
if ($user == "" | $pass == "")
{
$error = "Not all fields entered<br />;
}
else
{
$query = "SELECT user,pass FROM users
WHERE user='$user' and pass='$pass'";

if (mysql_num_rows(queryMysql($query)) == 0
{
$error = "Username/Password invalid<br />";
}
else
{
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
die("You are now logged in");
}
}
}

echo <<<_END

<form method='post' action='srlogin.php'>$error
Username <input type='text' maxlength='50' name='user'
value='$user' /><br />
Password <input type= 'password' maxlength='20' name='pass'
value='$pass' /><br />
&nbsp$ &nbsp$ &nbsp$ &nbsp$ &nbsp$ &nbsp$ &nbsp$
<input type='submit' value='Login' />
</form>
_END;
?>

We have answered you in our posts, and given you the solutions. Not only that, we have told you about techniques to help you in the future.

Also, PLEASE pay attention to the forum stickies, AND MY SIGNATURE, about [PHP] tags.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum