mysql – Prohibition of repeated voting


Hello. I made a vote and there was a need to prohibit repeated voting. I decided through the user id. To do this, I created a table with two fields user_id and poll_id. That is, when entering a vote, we look for all the id of the current vote in the table, take the id of the current user, and if there is a match – this user was already in this vote, then we forbid him to vote here, if the user id does not match any voting id, then we give vote for him and write down the voting id and user id in the table. Please tell me am I thinking correctly? Or piled up bullshit? And this table must be linked to the user and poll tables, right?


Your decision is correct and adequate. Add a unique composite key to the table (user_id, poll_id), then the database itself will not allow identical entries in the database and therefore multiple votes of the same user in one vote. It is also desirable to add foreign keys to the user and poll tables.

Before voting, make a request for this table using a unique composite key and, if there is an entry, prohibit voting, and this selected entry can be written to the cache so that you do not need to access the table again.

It is possible, but not necessary, for additional information to add there the date and time when the user voted, the voting statistics will be visible here, and in the future you can add various functionality related to limiting voting by time or the number of votes for a certain interval.

Scroll to Top