Skip to Content
PHPCODE
code Poll and Voting System with PHP and MySQL
php code / September 8, 2018

Step By Step

Step 1 : Created Database and Database Name is demos
Step 2 : Crated Table and SQL QUERY This

CREATE TABLE `poll` (
`pollid` double NOT NULL,
`question` text,
`poll_date` double DEFAULT NULL,
`options` varchar(250) DEFAULT NULL,
`votes` varchar(250) DEFAULT NULL,
`close` tinyint(1) DEFAULT NULL,
`number_options` tinyint(3) DEFAULT NULL,
`poll_timeout` double DEFAULT NULL,
`voters` int(11) DEFAULT NULL,
`public` tinyint(1) DEFAULT NULL,
`last_vote_date` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 3 : Insert SQL QUERY

INSERT INTO `poll` (`pollid`, `question`, `poll_date`, `options`, `votes`, `close`, `number_options`, `poll_timeout`, `voters`, `public`, `last_vote_date`) VALUES
(1, 'Which team has the most wins in all of IPL?', 1524829888, 'Sunrisers||||Kings||||Daredavils||||Nightriders', '0||||0||||0||||0', 0, 4, 0, 0, 0, 1524836731);

Step 4 : Created heade.php File

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- jQuery -->/* Your code... */
Step 5 :Created footer.php File
<div class="insert-post-ads1" style="margin-top:20px;">
</div>
</div>
</body></html>
Step 6 : Created container.php File
</head>
<body class="">
<div role="navigation" class="navbar navbar-default navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a href="https://phpcodeinfomation.blogspot.com/p/php-script.html" class="navbar-brand">PHPCODE</a>
        </div>
        <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="https://phpcodeinfomation.blogspot.com/p/php-script.html">Home</a></li>
</ul>
</div><!--/.nav-collapse -->
      </div>
    </div>
<div class="container" style="min-height:500px;">
<div class=''>
</div>
Step 7 :Created index.php file
<?php
include('header.php');
?>
<title>phpcode: code Poll and Voting System with PHP and MySQL</title>
<style>
ul {
    list-style-type: none;
margin: 0;
    padding: 0;
}
ul li {
    margin-bottom:2px;
    padding:2px;
}
a:hover, a:active, a:focus, a:visited {
    text-decoration: none;
}
</style>
<?php include('container.php');?>
<div class="container">
<h2>Poll and Voting System with PHP and MySQL</h2> 
<?php
include ('Poll.php');
$poll = new Poll();
$pollData = $poll->getPoll(); 
if(isset($_POST['vote'])){
$pollVoteData = array(
'pollid' => $_POST['pollid'],
'pollOptions' => $_POST['options']
);
$isVoted = $poll->updateVote($pollVoteData);
if($isVoted){
setcookie($_POST['pollid'], 1, time()+60*60*24*365); 
$voteMessage = 'Your have voted successfully.';
} else {
$voteMessage = 'Your had already voted.';
}
}
?> 
<div class="poll-container"> 
<?php echo !empty($voteMessage)?'<div class="alert alert-danger"><strong>Warning!</strong> '.$voteMessage.'</div>':''; ?> 
<form action="" method="post" name="pollFrm"> 
<?php
foreach($pollData as $poll){
echo "<h3>".$poll['question']."</h3>"; 
$pollOptions = explode("||||", $poll['options']);
echo "<ul>";
for( $i = 0; $i < count($pollOptions); $i++ ) {
echo '<li><input type="radio" name="options" value="'.$i.'" > '.$pollOptions[$i].'</li>';
}
echo "</ul>";
echo '<input type="hidden" name="pollid" value="'.$poll['pollid'].'">';
echo '<br><input type="submit" name="vote" class="btn btn-primary" value="Vote">';
echo '<a href="results.php?pollID="'.$poll['pollid']."> View Results</a>"; 
}
?> 
</form> 
</div> 
<div style="margin:50px 0px 0px 0px;">
<a class="btn btn-default read-more" style="background:#3399ff;color:white" href="https://phpcodeinfomation.blogspot.com/p/php-script.html">Back</a> 
</div> 
</div>
<?php include('footer.php');?>
Step 8 : Created Poll.php File
<?php
class Poll{
    private $host  = 'localhost';
    private $user  = 'root';
    private $password   = '';
    private $database  = 'demos';
    private $dbConnect = false;
    private $pollTable = 'poll';
    public function __construct(){
        if(!$this->dbConnect){
            $conn = new mysqli($this->host, $this->user, $this->password, $this->database);
            if($conn->connect_error){
                die("Failed to connect with MySQL: " . $conn->connect_error);
            }else{
                $this->dbConnect = $conn;
            }
        }
    }
private function getData($sqlQuery) {
$result = mysqli_query($this->dbConnect, $sqlQuery);
if(!$result){
die('Error in query: '. mysqli_error());
}
$data= array();
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$data[]=$row;
}
return $data;
}
public function getPoll(){
$sqlQuery = 'SELECT pollid, question, options, votes, voters FROM '.$this->pollTable;
        return  $this->getData($sqlQuery);
}
public function getVotes($pollid){
$sqlQuery = 'SELECT votes, voters FROM '.$this->pollTable.' where pollid = '.$pollid;
$result = mysqli_query($this->dbConnect, $sqlQuery);
        return  mysqli_fetch_array($result, MYSQL_ASSOC);
}
public function updateVote($pollVoteData) { 
if(!isset($pollVoteData['pollid']) || isset($_COOKIE[$pollVoteData['pollid']])) {
           return false;
        }
$pollVoteDetails = $this->getVotes($pollVoteData['pollid']);
$votes = explode("||||", $pollVoteDetails['votes']);
$votes[$pollVoteData['pollOptions']] += 1;
implode("||||",$votes);
$pollVoteDetails['voters'] += 1;
$sqlQuery = "UPDATE ".$this->pollTable." set votes = '".implode("||||",$votes)."' , voters = '".$pollVoteDetails['voters']."' where pollid = '".$pollVoteData['pollid']."'";
mysqli_query($this->dbConnect, $sqlQuery);
return true;
}
}
?>
Step 9 : Created results.php File
<?php
include('header.php');
?>
<title>phpcode : Demo Poll and Voting System with PHP, MySQL & jQuery</title>
<?php include('container.php');?>
<div class="container">
<h2>Poll and Voting System with PHP, MySQL & jQuery</h2> 
<?php
include ('Poll.php');
$poll = new Poll();
$pollData = $poll->getPoll();
foreach($pollData as $poll) {
echo "<h3>".$poll['question']."</h3>"; 
$pollOptions = explode("||||", $poll['options']);
$votes = explode("||||", $poll['votes']);
for( $i = 0; $i<count($pollOptions); $i++ ) {
$votePercent = '0%';
if($votes[$i] && $poll['voters']) {
$votePercent = round(($votes[$i]/$poll['voters'])*100);
$votePercent = !empty($votePercent)?$votePercent.'%':'0%'; 
} 
echo '<h5>'.$pollOptions[$i].'</h5>';
echo '<div class="progress">';
echo '<div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width:'.$votePercent.'">'.$votePercent.'</div></div>';
}
}
?> 
<a class="btn btn-default read-more" href="http://localhost/phpinfo/poll-and-voting-system-with-php-and-mysql/">Back to Poll</a> 
</div>
<?php include('footer.php');?>

Step 10 : result Image

 

PHPCODE © 2023