user login problem - whats wrong with my code

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
tinoda
Forum Commoner
Posts: 33
Joined: Mon Dec 29, 2008 3:32 am

user login problem - whats wrong with my code

Post by tinoda »

the code below repeatedly outputs "login unsuccessful" even if I enter the correct username and password. Where am I going wrong? Sorry I am a newbie in PHP. Thanks for helping me.

Code: Select all

<?php
 
$username=$_POST['username'];
$password=$_POST['password'];
 
if ($username && $password) {
 
     $db = odbc_connect("xx","xxx","xxx") or die ("xxxxx");
     
      $sql = "SELECT * FROM logintest WHERE username='$username' AND password='$password'";
      $res = odbc_exec($db, $sql);
      $row = odbc_fetch_array($res);
     
         
      if(($username && $password) == ($row['username'] && $row['password'])) {
        
        echo "login successful";
        } else {
        echo "login unsuccessful";
        }
}
   odbc_free_result($res);
 
odbc_close($db);
 
?>      
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: user login problem - whats wrong with my code

Post by php_east »

Code: Select all

      
if( ($username == $row['username']) && ($password==$row['password']) ) {
 
       
tinoda
Forum Commoner
Posts: 33
Joined: Mon Dec 29, 2008 3:32 am

Re: user login problem - whats wrong with my code

Post by tinoda »

php_east wrote:

Code: Select all

      
if( ($username == $row['username']) && ($password==$row['password']) ) {
 
       
unfortunately it still gives me the same result
FireLord
Forum Newbie
Posts: 6
Joined: Fri Mar 20, 2009 5:44 am

Re: user login problem - whats wrong with my code

Post by FireLord »

maybe:

Code: Select all

#      $sql = "SELECT * FROM logintest WHERE username='".$username."' AND password='".$password."'";
#       $res = odbc_exec($db, $sql);
#       $row = odbc_fetch_array($res);
#      
#          
#       if( ($username == $row['username']) && ($password==$row['password']) ) {
#        
#         echo "login successful";
#         } else {
#         echo "login unsuccessful";
#         }
# }
#    odbc_free_result($res);
#  
# odbc_close($db);
#  
# ?>
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: user login problem - whats wrong with my code

Post by mattpointblank »

Echo out your query variables and make sure they're what you think they are.
tinoda
Forum Commoner
Posts: 33
Joined: Mon Dec 29, 2008 3:32 am

Re: user login problem - whats wrong with my code

Post by tinoda »

mattpointblank wrote:Echo out your query variables and make sure they're what you think they are.
after echoing the variables $res and $num_rows I get the following output

Code: Select all

login unsuccessful, 
Resource id #3, 0
FireLord
Forum Newbie
Posts: 6
Joined: Fri Mar 20, 2009 5:44 am

Re: user login problem - whats wrong with my code

Post by FireLord »

tinoda wrote:the code below repeatedly outputs "login unsuccessful" even if I enter the correct username and password. Where am I going wrong? Sorry I am a newbie in PHP. Thanks for helping me.

Code: Select all

<?php
 
$username=$_POST['username'];
$password=$_POST['password'];
 
if ($username && $password) {
 
     $db = odbc_connect("xx","xxx","xxx") or die ("xxxxx");
     
      $sql = "SELECT * FROM logintest WHERE username='$username' AND password='$password'";
      $res = odbc_exec($db, $sql);
      $row = odbc_fetch_array($res);
     
         
      if(($username && $password) == ($row['username'] && $row['password'])) {
        
        echo "login successful";
        } else {
        echo "login unsuccessful";
        }
}
   odbc_free_result($res);
 
odbc_close($db);
 
?>      
I suggest you do some traces:

1) trace($username); and trace($password); after do

Code: Select all

$username=$_POST['username'];
$password=$_POST['password'];
2) test your query with correct username and password with mysql or with what you use

3) trace($row)

4) test if control


Check it on apache log
Post Reply