Here, we’re going to create a mini voting website where users can vote for their favorite candidates, and the votes count will stored in a database and the total number of votes will be displayed.

We’ll be using php programming language and MySQL database for this tutorial. It is advised to already have a basic knowledge of php before you continue with the article.

Our site will have 3 candidates, candidate A, candidate B and candidate C. The candidates name and their number of votes will be stored in our database table called “Votes-table”. We’ll also set up cookies to stop users from voting more than once. Let’s begin with creating the database.

Let’s use our phpmyadmin too to set up our database. You can access your phpmyadmin on xampp by typing “localhost://phpmyadmin” on your browser address bar. Make sure your Apache and MySQL server is running in your xampp control panel. How to set up a localhost on your computer.

After you’ve opened your phpmyadmin, select create new database and input the database name. We’ll name our database “Charmingdesk” for simplicity. Now we’re going to create a table to store the candidates and their number of votes.

We’ll name our table “voters-table” with three columns, “id“, “candidate”, and “votes”. We now move on to our index.php file to design the user interface.

Creating the voting panel.

In this file, were going to display all the candidates stored in our database and a vote button beside each candidate which allows users to vote them. If they have voted, the cookie is set and it updates the number of votes for that candidate in our database. The vote button also disappears for all the candidates, ensuring that the user only votes once. We’re done with the talks for now, let’s begin the actual practice shall we?


//connecting your index.php file to your database.

$conn= mysqli_connect("localhost", "username", "password", "database name");

//Let's display the candidates.

$query=mysqli_query($conn, "SELECT * FROM votes-table");

if( mysqli_num_rows($query)>0 ){

while ($row= mysqli_fetch_assoc($query )){


$candidate= $row['candidate'];

$votes= $row['votes'];


<div class='card'>

<div class='card-body'>


if(!isset ( $_COOKIE['voted'])){

<button class='btn btn-success' name='votebtn' value='$contestant'> Vote



echo "you've voted already. ";


//the cookie superglobal checks if a cookie has been set in a browser. The cookie is usually set after a user has voted for a candidate.


<sub> vote count: $votes </sub>



Now let’s complete the script to do the actual voting.

if( isset($_GET['votebtn'])){

$voted= $_GET['votebtn'];

//The above gets the candidates name.


//Increase the number of votes by one.

$insert= mysqli_query($conn, "UPDATE votes-table SET votes='$votes' WHERE candidate= 'candidate'");

//Adds the new vote to the database.

setcookie("voted", "$candidate", 64000, "/");

//This sets the cookie if voted.

Here you have it. You’ve built your own voting website. Feel free to add your own custom css to the script. You can Even include bootstrap frame work to make it responsive.

You can follow us on social media to get updates on more “how-to’s ” tutorial. We’ll be building tons of web applications projects here with more detailed notes.

Your favorite web developers.

vic okeke

I'm just a web developer hoping to help out junior developers in algorithms for their projects.


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *