Simple PHP & MySQL Poll

engineer at laptop
Hero Images/Getty Images

This tutorial will demonstrate how to make a basic poll using ​PHP and store the results in MySQL. We will then display the results by making a pie chart with the GD Library.

01
of 05

Making the Database

The first thing we must do is create a database. Our example poll will have three options. However, you can modify this to fit your needs.

CREATE TABLE votes (first INTEGER, sec INTEGER, third INTEGER);
INSERT INTO votes (first, sec, third) VALUES (0,0,0) 
02
of 05

Voting Script - Part 1

<?php
// Connects to your Database
mysql_connect("your_server", "your_login", "your_pass") or die(mysql_error());
mysql_select_db("your_database") or die(mysql_error());
//Name of our cookie
$cookie = "Voted";
//A function to display our results - this refrences vote_pie.php which we will also make 
function pie ()
{
$data = mysql_query("SELECT * FROM votes")
or die(mysql_error());
$result = mysql_fetch_array( $data );
$total = $result[first] + $result[sec] + $result[third];
$one = round (360 * $result[first] / $total);
$two = round (360 * $result[sec] / $total);
$per1 = round ($result[first] / $total * 100);
$per2 = round ($result[sec] / $total * 100);
$per3 = round ($result[third] / $total * 100);
echo "<img src=vote_pie.php?one=".$one."&two=".$two."><br>";
Echo "<font color=ff0000>FIRST</font> = $result[first] votes, $per1 %<br>
<font color=0000ff>SECOND </font> = $result[sec] votes, $per2 %<br>
<font color=00ff00>THIRD </font> = $result[third] votes, $per3 %<br>";
}

We start out or script with the information we need to connect to our database. We then name our cookie and define a function called pie. In our pie function, we retrieve the data from our database. We also perform a few calculations that help us display the results in a user-friendly way, such as the percentage each vote has and how many degrees out of 360 that percentage makes up. We reference vote_pie.php, which we will create later in the tutorial.

03
of 05

Voting Script - Part 2

 //This runs if it is in voted mode
if ( $mode=="voted")
{
//makes sure they haven't already voted
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already voted this month<br>";
}
//sets a cookie
else
{
$month = 2592000 + time();
setcookie(Voted, Voted, $month);
// adds their vote to the database
switch ($vote)
{
case 1:
mysql_query ("UPDATE votes SET first = first+1");
break;
case 2:
mysql_query ("UPDATE votes SET sec = sec+1");
break;
case 3:
mysql_query ("UPDATE votes SET third = third+1");
}
//displays the poll results
pie ();
}
} 

The next section of code runs if our voting form has been submitted. It first checks the user to see if they already have a voted cookie. If they do, it does not let them vote again and gives them an error message. However, if they do not, it sets the cookie in their browser and then adds their vote to our database. Finally, it displays the results of the poll by running our pie function.

04
of 05

Voting Script - Part 3

 //if they are not voting, this displays the results if they have already voted
if(isset($_COOKIE[$cookie]))
{
pie ();
}
// or if they have not voted yet, they get the voting box
else
{
if(!$mode=='voted')
{
?>
<form action = "<?php echo $_SERVER[’PHP_SELF’]; ?>" method = "GET">
<select name="vote"> <option value="1">Option 1</option>
<option value="2">Option 2</option> <option value="3">Option 3</option>
</select>
<input type=hidden name=mode value=voted>
<input type=submit>
</form>
<?
}
}
?>

The final part of the script runs if they are not in voting mode. It checks to see if they have a cookie in their browser. If they do, then it knows they have already voted and displays the poll results for them. If there is no cookie, it then checks to make sure they aren't in voted mode. If they are, then nothing happens. But if they are not, it displays the form that lets them vote.

It is a good idea to include this poll on your page using the include function. Then you can place the poll anywhere you want within the page, simply using one line.

INCLUDE 'http://www.yoursite.com/path/to/poll.php' ; 
05
of 05

Using GD Library

<?php 

header('Content-type: image/png'); 
$one = $_GET['one'];
$two = $_GET['two'];
 $slide = $one + $two; 
 $handle = imagecreate(100, 100); 
 $background = imagecolorallocate($handle, 255, 255, 255); 
 $red = imagecolorallocate($handle, 255, 0, 0); 
 $green = imagecolorallocate($handle, 0, 255, 0); 
 $blue = imagecolorallocate($handle, 0, 0, 255); 
 $darkred = imagecolorallocate($handle, 150, 0, 0); 
 $darkblue = imagecolorallocate($handle, 0, 0, 150); 
 $darkgreen = imagecolorallocate($handle, 0, 150, 0); 

 // 3D look
 for ($i = 60; $i > 50; $i--) 
 { 
 imagefilledarc($handle, 50, $i, 100, 50, 0, $one, $darkred, IMG_ARC_PIE); 
 imagefilledarc($handle, 50, $i, 100, 50, $one, $slide , $darkblue, IMG_ARC_PIE); 

if ($slide = 360)
{
 }
else
{
 imagefilledarc($handle, 50, $i, 100, 50, $slide, 360 , $darkgreen, IMG_ARC_PIE); 
 }
}
 imagefilledarc($handle, 50, 50, 100, 50, 0, $one , $red, IMG_ARC_PIE); 
 imagefilledarc($handle, 50, 50, 100, 50, $one, $slide , $blue, IMG_ARC_PIE); 
 if ($slide = 360)
{
 }
else
{
 imagefilledarc($handle, 50, 50, 100, 50, $slide, 360 , $green, IMG_ARC_PIE); 
}
 imagepng($handle);

In our script, we called vote_pie.php to display the pie chart of our results. The above code should be placed in the vote_pie.php file. Basically what this does is draw arcs to create a pie. We passed the variables it needed in the link from our main script. To better understand this code, you should read our GD tutorial that covers arcs and pies.

This entire project can be downloaded from: http://github.com/Goatella/PHPGraphicalPoll

Format
mla apa chicago
Your Citation
Bradley, Angela. "Simple PHP & MySQL Poll." ThoughtCo, Nov. 19, 2017, thoughtco.com/simple-php-and-mysql-poll-2693854. Bradley, Angela. (2017, November 19). Simple PHP & MySQL Poll. Retrieved from https://www.thoughtco.com/simple-php-and-mysql-poll-2693854 Bradley, Angela. "Simple PHP & MySQL Poll." ThoughtCo. https://www.thoughtco.com/simple-php-and-mysql-poll-2693854 (accessed May 27, 2018).