Code:
// Connection stuff here
$username = "root";
$password = "";
$host = "localhost";
$database = "polls";
$connect = mysql_connect($host, $username, $password) or die ( "Could not connect: " . mysql_error() );
$database = mysql_select_db($database, $connect) or die ("Could not select database: " . mysql_error() );
if(!isset($_POST['submit']))
{
// Get all the fields from the poll
$sql = "SELECT * FROM poll";
$resultPoll = mysql_query($sql) or die (mysql_error());
// Find out how many rows there are. The last row will be the most current poll
$pollRows = mysql_num_rows($resultPoll);
/*$sql = "SELECT question FROM poll where id=$pollRows";
$resultAnswers = mysql_query($sql) or die (mysql_error());*/
//Using the $pollRows output the most current poll
echo "<form action='index.php' method='post'>";
echo mysql_result($resultPoll, $pollRows - 1, "question") . "<br />";
echo "<input type='radio' name='question' value='firstAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "firstChoice") . "<br />";
echo "<input type='radio' name='question' value='secondAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "secondChoice") . "<br />";
echo "<input type='radio' name='question' value='thirdAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "thirdChoice") . "<br />";
echo "<input type='radio' name='question' value='fourthAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "fourthChoice") . "<br />";
echo "<input type='submit' name='submit' />";
echo "</form>";
}
else
{
$question = $_POST['question'];
//$pollName = $_POST['pollname'];
$username = "root";
$password = "";
$host = "localhost";
$database = "polls";
$connect = mysql_connect($host, $username, $password) or die ( "Could not connect: " . mysql_error() );
$database = mysql_select_db($database, $connect) or die ("Could not select database: " . mysql_error() );
// Update the answer field before doing calculations
$sql = "UPDATE answers SET $question = $question + 1";
$result = mysql_query($sql) or die (mysql_error());
header("Location: ".$_SERVER['PHP_SELF']."?sub");
exit;
}
if(isset($_GET['sub'])){
$username = "root";
$password = "";
$host = "localhost";
$database = "polls";
$connect = mysql_connect($host, $username, $password) or die ( "Could not connect: " . mysql_error() );
$sql = "SELECT * FROM answers";
$result = mysql_query($sql) or die (mysql_error());
$length = mysql_num_rows($result);
// Get all the numbers and add them together
$first = mysql_result($result, $length - 1, "firstAnswer");
$second = mysql_result($result, $length - 1, "secondAnswer");
$third = mysql_result($result, $length - 1, "thirdAnswer");
$fourth = mysql_result($result, $length - 1, "fourthAnswer");
$sum = $first + $second + $third + $fourth;
$sql = "SELECT * FROM poll";
$result = mysql_query($sql) or die (mysql_error());
// Get the questions to display them for the result
$firstChoice = mysql_result($result, $length - 1, "firstChoice");
$secondChoice = mysql_result($result, $length - 1, "secondChoice");
$thirdChoice = mysql_result($result, $length - 1, "thirdChoice");
$fourthChoice = mysql_result($result, $length - 1, "fourthChoice");
echo "<table>";
echo "<tr><th>Poll Results</th></tr>";
echo "<tr>";
echo "<td>". $firstChoice. "</td><td><span style='color: blue;'>" . number_format((($first/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td>" . $secondChoice . "</td><td><span style='color: blue;'>" . number_format((($second/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td>" . $thirdChoice . "</td><td><span style='color: blue;'>" . number_format((($third/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td>" . $fourthChoice . "</td><td><span style='color: blue;'>" . number_format((($fourth/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td colspan='2'>This poll has been taken <span style='color: blue;'>" . $sum . "</span> times.</td></tr></table>";
}
Bookmarks