phpnoobie9 Posted March 29, 2008 Share Posted March 29, 2008 I have a table "banned" which has ips of banned ips. I don't want the banned ip to submit the form...but it doesn't seem to work. I just keep getting my else statement. Not all the code but this is pretty much what it looks like if (isset ($_POST['submit'])) { $ip = $_SERVER['REMOTE_ADDR']; $banquery = mysql_query("SELECT ip FROM banned"); if($ip !== $banquery) {//Check for banned ip not banned do this.... } else { if banned do this.. } } Quote Link to comment https://forums.phpfreaks.com/topic/98535-dont-submit-form-if-ip-banned/ Share on other sites More sharing options...
Psycho Posted March 29, 2008 Share Posted March 29, 2008 You have no WHERE clause on the query and you have to extract the data from the result set (or in this case just do a record count) <?php if (isset ($_POST['submit'])) { $ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']); $banquery = mysql_query("SELECT ip FROM banned WHERE ip = '$ip'"); if(mysql_num_rows($banquery) !== 0) {//Check for banned ip not banned do this.... } else { if banned do this.. } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98535-dont-submit-form-if-ip-banned/#findComment-504294 Share on other sites More sharing options...
phpnoobie9 Posted March 29, 2008 Author Share Posted March 29, 2008 Still doesn't seem to work. Escaping the remote_addr inserts a blank in the table. Quote Link to comment https://forums.phpfreaks.com/topic/98535-dont-submit-form-if-ip-banned/#findComment-504305 Share on other sites More sharing options...
Psycho Posted March 29, 2008 Share Posted March 29, 2008 Still doesn't seem to work. Escaping the remote_addr inserts a blank in the table. There is no INSERT in that code - no way it could insert a blank line. Although, the logic in the IF staemetn needs to be reversed to work correctly if(mysql_num_rows($banquery) == 0) {//Check for banned ip Quote Link to comment https://forums.phpfreaks.com/topic/98535-dont-submit-form-if-ip-banned/#findComment-504314 Share on other sites More sharing options...
phpnoobie9 Posted March 29, 2008 Author Share Posted March 29, 2008 oh that's because I had an insert for the ip.. I'm wondering why it works if it is negate.... if i have a banned ip with !== 0 it does the if statement which should be done if it == 0... if i change it to just ==0 like you said it does the else statement which should be done if ip !==0 It works like I wanted to if I negate it, but looking at the code it should do the opposite..? Quote Link to comment https://forums.phpfreaks.com/topic/98535-dont-submit-form-if-ip-banned/#findComment-504325 Share on other sites More sharing options...
Psycho Posted March 29, 2008 Share Posted March 29, 2008 I don't think you are looking at it correctly. The IF statement was change to use mysql_num_rows(). So if the query finds 1 or more banned records, then mysql_num_rows() will be greater than 0. IF the ip is NOT in the banned records list then the result of mysql_num_rows() will be 0. if(mysql_num_rows($banquery) == 0) {//Check for banned ip //Number of records from banned IP list matching this IP was 0 not banned do this.... } else { //Number of records from banned IP list matching this IP was > 0 //In other words, the ip exists int he banned records list if banned do this.. } Quote Link to comment https://forums.phpfreaks.com/topic/98535-dont-submit-form-if-ip-banned/#findComment-504336 Share on other sites More sharing options...
phpnoobie9 Posted March 29, 2008 Author Share Posted March 29, 2008 I see. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/98535-dont-submit-form-if-ip-banned/#findComment-504337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.