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 } Quote Link to comment 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 Quote Link to comment 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"; } ?> Quote Link to comment 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. } ?> Quote Link to comment Share on other sites More sharing options...
phpnoobie9 Posted March 25, 2008 Author Share Posted March 25, 2008 Thanks for the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.