PHP poll

The poll will be split into three parts. The first will display the question and answers, the second will allow the user to vote and the third will update the database.

$identifier = '1';
$type = $_GET['type'];

This identifier allows this system to consist of multiple polls at once, each poll will have a unique id and this variable will reference the id. The type variable is used to switch the mode to allow the user to vote.

   $output = mysql_fetch_assoc(mysql_query("SELECT question, date, totalvotes FROM pollquestions WHERE id='$identifier'"));
   echo $output['question'].' ('.date('d-m-y', $output['date']).')<br />';
   $query2 = mysql_query("SELECT votes, choice FROM polloptions WHERE link='$identifier'");
   while ($output2 = mysql_fetch_assoc($query2))
      echo $output2['choice'].' ('.round($output2['votes']/$output['totalvotes']*100, 2).'%)<br />';
   echo '<a href="?type=vote">Vote</a> - Total Votes: '.$output['totalvotes'];

The type variable is checked to see if its empty and if it is then the question and date are taken from the database and output to the user. Using the id variable the related answers to the question are output.

   $result = mysql_fetch_assoc(mysql_query("SELECT totalvotes FROM pollquestions WHERE id = '$identifier'"));
   $totalvotes = $result['totalvotes'] + 1;
   $i = $_POST['choices'];
   $result2 = mysql_fetch_assoc(mysql_query("SELECT votes FROM polloptions WHERE id = '$i' AND link = '$identifier'"));
   $votes = $result2['votes'] + 1;
   mysql_query("UPDATE pollquestions SET totalvotes = '$totalvotes' WHERE id = '$identifier'");
   mysql_query("UPDATE polloptions SET votes = '$votes' WHERE id = '$i'");
   echo 'Thank you for voting. <a href="?">View Poll</a>';

The next if statement checks if the submit button has been pressed and if so then the total number of votes is updated and the number of votes for the answer selected is incremented.

   $output = mysql_fetch_assoc(mysql_query("SELECT question FROM pollquestions WHERE id='$identifier'"));
   echo '<form id="email" name="email" method="post" action="?type=vote">';
   echo $output['question'];
   $query2 = mysql_query("SELECT id, choice FROM polloptions WHERE link = '$identifier'");
   $output2 = mysql_fetch_assoc($query2);
   echo '<br /><input name="choices" type="radio" value="'.$output2['id'].'" checked="checked" />'.$output2['choice'];
   while ($output2 = mysql_fetch_assoc($query2))
      echo '<br /><input name="choices" type="radio" value="'.$output2['id'].'" />'.$output2['choice'];
   echo '<input type="submit" name="submit" id="submit" value="Vote" />';
   echo '</form>';

Finally is the type variable has been set and the submit button has not been pressed then a form is displayed with the question and related answers, with buttons to allow the user to select the appropriate answer.

Adding questions and answers

This tutorial gives a quick introduction to the form and database processing necessary to create a poll. Questions and related answers need to be added manually into the database. To add a question use the code below.

mysql_query("INSERT INTO pollquestions (id, totalvotes, question, date) VALUES ('', '0', '$question', '".time().")");

The $question variable needs to be set to the question to be input into the database. Now that the question has been added it needs to be obtained from the database. When the id is known answers to the question can be set as follows.

mysql_query("INSERT INTO polloptions (id, choice, link, votes) VALUES ('', '$answer1', '$id', '0)");
mysql_query("INSERT INTO polloptions (id, choice, link, votes) VALUES ('', '$answer2', '$id', '0)");
mysql_query("INSERT INTO polloptions (id, choice, link, votes) VALUES ('', '$answer3', '$id', '0)");

Where the variables $answer1, $answer2 and $answer3 are the choices respectively and $id variable is the id of the question in the database.