简体   繁体   English

如何确保用户只按一下我的喜欢按钮?

[英]How do I make sure my like button is pressed only once by user?

Like Button's table 像Button的表一样

  • LIKE_ID (unique like ID for each post) LIKE_ID(每个帖子的ID都是唯一的)
  • LIKES (number of times someone clicks like button) LIKES(有人点击按钮的次数)
  • POST_ID (corresponds to the POST_ID of posts table) POST_ID(对应于posts表的POST_ID)

A separate post table has the POST_ID from above that is unique for each post 一个单独的post表具有上面的POST_ID,对于每个帖子都是唯一的

A separate user table exists for users 用户存在单独的用户表

So when a user clicks the like button, it adds +1 to the Like table where post_id is whatever post they are liking. 因此,当用户单击like按钮时,它会向Like表中添加+1,其中post_id是他们喜欢的任何帖子。

javascript file javascript文件

$(document).ready(function() {
$('img.like_click').click(function() {
var form = $(this).closest('form[name=like_form]');
var lid = $(form).find('input[name=lid]').val();
$.post("like.php?lid='" + lid + "', function(data) {
$(form).find('span.like_count').html(data);
});
});

like.php file like.php文件

$lid = $_GET['lid'];
mysql_query("UPDATE tbl_likes SET likes=likes+1 WHERE like_id=".$lid) or     die(mysql_error());
$result = mysql_query("SELECT likes from files where fileid=" . $id) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['likes'];

I can't figure out how to stop a user from liking over and over. 我无法弄清楚如何阻止用户一遍又一遍地喜欢。 I found facebook style like scripts on the web that stop people from doing this, but they are based on IP address (you can't like my posts if you are not logged in) and those codes were confusing to me as I am not a jquery guy. 我发现网络上的facebook风格就像阻止人们这样做的脚本,但它们基于IP地址(如果你没有登录就不能喜欢我的帖子)这些代码让我感到困惑,因为我不是jquery的家伙。 I'm still trying to figure out how to show the like button properly using the above code, but the hardest part is restricting multiple likes which has stumped me. 我仍然试图弄清楚如何使用上面的代码正确显示类似的按钮,但最困难的部分是限制多个喜欢,这让我很难过。 Anyone can help? 有人可以帮忙吗? Thanks 谢谢

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. 在相似的表中,删除likes列。 We'll calculate that later. 我们稍后会计算出来。 Add a user_id column so you can track which users like which posts. 添加user_id列,以便跟踪哪些用户喜欢哪些帖子。 Then add a combined primary_key on post_id AND user_id to prevent duplicates. 然后在post_iduser_id上添加组合的 primary_key以防止重复。

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保用户仅在我的网页上单击过一次类似“赞”按钮的内容 - how to make sure a user only clicks on something once on my webpage like a “like” button 如何确定某个特定帖子的用户仅点击一次我的投票链接? - How do I make sure that my vote link gets clicked only once by a user for a particular post? 我正在制作一个PHP测验应用程序。 如何确保只问一次问题? - I am making a PHP quiz app. How do I make sure the questions are asked only once? 按下提交按钮时如何更改表单的动作|| 使提交按钮只能单击一次? - how to change the action of a form when the submit button is pressed || make the submit button clickable only once? 如何修复我的代码,以便在按下按钮时它只会增加相应帖子的投票数? - How do I fix my code so that when the button is pressed it will only increment the vote count of the corresponding post? 如何登录用户在帖子上只有一次? - How to make logged in user Like only once on a post? 如何更改我的代码,以便用户只能喜欢该帖子一次? - How can I change my code so that the user can only like the post once? 如何确保我的表单进入PHP数组 - How do I make sure my form comes in an array in PHP 如何确保只有从特定功能重定向的用户才能访问我的网站页面? - How can I make sure that a user visits a page of my site only if he's redirected from a specific function? 如何确保用户每个帖子只能投票一次 - How to make sure users can only vote once per post
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM