bob2588 Posted November 4, 2009 Share Posted November 4, 2009 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 More sharing options...
joel24 Posted November 4, 2009 Share Posted November 4, 2009 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); } ?> Link to comment https://forums.phpfreaks.com/topic/180225-adding-ip-to-data-bases/#findComment-950769 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.