Forum Moderators: coopster

Message Too Old, No Replies

Help with MySQL query!

         

dreamcatcher

4:12 pm on May 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I`m always getting confused with stuff I shouldn`t be getting confused with. :(

Anyway, I`m writing (well, attempting to write) a guestbook script and I want to log ip addresses in the database, so ipbanning can be switched on or off. To pass the ip address into the database I`m using:

<input type=\"hidden\" name=\"useraddress\" value=\"REMOTE_ADDR\">

Whats the best syntax to check if the ipaddress exists in the database?

What I`m using is:


if ($_REQUEST['signguestbook']){ 
$query = "SELECT ipbanning FROM guestbook";
$result = mysql_query($query) or die ("Error, couldn`t connect to Database");
$row = mysql_fetch_array($result);
if ($row['s_ipbanning']){
$query2 = "SELECT ipaddress FROM guestbook";
$result2 = mysql_query($query2) or die ("Error, couldn`t connect to Database");
$row2 = mysql_fetch_array($result2);
$ipcheck = $row2[0];
if ($useraddress == $ipcheck){
echo "Error, you have already signed the guestbook";
guestbookFooter();
exit;
} else {
return false;
}
}
rest of code.....
}

Does that make any sense? Should "return false" even be there? If I do:


$query2 = "SELECT ipaddress FROM guestbook WHERE $useraddress = '$REMOTE_ADDR'";

Does that work?

Thanks for the help!

:)

dreamcatcher

7:24 pm on May 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I`ve been having a play around with this. I`ve now written a function as follows:

function ipBanning() {

global $REMOTE_ADDR;

$queryipban = "SELECT ipaddress FROM guestbook" or die ("Error, couldn`t connect to Database");
$resqueryipban = mysql_query($queryipban);

while ($ip = mysql_fetch_row($resqueryipban))

{

if ($ip["0"] == $REMOTE_ADDR)

{

echo "Error, you have already signed Guestbook";
guestbookFooter();
exit;

}

}

return 0;

}

It seems to be working ok.

:)

BCMG_Scott

12:47 pm on May 28, 2003 (gmt 0)

10+ Year Member



Looks good :) Welcome to Webmaster World, BTW.

Scott Geiger