Simple Voting System

This tutorial will teach you how to create a simple voting system in PHP. And it has a two user side, the student and the admin, for the student side is were the user vote there candidate while for the admin is the control unit of the system that can edit, update, and see the results or count of all the candidates. This simple voting system is creating a vote candidates and position, and it generates result for all the candidates that the user voted.

Sample Code

Script for the Vote Process
  1. <?php
  2. mysql_connect('localhost','root','');
  3.  
  4. if(isset($_POST['vote']))
  5. {
  6.         $query = mysql_query("SELECT * FROM position ORDER BY position_id ASC") or die(mysql_error());
  7.  
  8.         if(mysql_num_rows($query) > 0)
  9.         {
  10.                 while($row = mysql_fetch_array($query))
  11.                 {
  12.                         $position = $_POST['position_' . $row['position_id']];
  13.                         mysql_query("INSERT INTO votes (candidate_id,student_id) VALUES ('$position','".$_SESSION['student_id']."')") or die(mysql_error());
  14.                 }
  15.         }
  16.         $_SESSION['checkVote'] = 1;
  17.         header("location: home.php");
  18. }
  19. ?>
Result Hope you learn it and enjoy coding. Thanks.

Comments

Add new comment