hi,

I am currently making a website and was wondering how to make it so that when
a button is clicked it will update a field in the database by +1. This is sort of a
'like' system for photos so someone can view a photo and then press this button to like it
and this will add 1 like to the 'like' field for that photo in the table.

Examples of this are the 'hype' system on http://lookbook.nu/
and the "good lad" and "shit lad" system on http://truelad.com

I need this 'like' system to create a page that can display the most popular photos and I was planning
on doing this through showing the 10 or so images with the highest amounts of likes in the database.

Any help will be greatly appreciated,

thanks.

Recommended Answers

All 5 Replies

When the button is clicked run a mysql query like:

UPDATE MyTable SET MyColumn=MyColumn+1 Where MyID=123

You would probably want to use JavaScript to load a file with the update code in a hidden div instead of reloading the whole page so it's more seamless for the user.

thank you very much, I have this working now. However, is there any way of doing it
so that once clicked by the user it can't be clicked again? I've been trying to work it by running an if statement and then basing a query on the results of the if statement but it doesn't seem to work.

At the moment I have three tables - one for users, one for image uploads and one for likes. In the like table I was trying to have it so that when the like button is pressed the id of the image and the id of the user and inserted into the like table with a "clicked" value of 1 only if there isn't a previous entry that is the same. Sorry I am really new to this and need some help. Here is my code:

include("config.php");

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
      <title>Home page</title>
      <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css">
</head>
<body>

<?php

include("config.php");

$id_img = mysqli_real_escape_string($link, $_GET['id_img']);
$id_user =  $fgmembersite->UserFullName();
$click = '1';
$clicks = mysqli_fetch_object(mysqli_query($link, "SELECT clicks FROM uploads WHERE id_img=".$id_img.""));
$new_click = $clicks->clicks+1;

$query = mysqli_query("SELECT * from likes");
while($row = mysqli_fetch_array($query))
{
	$likes = $row['id_likes'];
	$img = $row['id_img'];
	$user = $row['id_user'];
	$counted = $row['clicked'];
	
}


$sql = "INSERT into likes (id_img, id_user, clicked) VALUES ('.$id_img.', '$id_user', '1') ON DUPLICATE KEY 
UPDATE uploads SET clicks=".$new_click." WHERE id=".$id_img."";

	if ("!$img=='.$id_img.' && $user!=='$id_user' && $counted!=='1'")
	
	 mysql_query($link, $sql) or die(mysql_error());
	
	else echo "error!";

?>

To keep the user from clicking more than once, I think I would just do:

<?php
...
$count=mysql_num_rows(mysql_query("SELECT * FROM likes WHERE id_img='$id_img' AND id_user='$id_user'"));
if($count==1) //count should either = 1 or 0
{
echo "html for a button you can't click";
}
else echo "current button html";
...
?>

To keep the user from clicking more than once, I think I would just do:

<?php
...
$count=mysql_num_rows(mysql_query("SELECT * FROM likes WHERE id_img='$id_img' AND id_user='$id_user'"));
if($count==1) //count should either = 1 or 0
{
echo "html for a button you can't click";
}
else echo "current button html";
...
?>

thanks, works brilliantly. Thank you everyone

Mark this thread as solved if your problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.