user1551672
user1551672

Reputation: 119

Voting system using 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?

edit:
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.

Upvotes: 2

Views: 4310

Answers (5)

Freelancer
Freelancer

Reputation: 9064

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

tlbUserVote:

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

tlbCandidate:

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.

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213193

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..)

Upvotes: 3

Ravi Soni
Ravi Soni

Reputation: 2250

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

Upvotes: 0

Deathspike
Deathspike

Reputation: 8770

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.

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39704

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:

database:

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

Upvotes: 1

Related Questions