Jump to content

Recommended Posts

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 :)

 

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

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.

Link to comment
https://forums.phpfreaks.com/topic/287456-like-a-comment/#findComment-1474671
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.