Jump to content


Photo

Checking to see if variable exists


  • Please log in to reply
4 replies to this topic

#1 Deadman2

Deadman2
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 October 2005 - 05:31 AM

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!!!

#2 Deadman2

Deadman2
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 October 2005 - 04:19 PM

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????

#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 16 October 2005 - 08:21 PM

mysql_num_rows($result)>0

Where is your query?
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 Deadman2

Deadman2
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 October 2005 - 08:31 PM

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..

#5 Deadman2

Deadman2
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 17 October 2005 - 02:51 AM

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--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users