PDA

View Full Version : If Statement question.


ArcadEd
03-21-2006, 07:50 PM
I'm fairly new to PHP and I've been looking through some example code and I am having a hard time figuring out why this isn't working.

if($rec=mysql_fetch_array(mysql_query("SELECT * FROM devs WHERE uname='$uname' AND password = '$password'"))){
if(($rec['uname']==$uname)&&($rec['password']==$password)){

echo "<p class=data> <center>Successfully,Logged in<br><br><a href='logout.php'> Log OUT </a><br><br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>";
print "<script>";
print " self.location='welcome.php';"; // Comment this line if you don't want to redirect
print "</script>";

}
}
else {

session_unset();
echo "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct UserName and Password and Try <br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>";

Now, my login page works fine. If I use a good Uname and Password I just get a blank screen, like the If statement is true, but none of the echo's or redirects happen.

If I put in a bad name and password, the else statement works perfect.

I can't figure out what I am missing. Thanks.

ArcadEd
03-21-2006, 11:56 PM
Resolved, dang case sensetivity :)

Brandoe85
03-22-2006, 12:01 AM
Hmm that code seems a bit odd to me. First, you're checking the username and password in your query but, then you're checking it again in that if statement. What I would do is check the username and password in the query (also with some encryption on the password md5() (http://us2.php.net/md5) or sha1() (http://us2.php.net/manual/en/function.sha1.php)) then use mysql_num_rows() (http://us3.php.net/mysql_num_rows) for determining if you have a succesfull login, something along these lines:

$query = 'select fields from table where username = "' . $username . '" and password = "' . $password . '"';
$result = mysql_query($query) or die(mysql_error()); // use mysql error for debugging
$rows = mysql_num_rows($result);
if($rows == 1)
{
// correct login
}
else
{
// login failed
}


Good luck;