PHP / MYSQL Voting system for user blog/forum

时间:2017-07-17 15:27:53

标签: php mysql mysqli

I have a blog/forum system which has upvotes/downvote and each comment can also have an upvote/downvote like the reddit/stackoverflow system.

Which is the best way to ensure that a user who has already upvoted can't upvote it again?

I did consider the possibility of making a new row with an seperate upvote table.But if a post has 30 comments then the script would end up doing 30 database queries which can also put a tremendous load on my server. Another possibility would be to make a row for each upvote of user,query them together and check with post_id which is again infeasible.

So which is the best way for upvote/downvote system and what do sites like stumbleupon/reddit do? As in the moment he log's in, the he can see a red color maybe to the posts he has already upvoted

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是为上/下投票单独制作一个表格,您可以在其中保存用户的ID并进行投票或投票,并在评论/帖子/其他内容中包含声誉表。

如果用户尝试进行上/下投票,您只需要查看一条评论(为什么30?)。然后,如果用户已经投票赞成该评论/帖子,您只需在单独的表格中查看。如果是这样,你可以删除投票(例如在SO中 - 如果你按两次投票,你的upvote将被删除),或者如果一个人按下一个不同的按钮(在upvote之后向下投票),你可以改变他的投票。我之所以愿意为您的帖子/评论表添加声誉列并在所有向上和向下投票之后更新它是因为您不需要从该单独的表和您的评论/帖子中获得count(*)的声誉如果你的数据库有很多行,特别是,如果你的数据库有很多行,那么声誉将很容易和快速地接收。