Reputation: 113
i have the following php code which selects all the elections which are currently recorded in my mysql database:
<?php
$result = mysql_query("SELECT * FROM elections WHERE status = 'in_progress'")
or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo '<h5>This Election Does Not Exist Anymore</h5><hr> ';
} else {
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name_of_election']. "</td>";
echo "<br/><br/><td>" . $info['date']. ' '. $info['month']. ' ' . $info['year']. "</td>";
echo "<br/><br/><td>" . '<a href="vote.php?election=' . $info['election_id'] . '">Vote Now</a>' . "</td>";
echo '<hr>';
}
}
?>
as you may notice, there is a link at the bottom which says 'vote now'. this allows the user to go and vote. how can i possibly alter this code to show/hide this link depending on whether or not the user has casted a vote.
my database tables look like this:
elections: election_id, name_of_election, date, party1, party2, party3, status.
public: firstname, surname, address, postcode, ni, ref, active.
votes: vote_id, election_id, ni, party
the users logon with their ni and ref number which are all unique. i have a session which registers the users ni number. the casted votes go into the votes table.
i hope this post is much clearer then my other post.
thanks in advance for any help.
Upvotes: 2
Views: 871
Reputation: 847
There's 3 ways you can go about doing this, that come to mind quickly all with there own caveats.
User accounts. Allow users to create accounts and require that they login to vote. Then you can create a table that tracks whether or not users have voted. This way you can enforce that one user account only ever votes once.
sessions and cookies. Set a cookie anytime a user votes and reading that cookie to see if a user has voted and decide if you want to show the link or not. Keep in mind that users can delete there cookies and thereby be able to vote again.
log votes from ips. Treat each ip as a single user and thus keep track of it in the databsae. If an ip has voted already hide the link. Keep in mind users can vote from multiple computers also only one user from a given ip will be able to vote. So a house, company or school may only be able to vote a few times based on the number of ips they have originating.
Seeing as how you have user accounts you'd do a query like check function names and be sure to escape
SELECT COUNT(*) as voted
FROM votes
JOIN elections USING (election_id)
JOIN public using (ni)
WHERE election_id = mysql_escape( $electionId ),
AND public.ni = mysql_escape( $userId );
Then using the result do an:
//User has voted
if ( $result > 0 ){
}
//Else they havn't
else{
}
Upvotes: 3