Page 1 of 1

unexpected T_VARIABLE problem with simple log in.

Posted: Fri Oct 10, 2008 5:26 am
by Jeggae
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

Re: unexpected T_VARIABLE problem with simple log in.

Posted: Fri Oct 10, 2008 5:28 am
by papa
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.

Posted: Fri Oct 10, 2008 5:40 am
by Jeggae
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?

Re: unexpected T_VARIABLE problem with simple log in.

Posted: Fri Oct 10, 2008 5:45 am
by papa
It's good practice to use single quotes in mysql querys.

Try this:

Code: Select all

14 $rs = @mysql_select_db ( "dbforum", $conn )
15 or die("No database selected.");
 
Add a semi-colon.

Re: unexpected T_VARIABLE problem with simple log in.

Posted: Fri Oct 10, 2008 6:08 am
by Jeggae
papa wrote:It's good practice to use single quotes in mysql querys.

Try this:

Code: Select all

14 $rs = @mysql_select_db ( "dbforum", $conn )
15 or die("No database selected.");
 
Add a semi-colon.

:oops: :banghead: embarrased I never noticed that papa. Such a basic thing lol
All part of the learning process though, thanks :wink:

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.

Posted: Fri Oct 10, 2008 6:29 am
by papa

Code: Select all

24 #get number of rows to match username and password
25 $num = mysql_numrows( $rs );
Come on dude. :)

Re: unexpected T_VARIABLE problem with simple log in.

Posted: Fri Oct 10, 2008 7:27 am
by Jeggae
papa wrote:

Code: Select all

24 #get number of rows to match username and password
25 $num = mysql_numrows( $rs );
Come on dude. :)
Damn, what a fool I am :oops: But I'm learning, thanks :mrgreen:

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.

Re: unexpected T_VARIABLE problem with simple log in.

Posted: Fri Oct 10, 2008 7:47 am
by aceconcepts

Code: Select all

 
//line 9
if( ( !$username ) or ( !$password ) )
 
//should read
if( ( !$username ) || ( !$password ) )
 
I've re-written the rest of the code so it makes sense:

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.

Posted: Sat Oct 11, 2008 4:40 am
by Jeggae
aceconcepts wrote:

Code: Select all

 
//line 9
if( ( !$username ) or ( !$password ) )
 
//should read
if( ( !$username ) || ( !$password ) )
 
I've re-written the rest of the code so it makes sense:

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
}
 
Thanks aceconcepts, sorry about the delay in replying.

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.

Posted: Sat Oct 11, 2008 7:19 am
by mukunthan
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 );

Re: unexpected T_VARIABLE problem with simple log in.

Posted: Sat Oct 11, 2008 10:09 am
by Jeggae
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 );
Thanks, that has made the brown 'mysql_numrows' turn into red 'mysql_num_rows' in edit plus. :wink: Checked the book and looks like they had it wrong in there.

Still not working though. Its been one big mess up, I'll have to be more careful.