unexpected T_VARIABLE problem with simple log in.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jeggae
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2008 4:53 am

unexpected T_VARIABLE problem with simple log in.

Post 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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: unexpected T_VARIABLE problem with simple log in.

Post by papa »

Try

Code: Select all

 
18 $sql = "select * from members where username='".$username."' and password = password('".$password."')";
Jeggae
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2008 4:53 am

Re: unexpected T_VARIABLE problem with simple log in.

Post 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?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: unexpected T_VARIABLE problem with simple log in.

Post 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.
Jeggae
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2008 4:53 am

Re: unexpected T_VARIABLE problem with simple log in.

Post 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?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: unexpected T_VARIABLE problem with simple log in.

Post by papa »

Code: Select all

24 #get number of rows to match username and password
25 $num = mysql_numrows( $rs );
Come on dude. :)
Jeggae
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2008 4:53 am

Re: unexpected T_VARIABLE problem with simple log in.

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: unexpected T_VARIABLE problem with simple log in.

Post 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
}
 
Jeggae
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2008 4:53 am

Re: unexpected T_VARIABLE problem with simple log in.

Post 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.
mukunthan
Forum Newbie
Posts: 12
Joined: Sat Sep 13, 2008 12:52 am

Re: unexpected T_VARIABLE problem with simple log in.

Post 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 );
Jeggae
Forum Newbie
Posts: 10
Joined: Fri Oct 10, 2008 4:53 am

Re: unexpected T_VARIABLE problem with simple log in.

Post 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.
Post Reply