Poll and Voting System with PHP and MySQL
发布时间:2026-09-13 | 浏览:2
In this tutorial, we'll develop a secure poll and voting system using PHP and MySQL. This system will allow you to interact with your audience and display a collection of polls. You'll learn to create polls, implement a voting system, delete polls, and display the list of published polls.
A poll and voting system lets people share their opinions on a question by choosing from several answer options. Users select their choice, and the system counts all the votes to show the overall results. This is often used in surveys, market research, and online platforms to gather feedback and understand what people think about various topics.
During poll creation, you can specify multiple answers, as they will be stored in a separate database table. One table will store poll-related data (title, description, etc.), while the other will store the answers, linking both to display the poll list.
The Advanced package includes additional features and a download link to the source code.
Getting Started What You Will Learn in this Tutorial Requirements File Structure and Setup
What You Will Learn in this Tutorial
File Structure and Setup
Creating the Database and setting-up Tables
Creating the Stylesheet (CSS3)
Creating the Poll and Voting System with PHP Functions Index Page Create Page Vote Page Result Page Delete Page
1. Getting Started
Before we jump into programming our poll and voting system, there are a few requirements that need to be met. We need to install the development tools and set up the file structure for our app.
1.1. What You Will Learn in this Tutorial
Form Design — Design a Poll and Voting app with HTML5 and CSS3.
Poll System — Create a working poll system with PHP & MySQL (create polls, delete polls, and view polls).
Voting System — Each poll will consist of answers that the user can select to cast a vote and subsequently view the result.
MySQL Database Interaction — Interact with a MySQL database using the PHP PDO interface. All data entered during the creation phase will be stored in the MySQL database.
Basic Template System — We'll create a basic template system for our app, which will consist of header and footer functions. Those functions can then be implemented on all the pages we create. It's so we don't have to write the same code over and over.
1.2. Requirements
Download and install XAMPP — XAMPP is a web server that includes the essential software for web developers (PHP, MySQL, Apache, etc). Skip this step if you already have a development server installed.
1.3. File Structure & Setup
Navigate to your XAMPP htdocs directory (usually located at C:\xampp\htdocs ) and create the following files and directories:
\-- phppoll |-- functions.php |-- index.php |-- create.php |-- vote.php |-- result.php |-- delete.php |-- style.css
Each file will contain the following:
functions.php — The functions file will contain the template and database connection functions.
index.php — The index page will contain the list of published polls and the navigation buttons.
create.php — The create page will contain form input fields, which we can use to create new polls.
vote.php — The vote page will consist of poll answers with the option to cast a vote.
result.php — The result page will show the results for the specified poll, while each answer will show the number of votes and the percentage bar.
style.css — The stylesheet (CSS3) for our poll and voting system.
2. Creating the Database and setting-up Tables
If you have installed XAMPP, you can create the MySQL database with phpMyAdmin . Although, you need to make sure you start your web server: open the XAMPP control panel and click the Start button for both Apache and MySQL.
Navigate to http://localhost/phpmyadmin/ in your browser.
Click the SQL tab at the top and execute the following SQL statement:
In phpMyAdmin, our database should resemble the following:
A summary of each table and the columns associated with them:
polls table — This table will contain information related to the polls we create (title and description). id — The unique ID for the poll, which will be auto-incremented, meaning the number will increase as more rows are created. title — The title of the poll, which could be a question, etc. description — The description of the poll, which is optional during the creation phase.
id — The unique ID for the poll, which will be auto-incremented, meaning the number will increase as more rows are created.
title — The title of the poll, which could be a question, etc.
description — The description of the poll, which is optional during the creation phase.
poll_answers table — This table will contain all the answers for our created polls. id — Unique ID, which will be auto incremented. poll_id — The poll ID, which will be associated with the id column in the polls table. It's how we can relate both tables. title — The title of the poll answer. votes — The number of votes the answer has.
id — Unique ID, which will be auto incremented.
poll_id — The poll ID, which will be associated with the id column in the polls table. It's how we can relate both tables.
title — The title of the poll answer.
votes — The number of votes the answer has.
To make sure the database has been successfully imported, you can check on phpMyAdmin — click the database name in the left side navigation panel and you should see the following:
3. Creating the Stylesheet (CSS3)
The stylesheet will format the structure of our poll and voting system and make it look more appealing. Add the following CSS code to the style.css file:
Feel free to customize it or use your own stylesheet.
4. Creating the Poll and Voting System with PHP
We can finally start programming our poll and voting system with PHP.
The functions.php file will contain the template and database connection functions, which we can implement in all the pages that we create.
Edit the functions.php file and add:
Every time we want to connect to our MySQL database, all we have to do is execute the above function, like the following:
If you encounter a connection issue with MySQL, you will most likely have to update the database variables to reflect your MySQL credentials and database name. You shouldn't have to change the variables if you're using XAMPP.
The header function for our templates, which includes the head section of the document and the top navigation bar that will appear on every page. We also include the Font Awesome library, which is a free icon library (icons we'll be using in our app).
The template footer function, which is basically the end of the document (closing the HTML tags, etc.).
And now, if we wanted to create a new page, we could implement code like so:
Awesome, right? Now, we don't have to include the same template function code and connection function code in all our PHP files, as all we have to do is execute the function.
Take note the following code:
Is the short version of:
That's all the functions created that we're going to be using.
4.2. Index Page
The index page will consist of all the populated lists of polls, along with buttons that we can utilize to view and delete polls.
Edit the index.php file and add:
The above code will retrieve all the polls and poll answers from our database tables, which we can then subsequently populate a list in HTML table format.
First and foremost, we start the above code by executing the template header function ( template_header() ), and subsequently iterate the polls using a foreach loop and populate them accordingly in an HTML table. Lastly, we end the template with the template footer function ( template_footer() ) that we defined previously.
With the HTML anchor links in the table, you can see we're going to pass the ID parameter (using a GET request) to the vote.php and delete.php pages. It's how the PHP code will know which poll the user has clicked in the table.
Tip To create a custom GET request in PHP, you can append parameters to the PHP file in the URL, for example: contact.php?name=david&email=david@codeshack.io.
If we navigate to our index page, it will resemble the following:
We can now view the list of polls created on the index page. Do not be concerned about the buttons not working, as we have yet to create the pages associated with them.
4.3. Create Page
The create page we can use to create new polls using an HTML form and input fields, and on the server side, we can use PHP and MySQL to insert new records into our database tables. However, for this to happen, we first need to execute a POST request with PHP.
Edit the create.php file and add:
We're going to need to use MySQL and use the template functions that we created previously. Therefore, we must include the functions.php file. The $msg variable will be the output message to the user.
The code above will only execute if the user has clicked the submit button in the HTML form, as it's a POST request. If the POST variable is not empty, insert a new record into both the polls and poll_answers tables — the number of records inserted into the poll_answers table depends on how many answers the user specified.
Not only can we insert new records, but we're also securing the user input as prepared statements will prevent SQL injection. We don't need to escape user input if we're using prepared statements.
Remember the index template we created earlier? We're technically using the same header and footer functions to structure our HTML template.
The form that we have created above we can use to insert new records into our database tables. The PHP POST variable names reflect the names of the elements in the HTML form. The forms method is set to post because we need to make a POST request.
And now, if we click the Create Poll button on the index page, we'll see the following:
That's essentially all we must do to capture the form data and insert the new poll record into our database tables.
On the voting page, users will be able to see the populated list of answers for the specified poll and have the option to vote. They can also see the result without voting.
Edit the vote.php file and add:
For the voting page to work correctly, the id parameter must be specified in the URL (vote.php?id=2, etc). If the id parameter exists, we can retrieve the record from our MySQL database by the id column (poll table) and the poll_id column (poll_answers table).
Not only do we make a GET request, but we also make a POST request — only if the user selects an answer and clicks the Vote button, which will subsequently update the votes for that particular answer (MySQL UPDATE query). Upon a successful POST request, the user is redirected to the result page (result.php).
The template above will iterate each answer and populate the input radio fields we require for our HTML form.
If we select the eye icon next to the test poll on the index page, we'll see the following:
We can now vote on the polls we have created.
4.5. Result Page
On the result page, the user can view the populated list of answers along with the number of votes.
Edit the result.php file and add:
Similar to the voting page, we need to retrieve the ID GET parameter (result.php?id=2, etc.), and with that, we can retrieve the poll results from our database — ordered by the number of votes (descending). The answers are iterated using a foreach loop, and the total votes are counted accordingly.
The above template will iterate the answers and populate them in HTML format along with the number of votes and a percentage bar.
Navigate to the test poll, and you can either select Vote or select the View Results button, which you should subsequently see something like the following:
That's basically how you populate the poll results and using a bit of CSS magic to create the percentage bar.
4.6. Delete Page
On the delete page, we'll be able to delete polls — we'll include a confirmation so the user doesn't accidentally delete the wrong poll.
Edit the delete.php file and add:
If the poll ID is specified and exists in our polls table, we can prompt the user whether they would like to delete the poll or not. If they choose Yes , the poll will be permanently deleted along with the poll answers. The data will be deleted from both the polls and poll_answers database tables using the DELETE statement.
The above code is the template for the delete page. If we navigate to the index page and select the trash icon next to one of our polls, it will resemble the following:
Great job! You have built a simple yet powerful poll and voting system using PHP and MySQL. This system lets users create polls, vote on them, and manage their polls with ease.
Now that you have the basics down, you can add more features. For example, you could add user login to make sure only certain people can create or delete polls. You could also add more options for the polls or make the results update in real-time.
If you enjoyed this tutorial, please share it with your friends or on social media. Your support helps us create more helpful content.
Happy coding, and thanks for reading!
You have the complete tutorial code above, free to use. If you want the production-ready build of this project, or everything on the site at once, here is how they compare.
Advanced Poll and Voting System
The production build of this one project
Instant download after payment
Admin Panel (manage polls, configure settings, and more)
Export & Import Polls (CSV, JSON, etc.)
Approval Feature
Poll Categories
1 Vote Per User Feature
Secure Poll & Voting System
Duplication Checking
Just this project, not the other 15
Ads stay on the page
Multiple Choices Feature
Start & End Dates Feature
Free support (bugs and minor issues)
Start & Expiry Date Feature
Create Poll Page
Delete Poll Page
Update Poll Page
View Poll Results Page
Database SQL File
Responsive Design
NO Code Restrictions
Extra: Basic Source Code
Extra: Tutorial Souce Code
Every package and premium tool, for less than one costs
Billed yearly at $96. Save $48 against monthly.
All 16 packages, worth $360 bought one at a time
Every premium tool on the site
No ads on any page of the site
Members-only tools and articles
Everything we add while you are a member
Cancel from your account in one click
Handy tools for this tutorial
CSV to SQL Converter