In this tutorial, we are going to explore how to create a simple poll system in PHP. This voting system will be purely using PHP without JavaScript client side enhancement. Its purpose is to demonstrate how we can use OOP style coding to create such as system. It is not made for production environment, for a real production ready pool system, you should extend it to use JavaScript to improve its user experience.

Table Of Content

  1. MySQL
  2. PHP Class
  3. Poll Page
  4. Download
  5. The End


1. MySQL

We will need storage to store voter's votes as well as poll's options. In this case, we will use MySQL as our storage. We will have table "options" to keep available poll options, and table "voters" to keep voters' information ( ip in our case). We assume you have experience of using MySQL, so create database tables as below:

Then, for demonstration purpose, I will insert some data into table "options":

2. PHP Class

As I have mentioned earlier, our approach is implement this system is OOP. So let us take a look at the PHP object class "Vote":

Firstly, let us take a look at variables in this class:

  • private $_host: Host url for MySQL connection.
  • private $_database: MySQL database name.
  • private $_dbUser: MySQL database user name.
  • private $_dbPwd: MySQL database user password.
  • private $_con: This stores MySQL connection. And it is created once per object.
  • private $_optionTable: options' table name. This makes life easy if you want to use other name instead of "options".
  • private $_voterTable: voters' table name.This makes life easy if you want to use other name instead of "voters".

This class is very straight forward if you have some PHP coding knowledge.
There four public functions:

  • public function vote($optionId): This function will vote for option with $optionId and it gets voter's ip using $_SERVER['REMOTE_ADDR'].
  • public function getList(): This function get array of available options with id and name fields.
  • public function showResults(): This function runs a left join to get array of options with its voting results.
  • public function getTotal(): This function returns total number of votes for this poll.

3.Poll Page

Poll page is the actual front page where we make sure of Vote class to implement a poll system.

  1. Include "vote.php" file, initialize a vote object and a $action string variable.
  2. Determine if there is action passed to this page, if so, store it to $action variable we have created above.
  3. Depends on the $action value, we will execute two different codes.
  4. If $action equals to "vote", firstly call vote() function of Vote class to vote. $id is passed in using URL.
  5. If $action equals to "vote", secondly call getTotal() function to get total number of results.
  6. If $action equals to "vote", thirdly call showResults() function to get array of the results, and then loop through each element to out put the result list.
  7. If $action does not equal to "vote", call function getList() to get available options and loop through each element to form a vote list, and note we append $id and $action with value "vote" to each URL.

You can download this script from my github account.

5.The End

Thank you for reading this article, and if you have any encountered anything different, have a different solution or think our solution is wrong, do let us know in the comment section.
We will be very happy to hear that.

If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.

You might also want to check out course Introducing PHP from