Same system this site uses for example. You can vote down or up, but your votes are persistent between logins so you cannot cast duplicate vote, only change your vote to the opposite.

How would one go implement this?

Only thing that I thought of is having an extra field in database row, that would store the IDs of users that have voted separated by common deliminator. So when user votes, he gets added to the end of the list. Something like voted{1, 45, 5} and then iterate over that array when new vote arrive and match it, if it is already in the array, do not count the vote. If it is not, add the vote and add the user ID to the field. But that spirals out of control quickly in terms of iterations on each subsequent vote.

Other thing is storing it in a cookie, but that could be circumvented by the tech savyy users. Or store it in a file and look up that file, but still, you get to iterate over the file just like if you kept it in database.

Is there any other, more elegant, way to do what I want? I tried googling for some solutions, but came empty handed.

  • 2
    Any particular reason why you don't want a votes table? Commented Dec 14, 2016 at 17:50
  • @RichardTingle Yeah, did not THINK of that. I use lot of foreign key constraints already and this is really simple and elegant, guess it is one of those rare brain diarrheas, where you think on solution for hours, while the solution is really easy and you already did it elsewhere.
    – The Law
    Commented Dec 14, 2016 at 17:54

1 Answer 1


You need a Votes table in the database


and an "Upsert" mechanism. That way, if the vote doesn't already exist, you can add it, and if it does, you can change it.

  • See my other comment, probably one of the fastest solved question on SE in history, will give you the karma once I can after the 5 minute grace period, question closed
    – The Law
    Commented Dec 14, 2016 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.