0

我创建了一个投票系统,只有登录的用户可以投票。我有一个表,我在其中存储用户的外键并进行如下投票:

poll_voters
| ref_user_id | ref_poll_id |

当用户投票时,我会检查用户 ID 是否存在于表中的当前投票中。如果是,则用户已经投票。

我想知道是否应该在每次页面加载时都执行此操作,以了解是否显示投票表格或结果(以防用户已经投票)。另一种方法是有一个会话来说明用户是否投票。当他登录时,我当然需要进行检查并设置会话。

你觉得我应该怎么做?

4

5 回答 5

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.

于 2013-01-22T17:46:12.453 回答
1

当您渲染记录时,无论用户是否对投票进行了投票。如果投票,则不显示投票指示器,并且在插入数据库之前检查记录是否存在。根据我的经验,我认为这将是最好的办法。

于 2013-01-22T18:37:15.483 回答
1

您可以将其添加到插入语句

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 )

这样如果每个人每次投票只能投票一次

于 2013-01-22T18:16:33.540 回答
0

如果您有多个活动民意调查,那么您也应该在数据库中存储一个民意调查 ID(您可能不需要更多民意调查,但如果是的话会怎样?)。在这种情况下,我可以在每个页面加载时进行查询,向用户显示已投票或未投票的投票。但是只有最新的民意调查应该显示在每个页面上,并且只查询最新的民意调查......

于 2013-01-22T17:49:53.473 回答
0

最好有一个会话,并在用户登录时检查投票状态。无需在每次页面加载时检查数据库,这将是低效的(尽管如果负载很轻,它仍然可以工作)。

当用户投票时,您还需要刷新页面(或元素,使用 ajax)。

于 2013-01-22T17:40:03.947 回答