0

I've created a poll system, only logged in users may vote. I have a table where I store a foreign key to users and polls like this:

poll_voters
| ref_user_id | ref_poll_id |

When a user votes I check if the user id exists with the current poll in the table. If it does, the user has already voted.

I'm wondering if I should do this on every page load to know whether to display vote forms, or the result (in case the user already has voted). Another way would be to have a session that says if the user has voted or not. And when he log in I would need to do the check and set the session of course.

What do you think I should do?

lawls
  • 1,498
  • 3
  • 19
  • 34
  • Hope this might help:: http://stackoverflow.com/questions/10480568/comma-separated-values-in-mysql-in-clause – Sashi Kant Jan 22 '13 at 17:36
  • @lawis so you have two tables, `users` and `poll_votes` yeah or this user is coming from a session? – bonCodigo Jan 22 '13 at 17:40
  • 1
    How are you currently displaying "vote forms" -- are these forms db driven? If so, can you just add the answers with the questions -- they would be null if the user hasn't answered? – sgeddes Jan 22 '13 at 17:45

5 Answers5

1

I would recommend that you render all the HTML without the vote indicator, and then after the page has loaded in the browser perform an AJAX call to get the vote status of items.

This allows you to cache the page on the browser or statically on the server, but still support dynamic updates. So that if the user visits the page again, and they have changed the vote status that it's updated.

Otherwise, you have to always re-render all the HTML pages differently for each user's session.

Reactgular
  • 52,335
  • 19
  • 158
  • 208
1

You could add it to the insert statement

if not exists(select ref_user_id 
    from poll_voters 
    where ref_user_id =@ref_user_id and ref_poll_id = @ref_poll_id)
insert into poll_voters (ref_user_id ,ref_poll_id )
values (@ref_user_id ,@ref_poll_id )

that way if is only possible for each person to vote once per poll

Lance
  • 3,193
  • 2
  • 32
  • 49
1

when u r rendering the records fetch a bit whether user had voted on a poll or not.If voted then don't show the vote indicator, and before inserting into database check whether records exists or not.From my experience I think this will be the best way.

Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
0

It would be best to have a session, and check the voted status when the user logs in. There's no need to check against the database on every page load, that would be inefficient (although it would still work if the load is light).

You'd also need to refresh the page (or element, using ajax) when the user votes.

NickJ
  • 9,380
  • 9
  • 51
  • 74
  • Session is not a great solution to this problem. Going to the database is the right way, and if you'd like you can implement a caching solution. – The Muffin Man Jan 22 '13 at 17:56
  • @Nick Can you please explain why it is not a great solution? – lawls Jan 22 '13 at 19:24
  • You would go to the database when the user logs in, and store the result in the session. That is more efficient than calling the database on every page load. – NickJ Jan 23 '13 at 14:50
0

If you have more than one active poll than you should store a poll ID in DB too (May you no need for more poll but what will if yes?). In this case I may do a query every page load which show the user the voted or unvoted poll. But only the latest poll should show on every page and do the query only the latest poll...

Gery
  • 147
  • 2
  • 10