Page 1 of 1
user login problem - whats wrong with my code
Posted: Fri Mar 20, 2009 5:17 am
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);
?>
Re: user login problem - whats wrong with my code
Posted: Fri Mar 20, 2009 5:44 am
by php_east
Code: Select all
if( ($username == $row['username']) && ($password==$row['password']) ) {
Re: user login problem - whats wrong with my code
Posted: Fri Mar 20, 2009 5:49 am
by tinoda
php_east wrote:Code: Select all
if( ($username == $row['username']) && ($password==$row['password']) ) {
unfortunately it still gives me the same result
Re: user login problem - whats wrong with my code
Posted: Fri Mar 20, 2009 5:57 am
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);
#
# ?>
Re: user login problem - whats wrong with my code
Posted: Fri Mar 20, 2009 5:58 am
by mattpointblank
Echo out your query variables and make sure they're what you think they are.
Re: user login problem - whats wrong with my code
Posted: Fri Mar 20, 2009 6:15 am
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
Re: user login problem - whats wrong with my code
Posted: Fri Mar 20, 2009 6:17 am
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