why isn't this if statement working in php?

Fiveohhh

Diamond Member
Jan 18, 2002
3,776
0
0
In this code I'm trying to get it setup so that if the $uname and $upass are not blank it will execute the if statement otherwise it'll go to the else. right no nomatter what it goes to the if. I echoed the vars in the if statement and they are blank, but it will still follow the if..


<?php
$conn = mysql_connect("localhost", "xxxxxx", "xxxxx");
mysql_select_db("xxxx_login", $conn);
$uname = $_POST['name'];
$upass = $_POST['password'];
if (isset($uname) && isset($upass)) {
echo "-->".$_POST['name']."<----
";
echo "-->".$_POST['password']."<----";
$sql = "select name, password from logins where name = '$uname'";
$result = mysql_query($sql) or die (mysql_error());
extract(mysql_fetch_assoc($result));
if ($password == $_POST["password"]) {
header ("Location: http://www.jza2000.com/");
}} else {

echo "sorry wrong username or password";
}
?>
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
You have two "if" statements there and you don't really say which you're talking about, but I'm guessing the first? You check if the variables exist, not whether they contain any text. Try strlen() instead of isset().
 

Fiveohhh

Diamond Member
Jan 18, 2002
3,776
0
0
yeah i was talking about the first. And thanks for the help strlen worked. so isset just checks to see if there is a var called $var, and not whether there is anything in it or not?
Thanks
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
Originally posted by: Fiveohhh
yeah i was talking about the first. And thanks for the help strlen worked. so isset just checks to see if there is a var called $var, and not whether there is anything in it or not?
Thanks

or use empty()

empty returns true if the variable contains NULL, 0-length, or false.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Fiveohhh
so isset just checks to see if there is a var called $var, and not whether there is anything in it or not?

Yep. IMO you should almost never need to use isset(). empty() would actually be better than strlen() here because strlen() will have to go through the whole string to check its size, which you don't need. Doesn't really make much of a difference unless the string is ridiculously large, but then again there's no point in unneeded processing.