2012-09-23 08:58
浏览 100

使用PHP + MySql的投票系统?

We have to make a ballot system that makes users vote for various candidates on different positions. There's a login for voters. How do you store a vote of one voter and then get that vote added to the previous votes? The only possible way is to store every vote on a database right? But what would the structure of the database look like? And how do you count it?

The voting system doesnt only have one group of candidates to vote. It has mayor, vice-mayor, senator, etc. There are too many. that's why I'm confused. If only it was just a voting system of a president, it would be easier. So if I have a table for the voter, with a column of his/her voted candidate, it's not possible since the voter votes for many candidates.

图片转代码服务由CSDN问答提供 功能建议

我们必须制定一个投票系统,让用户在不同的位置投票给各种候选人。 选民登录了。 你如何存储一个选民的投票然后将该投票添加到以前的投票中? 唯一可行的方法是对数据库进行每次投票吗? 但是数据库的结构会是什么样子? 你怎么算呢?

投票系统不只有一组候选人投票。 它有市长,副市长,参议员等。有太多。 这就是为什么我很困惑。 如果它只是一个总统的投票系统,那就更容易了。 因此,如果我为选民提供一张桌子,并且有一张他/她的选票候选人,那么由于选民投票给很多候选人,所以这是不可能的。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongle2627 2012-09-23 09:07

    A better way would be to have a different table to store Votes. And that table will have two attributes (VoterId, CandidateId) And you can fetch the Vote Count if you are allowing multiple votes from this table..

    But it would be better to make VoterId a Primary key in this table.. To avoid multiple voting

    CandidateType: - (TypeId(PK), typeName, maxVotePerVoterForThisType)

    Voter Table: - (voterId(PK), voterName, otherInfo)

    Candidate Table: - (candidateId(PK), candidateName, constituency, otherInfo, TypeId(FK))

    Votes:- (voterId(PK, FK), TypeId(PK, FK), candidateId(FK))

    *EDIT:- Schema edited with changed requirement in original post

    *EDIT: - Added a field in CandidateType table to allow multiple votes.(E.g.: Now a voter can vote for 10 Senators, if maxVotePerVoter for this type is set to 10..)

    打赏 评论
  • douji3426 2012-09-23 09:03

    You should e store each candidate in a table, positions in another table then make relations based on ID, the voting system is relatively simple:


    id, position_id, candidate_id, votes

    then PHP

    $query = "UPDATE `votes` SET `votes`=`votes`+1 WHERE `position_id`=1 AND candidate_id=1"; // adds 1 vote where position_id is 1 and candidate_id is 1
    打赏 评论
  • dongshi7433 2012-09-23 09:05

    These 3 tables are required for your accounts (the voters) and candidates.

    Account: Id (PK), Name, Password
    Candidates: Id (PK), Name
    Votes: AccountId (PK), CandidateId (PK)

    Insert a row into votes when a vote is cast. This prevents duplicate voting due to the PK's.

    打赏 评论
  • doumao8355 2012-09-23 09:06

    From the explanation of problem, I can suggest you the following database structure:


    UserID(PK)  |  UserName  |  Vote  |  CandidateId(FK)


    CandidateId(PK)  | CandidateName | TotalVotes

    By this structure you can add the current votes with previous ones (by taking previous votes first and adding it to the current).

    Total votes of the candidates will get updated, too.

    打赏 评论
  • douqi1931 2012-09-23 09:18

    I simple language i can say you are storing voting users in a table called users and you can use a field vaotecount (int) type initilize with zero and track their ip and vote for topic id on which they have voted for and when they vote you can check with IP and topic_id that they are not voting twice and if they passed you can increment the vaotecount field by one

    and while you are counting for votes count the users with topic id for which you counting votes. Simple :D

    打赏 评论

相关推荐 更多相似问题