Troubles with if else

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
drjay
Forum Newbie
Posts: 2
Joined: Thu Dec 04, 2008 2:54 pm

Troubles with if else

Post by drjay »

I'm trying to check $stringData for the string in $look and if found, add the IP to my banned_ip table and quit. Then, every time a request comes in it will check if the IP is in the banned_ip table and if not, it will add the values into testtable.

I can get it to check the string for $look and add the IP to the banned_ip table perfectly fine. But when I try to have it check the banned_ip before adding to testtable the whole thing fails. Please, I have been working on this for a while and it's my first venture into php. I'm sure my code looks horrible :0

Code: Select all

 
$stringData = $_REQUEST["id"];
$look = "http://www.site.com";
$pos = strpos($look, $stringData);
 
 
if ($pos !== false) 
$sqlCheckForDuplicate = "SELECT * from banned_ip WHERE ip = '". $ip ."'";
$result = mysql_query($sqlCheckForDuplicate) or die(mysql_error());
if(mysql_num_rows($result)>0)
    {
    $feedback[] = '';
    }
else
{
$query = "INSERT INTO banned_ip VALUES ('','$ip')";
mysql_query($query);
mysql_close();
}
 
// BELOW THIS IT DOES NOT WORK
 
$sqlCheckForDuplicate = "SELECT * from banned_ip WHERE ip = '". $ip ."'";
$result = mysql_query($sqlCheckForDuplicate) or die(mysql_error());
if(mysql_num_rows($result)>0)
    {
    $feedback[] = '';
    }
else
{
$query = "INSERT INTO testtable VALUES ('','$ip','$time','$agent','$stringData')";
mysql_query($query);
mysql_close();
 
}
 
 
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Troubles with if else

Post by Reviresco »

I'm not getting this part:

Code: Select all

WHERE ip = '". $ip ."'
Why not just do this:

Code: Select all

WHERE ip = '$ip'
drjay
Forum Newbie
Posts: 2
Joined: Thu Dec 04, 2008 2:54 pm

Re: Troubles with if else

Post by drjay »

Yeah, that's how I originally had it but in looking up bits of code I found someone had done it the other way. I'm out of ideas so I figured it was worth a shot and just left it like that. Seems to work the same either way.
guygk
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 4:06 pm

Re: Troubles with if else

Post by guygk »

Code: Select all

$stringData = (int) $_REQUEST["id"];
$look = 'http://www.site.com';
$pos = strpos($look, $stringData);
 
// this part don't seem to be logic for me
// if you don't want to make query on $pos !== false
// you should take all query process to brackets not only $sqlCheckForDuplicate query
// otherwise your script will just die as on mysql_error()
if ($pos !== false)
$sqlCheckForDuplicate = "SELECT * from `banned_ip` WHERE `ip` = '".$ip."';";
$result = mysql_query($sqlCheckForDuplicate) or die(mysql_error());
if(mysql_num_rows($result)>0) {
 $feedback[] = NULL;
} else {
 $query = "INSERT INTO `banned_ip` VALUES (NULL,'".$ip."');";
 mysql_query($query);
 mysql_close();
}
 
// BELOW THIS IT DOES NOT WORK
 
$sqlCheckForDuplicate = "SELECT * from banned_ip WHERE ip = '". $ip ."'";
$result = mysql_query($sqlCheckForDuplicate) or die(mysql_error());
if(mysql_num_rows($result)>0) {
 $feedback[] = NULL;
} else {
 $query = "INSERT INTO `testtable` VALUES (NULL,'".$ip."','".$time."','".$agent."','".$stringData."');";
 mysql_query($query);
 mysql_close();
}
Post Reply