Jump to content

Database storing the same ip multiple times


mayutaka8985

Recommended Posts

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'];
}

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.