To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Newbies

Newbies Help for those who are just getting started

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-20-2009, 02:28 PM   #1
flashburn
Junior Member
 
Join Date: Oct 2009
Posts: 3
Problem with the syntax HELPP!

Hello I am tryin to build a simple login in php code but i keep gettin a problem, on my else statement before the echo "Incorrect Password" why is that any ideas!
Please help being tryin to fix it for hours ((((((((((((((((((( ! Here is that part of the code :
PHP Code:
<?php

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

if (
$username&&$password)
{

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

$query=mysql_query("select * from users where username='$username'");

$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);
        {
        echo
" You are in!";
        }
        {else
            echo
"Incorrect Password!";
            }
}
else
    die (
"That user doesn't exist!");

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

?>
Do you find anything wrong?

Last edited by NogDog; 10-20-2009 at 02:46 PM. Reason: Added [php] tags around code
flashburn is offline   Reply With Quote
Old 10-20-2009, 02:31 PM   #2
flashburn
Junior Member
 
Join Date: Oct 2009
Posts: 3
{else
echo "Incorrect Password!";
}
I have put that part in bracets because i tried numerous times without them. but still nothing is fixed I keep getting that :
Parse error: syntax error, unexpected T_ELSE in C:\XAMPP\xampp\htdocs\login.php on line 29
flashburn is offline   Reply With Quote
Old 10-20-2009, 02:53 PM   #3
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 11,752
I found 2 syntax errors: (1) you had an unwanted semi-colon after the IF condition where you check the login and password values, and (2) the "{" needs to come after the "else". Also, just for consistency, and ease in debugging, I'd recommend using braces with all if/else blocks, even if they're only one-liners:
PHP Code:
<?php
$username
= $_POST['username'];
$password = $_POST['password'];
if (
$username && $password) {
   
$connect = mysql_connect("localhost", "root", "") or die("couldn't connect!!");
   
mysql_select_db("phplogin") or die("Couldn't find db!");
   
$query = mysql_query("select * from users where username='$username'");
   
$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) {
         echo
" You are in!";
      } else {
         echo
"Incorrect Password!";
      }
   } else {
      die(
"That user doesn't exist!");
   }
} else {
   die(
"Please enter a username and password");
}
?>
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
freelancer.internet.com
Email me
NogDog is offline   Reply With Quote
Old 11-03-2009, 12:09 PM   #4
flashburn
Junior Member
 
Join Date: Oct 2009
Posts: 3
hmm not working

Quote:
Originally Posted by NogDog View Post
I found 2 syntax errors: (1) you had an unwanted semi-colon after the IF condition where you check the login and password values, and (2) the "{" needs to come after the "else". Also, just for consistency, and ease in debugging, I'd recommend using braces with all if/else blocks, even if they're only one-liners:
PHP Code:
<?php
$username
= $_POST['username'];
$password = $_POST['password'];
if (
$username && $password) {
   
$connect = mysql_connect("localhost", "root", "") or die("couldn't connect!!");
   
mysql_select_db("phplogin") or die("Couldn't find db!");
   
$query = mysql_query("select * from users where username='$username'");
   
$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) {
         echo
" You are in!";
      } else {
         echo
"Incorrect Password!";
      }
   } else {
      die(
"That user doesn't exist!");
   }
} else {
   die(
"Please enter a username and password");
}
?>
Tried the code it has fixed the problem on line 29 thank you .
I keep getting the Incorrect password message even when the pass is correct . Any idea why this is happenin? I am sure that the username and pass are the same in MYSQL.
flashburn is offline   Reply With Quote
Old 11-03-2009, 02:25 PM   #5
dagon
RTFM it's a way of life.
 
dagon's Avatar
 
Join Date: Nov 2001
Location: N.Z
Posts: 3,099
check the values:

PHP Code:
//snip

echo 'u = '.$username .' du = '.$dbusername.' p = '. $password .' dp = '.$dbpassword;

if (
$username == $dbusername && $password == $dbpassword) {
//snip
dagon is offline   Reply With Quote
Old 11-04-2009, 07:14 AM   #6
kbc1
Senior Member
 
Join Date: Nov 2005
Posts: 235
Personally I would recommend you escape both your $_POST['username'] and $_POST['password'] to keep security in mind.

Also, your code seems to allow duplicate usernames and passwords in your database. Surely you would only allow a user to login if $numrows was equal to 1?

Just a thought.
kbc1 is offline   Reply With Quote
Old 11-04-2009, 12:09 PM   #7
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 11,752
Quote:
Originally Posted by kbc1 View Post
Personally I would recommend you escape both your $_POST['username'] and $_POST['password'] to keep security in mind.

Also, your code seems to allow duplicate usernames and passwords in your database. Surely you would only allow a user to login if $numrows was equal to 1?

Just a thought.
That can be enforced by the database table definition by ensuring the username field is unique, so it does not really need to be enforced in the PHP login code (assuming it is enforced in the DB). It wouldn't hurt anything to check that it's equal to 1 (as opposed to > 0), but I don't see any particular advantage to either comparison.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
freelancer.internet.com
Email me
NogDog is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 04:44 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.