I have simple question, I think its not that big of a deal, but I'd still like to ask it as its boggling my mind for past 2 days a lot.
What if I have a poll where instead of inserting answers to MySQL table one per row, it would just change the amount of votes answer got in row with poll data?
So basically my code would just call UPDATE polls SET optionX=optionX+1 WHERE id='$poll_id'
instead of INSERT INTO poll_votes (option, ip, poll) VALUES ('X', '$user_ip', '$poll_id')
- this is simplified version, but base is the same.
My question is, what happens if 1.000 people would submit their answer at same time. Its there any remote chance of server taking data from MySQL where optionX was not updated by previous call yet, and as the previous call (user) updates it, then new one would override it basically loosing that one vote?
TL;DR: Can this happen?
- User 1 sends vote for Option 4
- PHP downloads data from MySQL where Option 4 has 49 votes
- User 2 sends vote for Option 4
- PHP downloads data from MySQL where Option 4 has still 49 votes
- PHP finishes updating request and sets MySQL for Option 4 to have 50 votes
- PHP Finishes updating second request and sets Option to have 50 votes instead of 51
Main concern for me is, that I am storing all options as one array in one cell, so I can allow users to have stupidly long polls without need to have extremely messy MySQL table structure.
I think chances are really thin, but I am not sure if it can happen or if there are some safeties for such things.