Jump to content

'like' button


dachshund

Recommended Posts

hi,

 

i'm looking to introduce a 'like article' option on my site. much like youtube's thumbs up or facebook's 'like' option.

 

does anyone know how i can make it so each user can only 'like' once? there's no log in, so anyone is able to like. i guess it would involve remembering their ip address? ideally i would like it so a column called LIKE in my database just increases it's number by 1 for that article id when the button is clicked.

 

hope that all made sense?

 

thanks for any help in advance.

Link to comment
https://forums.phpfreaks.com/topic/190402-like-button/
Share on other sites

You will need to make a second table called active_sessions for the following to work, with two columns (ip, date). Next place your query to update the table that you want to affect where my last comment is:

 

<?php
$days = '10';
$ip = $_SERVER['REMOTE_ADDR']; # Get Users IP address
# Delete users from the table if time is greater than $days
mysql_query("DELETE FROM `active_sessions` WHERE `date` < DATE_SUB(NOW(),INTERVAL $days DAY)")or die(mysql_error()); 

# Check to see if the current ip is in the table
$sql = mysql_query("SELECT * FROM active_sessions WHERE ip='$ip'");
$row = mysql_fetch_array($sql);
# If the ip isn't in the table add it.
if(!$row){
mysql_query("INSERT INTO `active_sessions` (`ip`, `date`) VALUES ('$ip'', NOW()) ON DUPLICATE KEY UPDATE `date` = NOW()")or die(mysql_error());
// add your query to add one to the other table
}
?>

Link to comment
https://forums.phpfreaks.com/topic/190402-like-button/#findComment-1004408
Share on other sites

ok cool.

 

so i have

 

<?

$days = '10';
$ip = $_SERVER['REMOTE_ADDR']; # Get Users IP address
# Delete users from the table if time is greater than $days
mysql_query("DELETE FROM `active_sessions` WHERE `date` < DATE_SUB(NOW(),INTERVAL $days DAY)")or die(mysql_error()); 

# Check to see if the current ip is in the table
$sql = mysql_query("SELECT * FROM active_sessions WHERE ip='$ip'");
$row = mysql_fetch_array($sql);
# If the ip isn't in the table add it.
if(!$row){
mysql_query("INSERT INTO `active_sessions` (`ip`, `date`) VALUES ('$ip',' NOW()) ON DUPLICATE KEY UPDATE `date` = NOW()")or die(mysql_error());
// add your query to add one to the other table

$id=mysql_real_escape_string($_GET['id']);
$sql_update = "UPDATE content SET likes=likes+1 WHERE id='$id'";
mysql_query($sql_update) or die (mysql_error());
}
?>

 

but it's saying "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON DUPLICATE KEY UPDATE `date` = NOW()' at line 1"

Link to comment
https://forums.phpfreaks.com/topic/190402-like-button/#findComment-1004462
Share on other sites

Ops, I have an extra quote

 

This:

mysql_query("INSERT INTO `active_sessions` (`ip`, `date`) VALUES ('$ip',' NOW()) ON DUPLICATE KEY UPDATE `date` = NOW()")or die(mysql_error());

 

should be this:

mysql_query("INSERT INTO `active_sessions` (`ip`, `date`) VALUES ('$ip', NOW()) ON DUPLICATE KEY UPDATE `date` = NOW()")or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/190402-like-button/#findComment-1004466
Share on other sites

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.