phpnoobie9 Posted March 25, 2008 Share Posted March 25, 2008 The below logs a users ip to the database. I need help with finding duplicates. If the ip is already in the database I don't want to add the ip again. How would I prevent that...guessing it should go in the else {}. Can I just negate $ipquery in else{}? $ip = $_SERVER['REMOTE_ADDR']; $ipduplicate = mysql_query("SELECT * FROM ip WHERE ip==$ip LIMIT 1"); if ($ip !== $ipduplicate) { $ipquery = mysql_query("INSERT INTO ip (ip) VALUES (\"$ip\")"); } else { do this is duplicate } Link to comment https://forums.phpfreaks.com/topic/97831-php-ip-log-help/ Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 Either make the ip column unique, or perform 2 queries. 1 that selects rows WHERE `ip`=$ip, then check to see if rows are returned. If no rows are returned, perform INSERT Link to comment https://forums.phpfreaks.com/topic/97831-php-ip-log-help/#findComment-500528 Share on other sites More sharing options...
pocobueno1388 Posted March 25, 2008 Share Posted March 25, 2008 You don't use double equal signs in a query. <?php $ip = $_SERVER['REMOTE_ADDR']; $ipduplicate = mysql_query("SELECT COUNT(*) FROM ip WHERE ip='$ip'"); $count = mysql_result($ipduplicate, 0); if ($count <= 0) { $ipquery = mysql_query("INSERT INTO ip (ip) VALUES ('$ip')"); } else { echo "ERROR: Duplicate"; } ?> Link to comment https://forums.phpfreaks.com/topic/97831-php-ip-log-help/#findComment-500529 Share on other sites More sharing options...
soycharliente Posted March 25, 2008 Share Posted March 25, 2008 Make the IP a unique field. Then handle the error that it would throw when trying to add it. OR <?php $ip = $_SERVER['REMOTE_ADDR']; $result = mysql_query("SELECT DISTINCT * FROM `ip`"); if (mysql_num_rows($result) > 0) { $ipquery = mysql_query("INSERT INTO `ip` (`ip`) VALUES ('$ip')"); } else { // the ipquery will not have run. } ?> Link to comment https://forums.phpfreaks.com/topic/97831-php-ip-log-help/#findComment-500531 Share on other sites More sharing options...
phpnoobie9 Posted March 25, 2008 Author Share Posted March 25, 2008 Thanks for the help Link to comment https://forums.phpfreaks.com/topic/97831-php-ip-log-help/#findComment-500711 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.