Simple Polling System using PHP/MySQLi with Source Code

This PHP Polling System written in PHP and MySQL. We will teach how to create this system for your schools, business, etc. This system can vote and counts the result for each participants or candidates in the given data by the admin. Each user has a account and can create a new account if they want to vote. The users can also manage their profiles if they want. And for the admin side the admin only can view the total poll of records in each candidates if who are the winner or who has a higher votes that the users chooses. See the example code below.

Sample Code

login.html - This is for the html form for login script for the UI.
  1. <!DOCTYPE html>
  2. <title>PHP Polling System</title>
  3. <link href="css/user_styles.css" rel="stylesheet" type="text/css" />
  4. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  5. <script language="JavaScript" src="js/user.js"></script>
  6. </head>
  7. <body bgcolor="tan">
  8. <center><b><font color = "brown" size="6">PHP Polling System</font></b></center>
  9. <div id="page">
  10. <div id="header">
  11. <h1>Student Login</h1><hr/>
  12. </div>
  13. <div id="container">
  14. <form name="form1" method="post" action="checklogin.php" onsubmit="return loginValidate(this)">
  15. <div class="form-group">
  16. <label for="email">Email address:</label>
  17. <input type="email" name="myusername" class="form-control" id="myusername">
  18. </div>
  19. <div class="form-group">
  20. <label for="pwd">Password:</label>
  21. <input type="password" name="mypassword" class="form-control" id="mypassword">
  22. </div>
  23. <div class="checkbox">
  24. <label><input type="checkbox"> Remember me</label>
  25. </div>
  26. <button type="submit" name="Submit" class="btn btn-default">Login</button>
  27. </form>
  28. <br>Not yet registered? <a href="registeracc.php"><b>Register Here</b></a>
  29. </div>
  30. <div id="footer">
  31. <div class="bottom_addr">Sourcecodester &copy; @2016</div>
  32. </div>
  33. </div>
  34. </body>
  35. </html>
checklogin.php - This file is for the checking of the users' login if the user is existing in the database.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>PHP Polling System</title>
  5. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  6. <link href="css/user_styles.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body bgcolor="tan">
  9. <center><b><font color="#000" size="6">PHP Polling System</font></b></center><br><br>
  10. <body>
  11. <div id="page">
  12. <div id="header">
  13. <h1>Invalid Credentials Provided </h1>
  14. <p align="center">&nbsp;</p>
  15. </div>
  16. <div id="container">
  17. <?php
  18. ini_set ("display_errors", "1");
  19. $host="localhost";
  20. $username="root";
  21. $password="";
  22. $db_name="poll";
  23. $tbl_name="tbMembers";
  24.  
  25. $conn = mysqli_connect("$host", "$username", "$password")or die("cannot connect");
  26. mysqli_select_db($conn,"$db_name")or die("cannot select DB");
  27. $myusername=$_POST['myusername'];
  28. $mypassword=$_POST['mypassword'];
  29. $encrypted_mypassword=md5($mypassword);
  30. $myusername = stripslashes($myusername);
  31. $mypassword = stripslashes($mypassword);
  32. $myusername = mysqli_real_escape_string($conn,$myusername);
  33. $mypassword = mysqli_real_escape_string($conn,$mypassword);
  34. $sql="SELECT * FROM $tbl_name WHERE email='$myusername' and password='$encrypted_mypassword'" or die(mysqli_error($conn));
  35. $result=mysqli_query($conn,$sql) or die(mysqli_error($conn));
  36. $count=mysqli_num_rows($result);
  37.  
  38. if($count==1){
  39. $user = mysqli_fetch_assoc($result);
  40. $_SESSION['member_id'] = $user['member_id'];
  41. header("location:student.php");
  42. }
  43. else {
  44. echo "Wrong Username or Password<br><br>Return to <a href=\"login.html\">login</a>";
  45. }
  46. ?>
  47. </div>
  48. <div id="footer">
  49. <div class="bottom_addr">Sourcecodester &copy; @2016</div>
  50. </div>
  51. </div>
  52. </body>
  53. </html>
vote.php - This script is for the counting of votes that every user clicks the radio button and submits.
  1.  
  2. <?php
  3. <?php
  4. $conn = mysqli_connect('localhost', 'root', '') or die("Connection Failed");
  5. mysqli_select_db($conn,'poll') or die(mysqli_error($conn));
  6.  
  7. if(empty($_SESSION['member_id'])){
  8. header("location:access-denied.php");
  9. }
  10. ?>
  11. <?php
  12. $positions=mysqli_query($conn,"SELECT * FROM tbPositions")
  13. or die("There are no records to display ... \n" . mysqli_error($conn));
  14. ?>
  15. <?php
  16. if (isset($_POST['Submit']))
  17. {
  18. $position = addslashes( $_POST['position'] );
  19. $result = mysqli_query($conn,"SELECT * FROM tbCandidates WHERE candidate_position='$position'")
  20. or die(" There are no records at the moment ... \n");
  21. }
  22. else
  23. ?>
  24. <!DOCTYPE html>
  25. <html>
  26. <head>
  27. <title>PHP Polling System</title>
  28. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  29. <link href="css/user_styles.css" rel="stylesheet" type="text/css" />
  30. <script language="JavaScript" src="js/user.js"></script>
  31. <script language="JavaScript" src="js/jquery-1.2.6.min.js"></script>
  32. <script type="text/javascript">
  33. function getVote(int)
  34. {
  35. if (window.XMLHttpRequest)
  36. {
  37. xmlhttp=new XMLHttpRequest();
  38. }
  39. else
  40. {
  41. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  42. }
  43.  
  44. xmlhttp.open("GET","save.php?vote="+int,true);
  45. xmlhttp.send();
  46. }
  47.  
  48. function getPosition(String)
  49. {
  50. if (window.XMLHttpRequest)
  51. {
  52. xmlhttp=new XMLHttpRequest();
  53. }
  54. else
  55. {
  56. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  57. }
  58.  
  59. xmlhttp.open("GET","vote.php?position="+String,true);
  60. xmlhttp.send();
  61. }
  62. </script>
  63. <script type="text/javascript">
  64. $(document).ready(function(){
  65. var $ = jQuery.noConflict();
  66. $(document).ready(function()
  67. {
  68. // setInterval(function(i){
  69. // $.ajax({
  70. // url: "admin/refresh.php",
  71. // cache: false,
  72. // success: function(html){
  73. // $(".refresh").html(html);
  74. // }
  75. // })
  76. // },1000)
  77.  
  78. });
  79. $('.refresh').css({color:"green"});
  80. });
  81. </script>
  82. </head>
  83. <body bgcolor="tan">
  84. <center><b><font color="#000" size="6">PHP Polling System</font></b></center><br><br>
  85. <div id="page">
  86. <div id="header">
  87. <h1>CURRENT POLLS</h1><hr/>
  88. <ul class="nav navbar-nav"><li><a href="student.php"><h1>Home</h1></a></li><li><a href="vote.php"><h1>Current Polls</h1></a></li><li><a href="manage-profile.php"><h1>Manage My Profile</h1></a></li><li><a href="logout.php"><h1>Logout</h1></a></li></ul>
  89. </div>
  90. <div class="refresh"></div>
  91. <div id="container">
  92. <table width="420" align="center">
  93. <form name="fmNames" id="fmNames" method="post" action="vote.php" onsubmit="return positionValidate(this)">
  94. <tr>
  95. <td>Choose Position</td>
  96. <td><SELECT NAME="position" id="position" onclick="getPosition(this.value)">
  97. <OPTION VALUE="select">select
  98. <?php
  99. while ($row=mysqli_fetch_array($positions)){
  100. echo "<OPTION ".( isset($_POST['position'] ) && $row['position_name'] == $_POST['position'] ? 'selected' : '').">$row[position_name]";
  101. }
  102. ?>
  103. </SELECT></td>
  104. <td><input type="submit" name="Submit" value="See Candidates" /></td>
  105. </tr>
  106. </form>
  107. </table>
  108. <table width="270" align="center">
  109. <form>
  110. <tr>
  111. <th>Candidates:</th>
  112. </tr>
  113. <?php
  114. if (isset($_POST['Submit'])){
  115. while ($row=mysqli_fetch_array($result)){
  116. echo "<tr>";
  117. echo "<td>" . $row['candidate_name']."</td>";
  118. echo "<td><input type='radio' name='vote' value='$row[candidate_name]' onclick='getVote(this.value)' /></td>";
  119. echo "</tr>";
  120. }
  121. mysqli_close($link);
  122. }
  123. else
  124. ?>
  125. </form>
  126. </table>
  127. </div>
  128. <div id="footer">
  129. <div class="bottom_addr">Sourcecodester &copy; @2016</div>
  130. </div>
  131. </body>
  132. </html>
manage-profile.php - And for the managing of each users account.
  1. <?php
  2. $conn = mysqli_connect('localhost', 'root', '') or die("Connection Failed");
  3. mysqli_select_db($conn,'poll') or die(mysqli_error($conn));
  4.  
  5. if(empty($_SESSION['member_id'])){
  6. header("location:access-denied.php");
  7. }
  8. $result=mysqli_query($conn,"SELECT * FROM tbMembers WHERE member_id = '$_SESSION[member_id]'")
  9. or die("There are no records to display ... \n" . mysqli_error($conn));
  10. if (mysqli_num_rows($result)<1){
  11. $result = null;
  12. }
  13. $row = mysqli_fetch_array($result);
  14. if($row)
  15. {
  16. $stdId = $row['member_id'];
  17. $firstName = $row['first_name'];
  18. $lastName = $row['last_name'];
  19. $email = $row['email'];
  20. }
  21. ?>
  22. <?php
  23. if (isset($_POST['update'])){
  24. $myId = addslashes($_GET['id']);
  25. $myFirstName = addslashes( $_POST['firstname'] );
  26. $myLastName = addslashes( $_POST['lastname'] );
  27. $myEmail = $_POST['email'];
  28. $myPassword = $_POST['password'];
  29. $newpass = md5($myPassword);
  30. $sql = mysqli_query($conn, "UPDATE tbMembers SET first_name='$myFirstName', last_name='$myLastName', email='$myEmail', password='$newpass' WHERE member_id = '$myId'" )
  31. or die( mysqli_error($conn) );
  32. header("Location: manage-profile.php");
  33. }
  34. ?>
  35. <!DOCTYPE html>
  36. <html>
  37. <head>
  38. <title>PHP Polling System</title>
  39. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  40. <link href="css/user_styles.css" rel="stylesheet" type="text/css" />
  41. <script language="JavaScript" src="js/user.js"></script>
  42. </head>
  43. <body bgcolor="tan">
  44. <center><b><font color="#000" size="6">Simple PHP Polling System</font></b></center><br><br>
  45. <div id="page">
  46. <div id="header">
  47. <h1>MANAGE MY PROFILE</h1><hr/>
  48. <ul class="nav navbar-nav"><li><a href="student.php"><h1>Home</h1></a></li><li><a href="vote.php"><h1>Current Polls</h1></a></li><li><a href="manage-profile.php"><h1>Manage My Profile</h1></a></li><li><a href="logout.php"><h1>Logout</h1></a></li></ul>
  49. </div>
  50. <div id="container">
  51. <td>
  52. <table width="380" align="center">
  53. <CAPTION><h3>MY PROFILE</h3></CAPTION>
  54. <tr>
  55. <td>Student Id:</td>
  56. <td><?php echo $stdId; ?></td>
  57. </tr>
  58. <tr>
  59. <td>First Name:</td>
  60. <td><?php echo $firstName; ?></td>
  61. </tr>
  62. <tr>
  63. <td>Last Name:</td>
  64. <td><?php echo $lastName; ?></td>
  65. </tr>
  66. <tr>
  67. <td>Email:</td>
  68. <td><?php echo $email; ?></td>
  69. </tr>
  70. <tr>
  71. <td>Password:</td>
  72. <td>Encrypted</td>
  73. </tr>
  74. </table>
  75. </td>
  76. <CAPTION><hr/><h3>UPDATE PROFILE</h3></CAPTION>
  77. <form action="manage-profile.php?id=<?php echo $_SESSION['member_id']; ?>" method="post" onsubmit="return updateProfile(this)">
  78. <div class="form-group">
  79. <label for="name">First Name:</label>
  80. <input type="text" class="form-control" name="firstname">
  81. </div>
  82. <div class="form-group">
  83. <label for="name">Last Name:</label>
  84. <input type="text" class="form-control" name="lastname">
  85. </div>
  86. <div class="form-group">
  87. <label>Email Address:</label>
  88. <input type="text" class="form-control" name="email">
  89. </div>
  90. <div class="form-group">
  91. <label>New Password:</label>
  92. <input type="password" class="form-control" name="password">
  93. </div>
  94. <div class="form-group">
  95. <label>Confirm New Password:</label>
  96. <input type="password" class="form-control" name="ConfirmPassword">
  97. </div>
  98. <button type="submit" name="update" class="btn btn-default">Update Profile</button>
  99. </form>
  100. <hr>
  101. </div>
  102. <div id="footer">
  103. <div class="bottom_addr">Sourcecodester &copy; @2016</div>
  104. </div>
  105. </div>
  106. </body>
  107. </html>

And for more forms and file of this project to be added just download the file from the download button below.

Features

Admin
  • Manage Administrator
  • Manage Positions
  • Manage Candidates
  • View Poll Result
  • Login/Logout
Student
  • Login/Logout
  • Vote
  • Manage Account

How to Run

Requirements
  • Download and Install any local web server such as XAMPP/WAMP.
  • Download the provided source code zip file. (download button is located below)
Installation/Setup
  1. Open your XAMPP/WAMP's Control Panel and start the "Apache" and "MySQL".
  2. Extract the downloaded source code file.
  3. If you are using XAMPP, copy the extracted source code folder and paste it into the XAMPP's "htdocs" directory. And If you are using WAMP, paste it into the "www" directory.
  4. Browse the PHPMyAdmin in a browser. i.e. http://localhost/phpmyadmin
  5. Create a new database naming "poll".
  6. Import the provided SQL file. The file is known as "poll.sql" located inside the extracted source code folder.
  7. Browse the Simple Polling System in a browser. i.e. http://localhost/pollsys/login.html for the students/voters and http://localhost/pollsys/admin/login.html for the admin side

Admin Access:

Email: [email protected]
Password: admin

DEMO

I Hope that you have/will learn something useful from this tutorial. Don't forget to Like and Share.

Enjoy Coding!

Comments

Submitted byseen (not verified)on Tue, 09/06/2016 - 17:02

thank you for the lesson
Submitted byOlayemi Afolabi (not verified)on Fri, 09/09/2016 - 17:55

Thanks for the lesson, but there is no page for the SUBMIT and Update

Add new comment