dean7 Posted April 1, 2014 Share Posted April 1, 2014 Hey guys, I've only recently came back into coding as I lost interest although I've got a news script which users can "Like" the news post. Its only a basic script to refresh my memory. Liking the news post all works although how can I check a way that if that user has already liked that news post that user cannot do it agian? I've got this for liking the post: $Like = keepsafe($_GET['like']);if ($Like){ $UpdateLike = mysql_query("INSERT INTO $NewsLikes (`id`,`newsid`,`likedby`,`time`) VALUES ('','".$Like."','".$Username."','".$date."')") or die (mysql_error());echo "Successfully liked that update!"; $GetThingys = mysql_query("SELECT * FROM $TblNews WHERE `id` = '".$Like."' LIMIT 1") or die (mysql_error());$Thingysgot = mysql_fetch_object($GetThingys); $TheNumber = $Thingysgot->likes;$Addon = "1";$NowNumber = $TheNumber + $Addon;$UpdateNewsLike = mysql_query("UPDATE $TblNews SET `likes` = '".$NowNumber."' WHERE `id` = '".$Like."'") or die (mysql_error()); } MySQL Table: id | newsid | likedby | time Quite obviously newsid is the ID from the news which they clicked "like" on. Likedby is the username who liked it Thanks for anyone that helps Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted April 1, 2014 Share Posted April 1, 2014 Before adding the entry, you could run a query to check if they already liked the page. Just run a search that looks for an entry with the selected news post's ID and the username. If a match isn't found, run your insert query. Additionally, you could replace the "Like" button with something else (maybe an Unlike button) if the visitor already liked the news story they are currently viewing. You would run the same query as described above. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.