dachshund Posted January 30, 2010 Share Posted January 30, 2010 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 More sharing options...
The Little Guy Posted January 30, 2010 Share Posted January 30, 2010 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 More sharing options...
dachshund Posted January 30, 2010 Author Share Posted January 30, 2010 great, thanks. and is there anyway to make it so it doesn't have to re-load the page or is that all jquery? Link to comment https://forums.phpfreaks.com/topic/190402-like-button/#findComment-1004422 Share on other sites More sharing options...
The Little Guy Posted January 31, 2010 Share Posted January 31, 2010 and is there anyway to make it so it doesn't have to re-load the page or is that all jquery? That would use some form of AJAX, jquery is one method. Link to comment https://forums.phpfreaks.com/topic/190402-like-button/#findComment-1004448 Share on other sites More sharing options...
dachshund Posted January 31, 2010 Author Share Posted January 31, 2010 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 More sharing options...
The Little Guy Posted January 31, 2010 Share Posted January 31, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.