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. You will then display the results by making a pie chart with the GD Library.

01
of 05

Making the Database

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

02
of 05

Part One of Making a Voting Script

You start out or script with the information you need to connect to your database. You then name your cookie and define a function called pie. In your pie function, you retrieve the data from your database. You also perform a few calculations that will help you 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. You reference vote_pie.php, which you will create later in the tutorial.

03
of 05

Part Two of Making a Voting Script

The next section of code runs if your 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 your pie function.

04
of 05

Part Three of Making a Voting Script

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.

05
of 05

Part Four of Making a Voting Script

<?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 your script, you called vote_pie.php to display the pie chart of your results. The above code should be placed in the vote_pie.php file. Basically what this does is draw arcs to create a pie. You passed the variables it needed in the link from your main script. To better understand this code, you should read a GD tutorial that covers arcs and pies.

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