Home arrow MySQL arrow Page 3 - Creating a Voting Poll With PHP And MySQL Part 1/2

Creating a Voting Poll With PHP And MySQL Part 1/2

Voting polls provide a level of interactivity in a sense that visitors can express their opinions about particular topics and areas of interest, simply by choosing their response to a question and clicking on a vote button. In this series of two articles, Mitchell's going to show us how to create an interactive voting poll system. It will contain an admin page and a page to vote and view results.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 648
January 15, 2002
  1. · Creating a Voting Poll With PHP And MySQL Part 1/2
  2. · Creating the database
  3. · Adding a poll to the database
  4. · Deleting a poll from the database
  5. · Conclusion

print this article

Creating a Voting Poll With PHP And MySQL Part 1/2 - Adding a poll to the database
(Page 3 of 5 )

To add and delete polls from our MySQL database, we will use one PHP script named managepoll.php. The script is available as part of the support material for this article from the last page. Let's take a look at some of the more important parts of the script.

The script is divided up into five different functions: GetNewPollDetails, AddPoll, GetPollToDelete, DeletePoll and GetChoice. We use a switch variable to determine the value of the "method" get variable:



case "ShowAdd":



case "AddFinal":



case "ShowDelete":



case "DeleteFinal":






To pass a value for the get "method" variable, we would append it to our URL as the query string, like this: http://myserver/managepoll.php?method=ShowAdd.

As you can see in our switch statement above, we are explicitly referencing the "method" variable as part of the HTTP_GET_VARS array. We don't have to do this, however, because PHP automatically creates variables for any values that we pass in from either a form or querystring, so the variable $method is automatically created for us. I have chosen to explicitly reference the "method" variable as part of the array simply to show that you can.

This would call the GetNewPollDetails function for example. By default, when we load managepoll.php in our web browser, we aren't passing any get variable named "method". Our URL would be something like http://myserver/managepoll.php, thus having no "method" get variable specified. Because of this, our PHP script would use the default option of the switch control and call the GetChoice function, which displays a simple navigational menu in our browser:

The navigational menu in managepoll.php

Let's take look at how we add a poll to our database. By clicking on the "Add A New Poll Question", we are hyper linking to managepoll.php?method=ShowAdd, which calls the GetNewPollDetails function. The GetNewPollDetails function simply displays a HTML form containing five fields: one for the polls questions, and one for each of the answers that the poll can contain:

Adding a new poll via a HTML form

The <form> tag of our HTML page tacks the "method" variable along with its value onto the end of the URL in the "action" attribute, like this:

<form name="frmAddPoll" action="managepoll.php?method=AddFinal" method="post">

When the new poll form is submitted, our PHP script picks up the value of the "method" variable, and using the switch control it calls the AddPoll function. The AddPoll function actually adds the details of the newly created poll to the database. It also includes some error checking to make sure that at least two answers were added for the poll, because what good is a poll with just one answer?

The AddPoll function starts by obtaining global references to the values passed in from the HTML form containing the new poll details:

global $question;

global $answer1;

global $answer2;

global $answer3;

global $answer4;

global $answer5;

We have to register the form values as global, because we are accessing them from within a function that has no knowledge of any variables outside of its scope. Next, the AddPoll function calculates how many answers were provided for our new poll. If there were less than two, we inform the user that they need to enter at least two answers for this poll:


// Make sure the poll has a question and that at least two

// answer options were provided

$numAnswers = 0;

$err = "";

if($answer1 != "")

{ $numAnswers++; }

if($answer2 != "")

{ $numAnswers++; }

if($answer3 != "")

{ $numAnswers++; }

if($answer4 != "")

{ $numAnswers++; }

if($answer5 != "")

{ $numAnswers++; }

if($question == "")

$err .= "<li>You didn't enter a title</li>";

if($numAnswers < 2)

$err .= "<li>You must enter at least two answer choices</li>";

if($err != "")



<h1>Incomplete Fields</h1>

You didn't complete all of the details for this poll.

Take a look at the errors below and click the link

below to go back and correct them:


<?php echo $err; ?>


<a href="javascript:history.go(-1)">Go Back</a>




Note that we don't used the isset function to check whether or not the form values are set because the variables will always exists, but they may just contain an empty ("") value.

Next, the AddPoll function connects to our MySQL database that we created earlier, calling the die function to terminate if any errors occurred:

@$svrConn = mysql_connect($dbServer, $dbUser, $dbPass) or die("Couldn't connect to the database server");

@$dbConn = mysql_select_db("polls", $svrConn) or die("Couldn't connect to the database");

Notice how we're using three variables to represent the database server, user id and password when we call the mysql_connect function? These are created in a separate file, dbvars.php, which is included into our managepoll.php file using the include function, like this:


If you download the support material for this article, then you should change the database login details stored in dbvars.php to match yours.

Once we're connected to the database, we execute an insert statement against the pollQuestions table:

$strQuery = "INSERT INTO pollQuestions VALUES(";

$strQuery .= "0, '$question', '$answer1', '$answer2', '$answer3', '$answer4', '$answer5')";




If the mysql_query function returns a true value (indicating that the details of the new poll were added successfully), then we tell the user that the poll was added successfully and link them back to the main menu:

The confirmation screen once a poll has been created
blog comments powered by Disqus

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials