oo7ml — 2012-10-19T05:31:01-04:00 — #1
Hi, my site allows donators (members) to make donations to charity competitions. The donators can also vote in a poll for each charity competition.
When a user makes a donation to a charity competition, they also have the right to submit one vote.
I want to change the system, so that the donators can make as many donations as they like to a charity competition but still only have one vote.
My current database looks like this:
vote_status (submitted / not submitted)
How should i setting this up so that users can submit as many donation as they like per competition but they are still only limited to one vote? Thanks in advance for your help...
guido2004 — 2012-10-19T05:53:22-04:00 — #2
Two tables, one for the donations and one for the votes.
oo7ml — 2012-10-19T09:10:36-04:00 — #3
Ok cool, then just check to see if a record exists in the 'vote' table where the member_id and charity_comp_id and votes status exitst
cups — 2012-10-19T13:30:14-04:00 — #4
You could probably just make member_id and charity_comp_id unique by enforcing a UNIQUE index on that table, any attempt to add another row then throws a particular mysql error message upon whose appearance you can fork your code, generate a warning or log it etc.
That'll save the extra query to see if a record exists every time you get a bone fide vote (ie the vast majority of the time, no?)
kduv — 2012-10-19T15:37:06-04:00 — #5
You could also do an INSERT IGNORE if you're not concerned about showing an error to the user or logging it.
cups — 2012-10-20T06:45:32-04:00 — #6
kduv — 2012-10-20T19:37:55-04:00 — #7
As far as votes go, at least in this situation, I would probably go with the insert ignore option. What would be the point of showing an error? If they've already voted on that one, just let them move on. I wouldn't want to stop the flow of what they're doing (for usability reasons) just to tell them something as simple as "You've already voted for this". I'm sure they won't care enough about that to justify the interruption in their workflow.
As far as resources go... there wouldn't be much difference in performance. You're hitting the DB either way. Either to check if the vote is already there, or to perform an insert that never actually happens.
Another option would be to check if they've voted for that charity already when the user loads the page. Then you could grey-out/disable the voting option so they can't vote. Then they'd already know that they've voted on it before they try to vote again. You can combine that with the insert ignore option on the back end to prevent them from manually going to vote.php?charity_id=1 (or whatever your URL scheme is) and voting again when you've disabled the vote link.
oo7ml — 2012-10-21T09:36:59-04:00 — #8
Excellent, thanks for your detailed input into this...