Voting system adds and subtracts 1-5 each time
So, I implemented a voting system
http://ad1987.blogspot.com/2009/02/reddit-style-voting-with-php-mysql-and.htmlon my website. It works perfectly, except for the fact that each time you vote up or vote down it can add anywhere from 1-5 or so votes. The source code is pretty much the same to that on that website, so if you'd like to see it, please check it out there. For a live example:
ht开发者_开发知识库tp://www.campusbasement.com/cornell/entertainment/articles/927/electro-is-taking-over-the-world
Thanks
votes.php: http://pastie.org/1369778
I had a somewhat similar problem. Chrome for some still unknown reason downloaded each page twice, so the views counter was getting +2 each time.
I suppose you have a user table with user_id, and an article table with article_id. So you can implement the check connecting this two tables with many-to-many relation. You should create an intermediate table with fields post_id - the post being voted for, user_id and optionally a value field (if the user decides to change his vote). The first two fields might be a composite primary key.
So when user votes, you search for the row with the same post and user ids. If there are non - you add it.
The problem is that there is no check wether someone has already voted. So you need to either set a cookie (which can fail quite often) or check IP and browser for example... The best thing would be to check the User ID (if it is only possible to vote when logged in).
// EDIT:
I just read in the comments of your first post that the problem is the vote count when voting just once...
精彩评论