...

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



rnorr002
06-23-2010, 12: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

Keleth
06-23-2010, 12: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.

rnorr002
06-23-2010, 12: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.

Keleth
06-23-2010, 12:52 AM
Oh, then...

Uh...

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

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

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


<?php
//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.
//OR,
var_dump($_POST);

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


$president=($_POST['president']);
$vp=($_POST['vp']);
$tr=($_POST['tr']);
$sect=($_POST['sect']);

if ($president=='other') {
$president=($_POST['pres_txt']);
}
if ($vp=='other') {
$vp=($_POST['vp_txt']);
}
if ($tr=='other') {
$tr=($_POST['tr_txt']);
}
if ($sect=='other') {
$sect=($_POST['sect_txt']);
}

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

Keleth
06-23-2010, 02: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).

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

Keleth
06-23-2010, 02: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum