Online Voting System in PHP & MySQLi (Tutorial)

If you want to create an online voting system only using PHP & MySQLi then it’s very easy to create. I’ll teach you the process step by step on this page with source code. Voting system or online poll is a kind of contesting system which you can create for comparing the popularity of different candidates from the votes given by the people randomly. There are many built-in scripts out there on the internet for this purpose, but I thought to create one from scratch for PHP web development lovers, who want to learn this thing from scratch. So let’s create this thing in PHP.

online-voting-system-in-php

How to Create Voting System in PHP?

There are two simple steps to take in order to create a voting system in php, first of all you have to create a new database using PHPMyAdmin available in localhost, You can give any name to the new MySQL database, but I’ve given it a name like “mytest“, if you want to use my code then give it the same name, now create a table inside the “mytest” database and name it as “players“, because we want to add three cricket players to this voting system, so people will be able to vote any of the three players. While creating the table “players“, add three columns and click go, see the image below for example:

creating-table-in-database

When you click “go“, You’ll see the setting for the table, simply add column names as sachin, afridi and clark and make the following setting as showing in the picture below:

table-setting-in-mysql-database

So the database & table part is done, now let’s move to the HTML & PHP work which is now the important part of this online voting system, open notepad++ or any text editor which you use for writing codes, and paste the following simple HTML/CSS code into that:

<!DOCTYLE html>
<html>
<head>
<title>Online Voting System</title><style type=”text/css”>body {padding:0; margin:0;}.container {width:1000px; background:skyblue; margin:0 auto; padding:10px;}img {padding:20px; border-radius:100%; border:4px solid white;}

img:hover {background:orange;}

input {margin:30px;}

</style>
</head>

<body>

<div style=”background:black; color:white; text-align:center; width:100%; padding:10px;”>
<h1>Which Player Do You Want to Vote Today?</h2></div>
<div class=”container”>
<form action=”voting.php” method=”post” align=”center”>
<input type=”submit” name=”sachin” value=”Vote For Sachin Tendulkar”/>
<input type=”submit” name=”afridi” value=”Vote For Shahid Afridi”/>
<input type=”submit” name=”clark” value=”Vote For Michael Clark”/>
</form>

</div>
<div style=”background:black; color:white; text-align:center; width:100%; padding:10px;”>
<h1>Created by: Abdul Wali from www.onlinetuting.com</h1></div>
</body>
</html>

After pasting the above code in notepad++, just save the file with the name “voting.php”, and save it the htdocs folder inside XAMPP or WAMP Local Server, now it was all the simple work you need to do in HTML/CSS, Now visit the page by writing localhost/voting.php in your browser, and you should see something like this:

voting-system-in-php-mysql

Now the remaining part is writing the PHP code so that we can insert the votes to the table we earlier created, and we’ll also display the result on the same page dynamically, so here is the simple PHP/MySQLi code which you need to just insert after the closing </form> tag.

<?php
$con = mysqli_connect(“localhost”,”root”,””,”mytest”);if(isset($_POST[‘sachin’])){
$vote_sachin = “update players set sachin=sachin+1”;
$run_sachin = mysqli_query($con, $vote_sachin);
if($run_sachin){
echo “<h2 align=’center’>Your Vote Has Been Cast to Sachin Tendulkar!</h2>”;
echo “<h2 align=’center’><a href=’voting.php?results’>View Results</a></h2>”;
}
}if(isset($_POST[‘afridi’])){
$vote_afridi = “update players set afridi=afridi+1”;
$run_afridi = mysqli_query($con, $vote_afridi);
if($run_afridi){
echo “<h2 align=’center’>Your Vote Has Been Cast to Shahid Afridi!</h2>”;
echo “<h2 align=’center’><a href=’voting.php?results’>View Results</a></h2>”;
}
}if(isset($_POST[‘clark’])){
$vote_clark = “update players set clark=clark+1”;
$run_clark = mysqli_query($con, $vote_clark);
if($run_clark){
echo “<h2 align=’center’>Your Vote Has Been Cast to Michael Clark!</h2>”;
echo “<h2 align=’center’><a href=’voting.php?results’>View Results</a></h2>”;
}
}

if(isset($_GET[‘results’])){

$get_votes = “select * from players”;

$run_votes = mysqli_query($con, $get_votes);

$row_votes = mysqli_fetch_array($run_votes);

$sachin = $row_votes[‘sachin’];
$afridi = $row_votes[‘afridi’];
$clark = $row_votes[‘clark’];

$count = $sachin+$afridi+$clark;

$per_sachin = round($sachin*100/$count) . “%”;
$per_afridi = round($afridi*100/$count) . “%”;
$per_clark = round($clark*100/$count) . “%”;

echo ”
<div style=’background:orange; padding:10px; text-align:center;’>

<center>
<h2>Results So Far:</h2>
<p style=’background:black; color:white; padding:10px; width:500px;’>
<b>Sachin Tendulkar:</b> $sachin ($per_sachin)</p>
<p style=’background:black; color:white; padding:10px; width:500px;’>
<b>Shahid Afridi:</b> $afridi ($per_afridi)</p>
<p style=’background:black; color:white; padding:10px; width:500px;’>
<b>Michael Clark:</b> $clark ($per_clark)
</p>
</center>
</div>
“;
}
?>

In the PHP code, there are only two clauses of code, in the first clause we are updating the table by adding one number to a specific column if someone clicks the vote button which we’ve added in the HTML form,  We’ve added names to the <input> tags as sachin, afridi and clark, so we are saying that in case “sachin” is clicked, we are updating the table and increasing a number in the sachin column in the table and the same process we repeat for other two candidates.

And the second clause of code is showing the results, so we’ve added a dynamic link to the page when someone casts their vote, and by clicking that “View Results” link, we show them the result on the same page, for the results we’ve simply used PHP code to get data from the table and then add a round() to make the extra decimals round.

Download the Source Code & Table

If you have any questions then do ask them in the comment section below, and I’ll also add a video tutorial to this page very soon for more explanation about this tutorial. Happy Web Development!

About Abdul Wali

Abdul Wali is a professional blogger, marketer and web developer working online for last 5 years, he's been deeply involved in PHP, Web Development, SEO and computer video tutorials in different languages i.e English, Urdu and Pashto. You can catch him on and add him on Facebook.

Check Also

web hosting

The Best Web Hosting Providers (Tutorial)

Web hosting is one of the important aspects for establishing a business or website online. If ...

  • Jover Pilare

    Thanks sir it’s really helpful for me sir. can i ask sir what if there is a registered voters and limited votes they can have how can you do that sir. can you do that sir. thanks i’m begging you sir. sorry for bad english.

    • Hi Jover, Thanks for liking this tutorial, you can use $_SESSION for registered users to limit the votes, just search for using $_SESSION in PHP and you’ll find a solution to that, and you can watch my CMS tutorials to understand PHP sessions with more details.

      • Matthew Onyeka

        Good day Mr Abdul Wali, with regards to this voting system, shouldn’t there be a kind of restriction so that people wont vote more than once? How do we solve that?

  • Saddam Akhtar

    Abdul Wail bhi you r Great Person…
    Allah ap ko Bohat Kusyn dy….

  • Dhiny Aldrin

    Warning: Division by zero on line 99, 100, 101

    Please help

  • sowjanya

    Warning: Division by zero in /Applications/AMPPS/www/voting_system/voting.php on line 98

    Warning: Division by zero in /Applications/AMPPS/www/voting_system/voting.php on line 99

    Warning: Division by zero in /Applications/AMPPS/www/voting_system/voting.php on line 100

    • that’s great to know you fixed the issue.

      • Nandhu Krish

        how to solve this warning…. pls help me sir !!!!!!

    • Nandhu Krish

      Warning: Division by zero on line 99, 100, 101

    • Nandhu Krish

      how did you fixed this warning can you pls help me !!!!!!!

  • doesn’t work. Boy you are a phony as they come. Why does it always have to be a hindu that gives out bad codes online.

  • Gabriel “BeSocialWifi” Brunett

    how can I add SESSION or IP address restriction?

  • Chan Myae

    hi. Can I make my mini Thesis with this Tutorial? i love PHP and then i choose online volting system and then found u.plz answer me brother!

  • suresh

    sir what if there is a registered voters and limited votes they can have how can you do that sir.

  • Prakash

    Warning: Division by zero in /Applications/AMPPS/www/voting_system/voting.php on line 98

    Warning: Division by zero in /Applications/AMPPS/www/voting_system/voting.php on line 99

    Warning: Division by zero in /Applications/AMPPS/www/voting_system/voting.php on line 100

    Please help me out.

  • samuel sintayehu

    thank u so Mr Abdul Wali, with regards to this voting system, shouldn’t there be a kind of restriction so that people wont vote more than once? How do we solve that?