How Do I Make Sure My Like Button Is Pressed Only Once By User?
Like Button's table LIKE_ID (unique like ID for each post) LIKES (number of times someone clicks like button) POST_ID (corresponds to the POST_ID of posts table) A separate pos
Solution 1:
You said that users can't like your posts unless they are logged in. So in your case, you make it very easy for yourself. You just need to track which users liked which posts to prevent duplicates.
In the like table, remove the likes
column. We'll calculate that later. Add a user_id
column so you can track which users like which posts. Then add a combinedprimary_key
on post_id
AND user_id
to prevent duplicates.
Then structure your query like so:
$q = mysql_query("INSERT INTO tbl_likes (user_id, post_id) VALUES ('{$user_id}', {$post_id})");
if( $q === false ) {
// query didn't work, probably a duplicate
}
else {
// return a success etc
}
And if you want to get the number of likes, use a count query like so:
SELECT post_id, count(*) as c FROM tbl_likes WHERE post_id = $post_id;
Post a Comment for "How Do I Make Sure My Like Button Is Pressed Only Once By User?"