Site icon WDB24

Online Voting System with PHP & MySQL

PHP

Online voting system is one of the best way to get user opinion about any topic. Now a days, most of the news and sports sites usually have this system.

So in this tutorial I am going create an online voting system using PHP and MySQL with IP restriction. I will use the example of top football player.


Voting System with PHP & MySQL:

This system will have 3 files which are:

Create Database:

Create Table:

Config.php:

Above code has PHP PDO connection in try and catch block. I also set an attribute for error exception.

Functions.php:

As you can see in above code I have written 4 functions. Each function have global $pdo that will get the value (in our case db object) of $pdo from global namespace. getAllResults() returns total count of vote. getResultByName() functions returns count of specific footballer. getSinglePlayerData() returns the percentage of vote against specific player and ifIPIsExist checks the IP address of current user. If current already vote with current IP then it will return 1 otherwise empty.

Index.php

First of all index file include config.php and functions.php. After that there is vote form submit logic with error and success message. I have used $_SERVER['REMOTE_ADDR'] to get current user IP address and match with database IPs.

Note: $_SERVER[‘REMOTE_ADDR’] sometimes may not return correct IP address.


Exit mobile version