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
tinoda
Forum Commoner
Posts: 33 Joined: Mon Dec 29, 2008 3:32 am
Post
by tinoda » Fri Mar 20, 2009 5:17 am
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);
?>
php_east
Forum Contributor
Posts: 453 Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.
Post
by php_east » Fri Mar 20, 2009 5:44 am
Code: Select all
if( ($username == $row['username']) && ($password==$row['password']) ) {
tinoda
Forum Commoner
Posts: 33 Joined: Mon Dec 29, 2008 3:32 am
Post
by tinoda » Fri Mar 20, 2009 5:49 am
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
Post
by FireLord » Fri Mar 20, 2009 5:57 am
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
Post
by mattpointblank » Fri Mar 20, 2009 5:58 am
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
Post
by tinoda » Fri Mar 20, 2009 6:15 am
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
Post
by FireLord » Fri Mar 20, 2009 6:17 am
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