Jump to content

Adding ip to data bases


bob2588

Recommended Posts

hello i am writing  a script that will add every visitor form my site ip to an data base but there is a problem one it should check to see if the ip has been added today and the second it add the ip twice to the data bases when it should only be add it once here is the code

 

<?php
$ip = $_SERVER['REMOTE_ADDR'];  
$date = date('M,d,Y');
$host = "localhost"; //database location
$user = "user"; //database username
$pass = "pass"; //database password
$db_name = "ip"; //database name
//database connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);
$check = mysql_query("SELECT * FROM ip WHERE ip='$ip'");
while($row = mysql_fetch_array($check));
if ($row['ip']=="$ip" & $row['date']=="$date") {
mysql_close($link);
}
else
{
mysql_query("INSERT INTO ip (id, ip, date)
VALUES ( NULL, '$ip', '$date' )")
or die(mysql_error());
mysql_close($link);
}
?>

 

Thank for the help

 

bob

Link to comment
https://forums.phpfreaks.com/topic/180225-adding-ip-to-data-bases/
Share on other sites

you've got while infront of the mysql_fetch_array but theres no commands being executed within that while loop... its being ended by the semi-colon straight after it...

 

use this if theres only one row per IP...

<?php
$ip = $_SERVER['REMOTE_ADDR'];  
$date = date('M,d,Y');
$host = "localhost"; //database location
$user = "user"; //database username
$pass = "pass"; //database password
$db_name = "ip"; //database name
//database connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);
$check = mysql_query("SELECT * FROM ip WHERE ip='$ip'");
$row = mysql_fetch_array($check);
if ($row['ip']=="$ip" & $row['date']=="$date") {
mysql_close($link);
}
else
{
mysql_query("INSERT INTO ip (id, ip, date)
VALUES ( NULL, '$ip', '$date' )")
or die(mysql_error());
mysql_close($link);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.