View Full Version : voting site. how to keep each user from voting more than once

06-23-2010, 01:22 AM
hey guys i am fairly new to php and need some advice. i have created a login page for a voting site that allows usernames to vote on positions. i have been informed that i need to track who voted on what and allow each user to only vote once. any ideas on how to do this would be great. i have two tables one for users which has id, username, and password. the other that has the voting positions in it. thanks in advance. richard

06-23-2010, 01:31 AM
I actually run a site like this, professionally (bitballot.com but don't mind the home page, I'm running some updates over the next week or two)... to do it, and to do it well, requires a number of tables and a bit of intricacy, specially to do effectively.

You need a table for users, ballots, questions, options, votes, and registration, to start. You need to keep track of what options go to each question, what questions go to each ballot, and which users are allowed to vote for each ballot, and if they have or haven't voted.Take no offense as I will abstain from further answers, as you're my competition :-P

I'll just say that this isn't simple to do well... I've been working on the site for 4 years now.

06-23-2010, 01:38 AM
lol hardly this is a simple site for a teachers union to use once a year for voting. nothing as fancy as what you have kicking. teachers will vote on four positions through radio buttons which is submitted to a database. thats it short and simple.

06-23-2010, 01:52 AM
Oh, then...


Will there be multiple ballots, namely simultaneously, or one ballot at a time/per year?

06-23-2010, 03:04 AM
only one balot per user. once a year

06-23-2010, 03:16 AM
here is the php used to process the votes.

//show all $_POST variables the page is receiving.
echo '<pre>Debugging:'; print_r($_POST); echo '</pre>';
reset($_POST); //print_r() moves the array pointer to the last value, this resets it to number one.

mysql_connect("localhost", "un", "pw") or die(mysql_error());
mysql_select_db("membership") or die(mysql_error());


if ($president=='other') {
if ($vp=='other') {
if ($tr=='other') {
if ($sect=='other') {

mysql_query ("INSERT INTO `data` VALUES ('$president', '$vp', '$tr', '$sect')");
echo $query;
Print "Your vote has been successfully cast.";

06-23-2010, 03:26 AM
Yup, that'll do it in a simple fix... then you can just count after grouping by each candidate and you can get the results. Of course, that assumes you don't want write-ins. If you do, just a bit of cleanup code (trim, strip_html, etc).

06-23-2010, 03:29 AM
how do i add which username voted to the table. the usernames are stored in a different table? foriegn key?

06-23-2010, 03:32 AM
Yah, I'd go with a foreign key referring to the user table.

Ideally, you'd put a list of candidates and no write-ins (or even if you have write-ins) in a diff table and then store the identifiers in a "voted" table, but for your simpler purposes, that's not really necessary.