mayutaka8985 Posted December 18, 2010 Share Posted December 18, 2010 Okay I'm a newbie at PHP but was trying to write a counter for my website to count the amount of times a specific button is clicked. My desired outcome is as follows: If the form submit button "like" or "dislike" is clicked, get user's ip and check to see if it already exists in the database. If it exists, don't increment the count (of either button), otherwise log the ip and increment the count (of whichever button is clicked) by 1. Currently I've got the increment working (testing only the like button at the moment) and I've got the ip being stored into the database, but cannot get the comparison to work. Each time the button is clicked it adds the ip address into the database instead of disallowing it. Note: there are two different tables--one storing the ip (counter) and one storing the like clicks (Designs) (dunno if there's a better way?) Excuse my newbieness. The problem area: (like button only): if(isset($_POST['like'])) { $ip_address = $_SERVER['REMOTE_ADDR']; $query = mysql_query("SELECT ip FROM counter WHERE ip = '$ip_address'"); $num_rows = mysql_num_rows($query); if ($num_rows > 0) { die ("You've already clicked the button!"); } else { mysql_query("INSERT INTO counter VALUES ($ip_address)"); mysql_query("UPDATE Designs SET up = up + 1 WHERE ref = 'cataclysm'"); } } $select = mysql_query("SELECT up FROM Designs"); while($row = mysql_fetch_array($select)) { echo $row['up']; } Link to comment https://forums.phpfreaks.com/topic/222062-database-storing-the-same-ip-multiple-times/ Share on other sites More sharing options...
TerryMullins Posted December 19, 2010 Share Posted December 19, 2010 It would be helpful if you can post the entire page of code, less any connection strings that you are using to connect to your database. Link to comment https://forums.phpfreaks.com/topic/222062-database-storing-the-same-ip-multiple-times/#findComment-1149143 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.