unexpected T_VARIABLE problem with simple log in.
Moderator: General Moderators
unexpected T_VARIABLE problem with simple log in.
Hi
I'm copied a simple log in program from the 'PHP 5 in easy steps' book. But I'm getting:
'..Parse error: syntax error, unexpected T_VARIABLE in I:\Program Files\xampp\htdocs\practise\authenticate.php on line 18..'
when testing the code in xampp.
Can anyone help me please and see where the problem is, thanks in advance.
The code with the problem is:
authenticate.php
1 <?php
2
3 $username = $_POST['username'];
4 $password = $_POST['password'];
5 $self = $_SERVER['PHP_SELF'];
6 $referer = $_SERVER['HTTP_REFERER'];
7
8 #if either of the form field is enpty return to the log-in page
9 if( ( !$username ) or ( !$password ) )
10 { header( "Location:$referer" ); exit(); }
11
12 $conn=@mysql_connect( "localhost", "root", "" ) or die( "could not connect." );
13
14 $rs = @mysql_select_db ( "dbforum", $conn )
15 or die("No database selected.")
16
17 #create the mysql query
18 $sql = "select * from members where username=\"$username\" and password = password(\"$password\")";
19
20 #execute the query
21 $rs = mysql_query( $sql, $conn )
22 or die( "Could not execute query" );
23
24 #get number of rows to match username and password
25 $num mysql_numrows( $rs );
26
27 #if there is a match the log-in is athenticated
28 if( $num != 0 )
29 { $msg = "Welcome $username - your log in succeeded!"; }
30
31 else #or return to login page
32 { header( "Location:$referer" ); exit(); }
33 ?>
34 <html><head><title>Log in authenticated</title></head>
35 <body><?php echo( $msg ); ?> </body></html>
Incidentally I've tested it in Editplus and 'mysql_numrows' on line 25 comes up brown, but something tells me it should be red.
The html code being referenced is:
<html><head><title>Log-In page</title></head>
<body>
Please enter your user details to log in here..
<form action = "authenticate.php" method = "post">
Username:<br>
<input type = "text" name = "username">
<br><br>
Password:<br>
<input type = "text" name "password">
<br><br>
<input type = "submit" value = "log In">
</form>
</body>
</html>
Thanks
I'm copied a simple log in program from the 'PHP 5 in easy steps' book. But I'm getting:
'..Parse error: syntax error, unexpected T_VARIABLE in I:\Program Files\xampp\htdocs\practise\authenticate.php on line 18..'
when testing the code in xampp.
Can anyone help me please and see where the problem is, thanks in advance.
The code with the problem is:
authenticate.php
1 <?php
2
3 $username = $_POST['username'];
4 $password = $_POST['password'];
5 $self = $_SERVER['PHP_SELF'];
6 $referer = $_SERVER['HTTP_REFERER'];
7
8 #if either of the form field is enpty return to the log-in page
9 if( ( !$username ) or ( !$password ) )
10 { header( "Location:$referer" ); exit(); }
11
12 $conn=@mysql_connect( "localhost", "root", "" ) or die( "could not connect." );
13
14 $rs = @mysql_select_db ( "dbforum", $conn )
15 or die("No database selected.")
16
17 #create the mysql query
18 $sql = "select * from members where username=\"$username\" and password = password(\"$password\")";
19
20 #execute the query
21 $rs = mysql_query( $sql, $conn )
22 or die( "Could not execute query" );
23
24 #get number of rows to match username and password
25 $num mysql_numrows( $rs );
26
27 #if there is a match the log-in is athenticated
28 if( $num != 0 )
29 { $msg = "Welcome $username - your log in succeeded!"; }
30
31 else #or return to login page
32 { header( "Location:$referer" ); exit(); }
33 ?>
34 <html><head><title>Log in authenticated</title></head>
35 <body><?php echo( $msg ); ?> </body></html>
Incidentally I've tested it in Editplus and 'mysql_numrows' on line 25 comes up brown, but something tells me it should be red.
The html code being referenced is:
<html><head><title>Log-In page</title></head>
<body>
Please enter your user details to log in here..
<form action = "authenticate.php" method = "post">
Username:<br>
<input type = "text" name = "username">
<br><br>
Password:<br>
<input type = "text" name "password">
<br><br>
<input type = "submit" value = "log In">
</form>
</body>
</html>
Thanks
Re: unexpected T_VARIABLE problem with simple log in.
Try
Code: Select all
18 $sql = "select * from members where username='".$username."' and password = password('".$password."')";Re: unexpected T_VARIABLE problem with simple log in.
Thanks for your help papa, just tried that.
Made the variables turn blue and green when I got rid of the slashes. But still have the same error message.
Do I need to use the single quotes you wrote, or are they just there for emphasis?
Made the variables turn blue and green when I got rid of the slashes. But still have the same error message.
Do I need to use the single quotes you wrote, or are they just there for emphasis?
Re: unexpected T_VARIABLE problem with simple log in.
It's good practice to use single quotes in mysql querys.
Try this:
Add a semi-colon.
Try this:
Code: Select all
14 $rs = @mysql_select_db ( "dbforum", $conn )
15 or die("No database selected.");
Re: unexpected T_VARIABLE problem with simple log in.
papa wrote:It's good practice to use single quotes in mysql querys.
Try this:
Add a semi-colon.Code: Select all
14 $rs = @mysql_select_db ( "dbforum", $conn ) 15 or die("No database selected.");
All part of the learning process though, thanks
It has cured that problem but now have another error message:
Parse error: syntax error, unexpected T_STRING in I:\Program Files\xampp\htdocs\practise\authenticate.php on line 25
with 'mysql_numrows' which is brown. I thought that might be a problem. Any ideas please?
Re: unexpected T_VARIABLE problem with simple log in.
Code: Select all
24 #get number of rows to match username and password
25 $num = mysql_numrows( $rs );Re: unexpected T_VARIABLE problem with simple log in.
Damn, what a fool I ampapa wrote:Come on dude.Code: Select all
24 #get number of rows to match username and password 25 $num = mysql_numrows( $rs );
So there's no error messages now, but the 'mysql_numrows' is still brown, and just refreshes the web page after putting in the stored username/password and submitting. Not going on to the following if statement seemingly.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: unexpected T_VARIABLE problem with simple log in.
Code: Select all
//line 9
if( ( !$username ) or ( !$password ) )
//should read
if( ( !$username ) || ( !$password ) )
Code: Select all
$rs = mysql_query( $sql, $conn )
or die( "Could not execute query" );
if($rs)
{
#get number of rows to match username and password
$num=mysql_numrows( $rs );
#if there is a match the log-in is athenticated
if( $num > 0 )
{
$msg = "Welcome $username - your log in succeeded!";
}
else #or return to login page
{
header( "Location:$referer" ); exit();
}
}
else
{
//QUERY DID NOT EXECUTE PROPERLY
}
Re: unexpected T_VARIABLE problem with simple log in.
Thanks aceconcepts, sorry about the delay in replying.aceconcepts wrote:I've re-written the rest of the code so it makes sense:Code: Select all
//line 9 if( ( !$username ) or ( !$password ) ) //should read if( ( !$username ) || ( !$password ) )
Code: Select all
$rs = mysql_query( $sql, $conn ) or die( "Could not execute query" ); if($rs) { #get number of rows to match username and password $num=mysql_numrows( $rs ); #if there is a match the log-in is athenticated if( $num > 0 ) { $msg = "Welcome $username - your log in succeeded!"; } else #or return to login page { header( "Location:$referer" ); exit(); } } else { //QUERY DID NOT EXECUTE PROPERLY }
Tried your code and indeed does make more sense. But still just refreshes and resets the text boxes. Which I guess is the 'else' statement.
Re: unexpected T_VARIABLE problem with simple log in.
Hi,
There is a bug on line 8.
Bug on line 8 is
$num=mysql_numrows( $rs );
It should be
$num=mysql_num_rows( $rs );
There is a bug on line 8.
Bug on line 8 is
$num=mysql_numrows( $rs );
It should be
$num=mysql_num_rows( $rs );
Re: unexpected T_VARIABLE problem with simple log in.
Thanks, that has made the brown 'mysql_numrows' turn into red 'mysql_num_rows' in edit plus.mukunthan wrote:Hi,
There is a bug on line 8.
Bug on line 8 is
$num=mysql_numrows( $rs );
It should be
$num=mysql_num_rows( $rs );
Still not working though. Its been one big mess up, I'll have to be more careful.