Deadman2 Posted October 16, 2005 Share Posted October 16, 2005 Hey, i need some help with this I need my script to be able to check to see if an ip exists before it inserts it into the mysql database any ideas? heres my script. [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--] [span style=\"color:#0000BB\"]<?php extract [/span][span style=\"color:#007700\"]( [/span][span style=\"color:#0000BB\"]$_POST [/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]mysql_connect [/span][span style=\"color:#007700\"]( [/span][span style=\"color:#DD0000\"]\'host\'[/span][span style=\"color:#007700\"], [/span][span style=\"color:#DD0000\"]\'user\'[/span][span style=\"color:#007700\"], [/span][span style=\"color:#DD0000\"]\'password\' [/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]mysql_select_db[/span][span style=\"color:#007700\"]( [/span][span style=\"color:#DD0000\"]\'database\'[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]mysql_query [/span][span style=\"color:#007700\"]( [/span][span style=\"color:#DD0000\"]\"INSERT INTO banned VALUES ( \'$ip\', \'$date\',\'$reason\' )\" [/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]mysql_close [/span][span style=\"color:#007700\"](); echo [/span][span style=\"color:#DD0000\"]\"IP Added\" [/span][span style=\"color:#0000BB\"]?>[/span] [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] if the ip had allready been added it would say: "IP Address has allready been banned." but if ip has not been enterd befor the script would insert the ip to the database and say: "IP Added." >.< i need some help!!! Quote Link to comment https://forums.phpfreaks.com/topic/2668-checking-to-see-if-variable-exists/ Share on other sites More sharing options...
Deadman2 Posted October 16, 2005 Author Share Posted October 16, 2005 ok ive made an "attempt" to make this work and i keep drawing a blank. [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--] [span style=\"color:#0000BB\"]<?php extract [/span][span style=\"color:#007700\"]( [/span][span style=\"color:#0000BB\"]$_POST [/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]mysql_connect [/span][span style=\"color:#007700\"]( [/span][span style=\"color:#DD0000\"]\'blah\'[/span][span style=\"color:#007700\"], [/span][span style=\"color:#DD0000\"]\'blah\'[/span][span style=\"color:#007700\"], [/span][span style=\"color:#DD0000\"]\'blah\' [/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]mysql_select_db[/span][span style=\"color:#007700\"]( [/span][span style=\"color:#DD0000\"]\'blah\'[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$ip[/span][span style=\"color:#007700\"]=(isset([/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'ip\'[/span][span style=\"color:#007700\"]]))? [/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'ip\'[/span][span style=\"color:#007700\"]]: [/span][span style=\"color:#DD0000\"]\'\'[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$date[/span][span style=\"color:#007700\"]=(isset([/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'date\'[/span][span style=\"color:#007700\"]]))? [/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'date\'[/span][span style=\"color:#007700\"]]: [/span][span style=\"color:#DD0000\"]\'\'[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$reason[/span][span style=\"color:#007700\"]=(isset([/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'reason\'[/span][span style=\"color:#007700\"]]))? [/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'reason\'[/span][span style=\"color:#007700\"]]: [/span][span style=\"color:#DD0000\"]\'\'[/span][span style=\"color:#007700\"]; if (empty([/span][span style=\"color:#0000BB\"]$ip[/span][span style=\"color:#007700\"]) || empty([/span][span style=\"color:#0000BB\"]$date[/span][span style=\"color:#007700\"]) || empty([/span][span style=\"color:#0000BB\"]$time[/span][span style=\"color:#007700\"])){ die([/span][span style=\"color:#DD0000\"]\'Please, fill all fields\'[/span][span style=\"color:#007700\"]); } if ([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"])>[/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) { die([/span][span style=\"color:#DD0000\"]\'This IP Address Is Allready Banned!\'[/span][span style=\"color:#007700\"]); } else { [/span][span style=\"color:#0000BB\"]mysql_query [/span][span style=\"color:#007700\"]( [/span][span style=\"color:#DD0000\"]\"INSERT INTO banned VALUES ( \'$ip\', \'$date\',\'$reason\' )\" [/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]mysql_close [/span][span style=\"color:#007700\"](); echo [/span][span style=\"color:#DD0000\"]\"<center><b>IP Address Added</b></center>\" [/span][span style=\"color:#007700\"]} [/span][span style=\"color:#0000BB\"]?> [/span] [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] what am i doing wrong? can anyone help fix up this code???? Quote Link to comment https://forums.phpfreaks.com/topic/2668-checking-to-see-if-variable-exists/#findComment-8866 Share on other sites More sharing options...
ryanlwh Posted October 16, 2005 Share Posted October 16, 2005 mysql_num_rows($result)>0 Where is your query? Quote Link to comment https://forums.phpfreaks.com/topic/2668-checking-to-see-if-variable-exists/#findComment-8873 Share on other sites More sharing options...
Deadman2 Posted October 16, 2005 Author Share Posted October 16, 2005 here [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--] [span style=\"color:#0000BB\"]<?php extract [/span][span style=\"color:#007700\"]( [/span][span style=\"color:#0000BB\"]$_POST [/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]mysql_connect [/span][span style=\"color:#007700\"]( [/span][span style=\"color:#DD0000\"]\'blah\'[/span][span style=\"color:#007700\"], [/span][span style=\"color:#DD0000\"]\'blah\'[/span][span style=\"color:#007700\"], [/span][span style=\"color:#DD0000\"]\'blah\' [/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]mysql_select_db[/span][span style=\"color:#007700\"]( [/span][span style=\"color:#DD0000\"]\'blah\'[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$resultsearch [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"SELECT ip FROM banned WHERE ip=\'$ip\'\"[/span][span style=\"color:#007700\"]); if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$resultsearch[/span][span style=\"color:#007700\"]) == [/span][span style=\"color:#0000BB\"]1[/span][span style=\"color:#007700\"]) { { echo [/span][span style=\"color:#DD0000\"]\"<center><b>IP Address Allready Banned</b></center>\" [/span][span style=\"color:#007700\"]} else { [/span][span style=\"color:#0000BB\"]$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query [/span][span style=\"color:#007700\"]( [/span][span style=\"color:#DD0000\"]\"INSERT INTO banned VALUES ( \'$ip\', \'$date\',\'$reason\' )\" [/span][span style=\"color:#007700\"]); if([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]!=[/span][span style=\"color:#0000BB\"]FALSE[/span][span style=\"color:#007700\"]) { echo [/span][span style=\"color:#DD0000\"]\"<center><b>IP Address Banned!</b></center>\" [/span][span style=\"color:#007700\"]} [/span][span style=\"color:#0000BB\"]mysql_close [/span][span style=\"color:#007700\"](); [/span][span style=\"color:#0000BB\"]?>[/span] [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] i change it abit.. Quote Link to comment https://forums.phpfreaks.com/topic/2668-checking-to-see-if-variable-exists/#findComment-8874 Share on other sites More sharing options...
Deadman2 Posted October 17, 2005 Author Share Posted October 17, 2005 w00t after some time ive gotten it to work [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--] [span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]include([/span][span style=\"color:#DD0000\"]\'dbconnect.php\'[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]?> [/span]<html> <head> <title>Banning System!</title> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> </head> <body> <form name=\"register\" method=\"post\" action=\"[span style=\"color:#0000BB\"]<?php $php_self ?>[/span]\"> <table width=\"50%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\"> <tr> <td>Ip Address:</td> <td><input name=\"ip\" type=\"text\" id=\"ip\"></td> </tr> <tr> <td>Date:<br> </td> <td><input name=\"date\" type=\"text\" id=\"date\"></td> </tr> <tr> <td>Reason:</td> <td><input name=\"reason\" type=\"text\" id=\"reason\" size=\"38\"></td> </tr> <tr> <td> </td> <td><input name=\"submit\" type=\"submit\" id=\"submit\" value=\"Submit\"></td> </tr> </table> </form> [span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]if(isset([/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'submit\'[/span][span style=\"color:#007700\"]])) { [/span][span style=\"color:#0000BB\"]$ip [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'ip\'[/span][span style=\"color:#007700\"]]); [/span][span style=\"color:#0000BB\"]$date [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'date\'[/span][span style=\"color:#007700\"]]); [/span][span style=\"color:#0000BB\"]$reason [/span][span style=\"color:#007700\"]= ([/span][span style=\"color:#0000BB\"]$_POST[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'reason\'[/span][span style=\"color:#007700\"]]); [/span][span style=\"color:#0000BB\"]$check [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"SELECT * FROM banned WHERE ip=\'$ip\'\"[/span][span style=\"color:#007700\"]); if([/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$check[/span][span style=\"color:#007700\"])==[/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) { echo [/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"](); [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"INSERT INTO banned VALUES (\'$ip\',\'$date\',\'$reason\')\"[/span][span style=\"color:#007700\"]); echo [/span][span style=\"color:#DD0000\"]\'<p>Ip Address Banned!</p>\'[/span][span style=\"color:#007700\"]; } else { echo [/span][span style=\"color:#DD0000\"]\"Sorry, but the ip address, $ip has already been banned.\"[/span][span style=\"color:#007700\"]; } } [/span][span style=\"color:#0000BB\"]?> [/span]</body> </html> [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] Quote Link to comment https://forums.phpfreaks.com/topic/2668-checking-to-see-if-variable-exists/#findComment-8893 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.