Skip to content

Electronic Voting Machine using PHP & MySQL

Index.php


<?php
  require "admin/dbC.php";
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>EVM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <form class="main" action="<?php $_PHP_SELF; ?>" method="POST">

      <ul class="leftBox">
        <li>
          <img src="img/bjp.png" alt="">
          <span>Bharatiya Janata Party</span>
        </li>

        <li>
          <img src="img/inc.png" alt="">
          <span>Indian National Congress</span>
        </li>

        <li>
          <img src="img/bsp.png" alt="">
          <span>Bahujan Samaj Party</span>
        </li>

        <li  class="lf">
          <img src="img/aitc.png" alt="">
          <span>All India Trinamool Congress</span>
        </li>


        <li>
          <img src="img/cpi.png" alt="" >
          <span>Communist Party of India</span>
        </li>

        <li>
          <img src="img/ncp.png" alt="">
          <span>Nationalist Congress Party</span>
        </li>

        <li>
          <img src="img/app.jpg" alt="">
          <span>Aam Aadmi Party</span>
        </li>

        <li>
          <img src="img/bjd.jpg" alt="">
          <span>Biju Janata Dal</span>
        </li>

      </ul>


      <ul class="rightBox">
        <li>
          <button type="submit"  name="btn1" id="btnV1" class="btn_vote" onclick="lightOn(1)">  </button>
          <button class="btn_light" id="light1"></button>
        </li>

        <li>
          <button type="submit" name="btn2" id="btnV2" class="btn_vote" onclick="lightOn(2)">  </button >
          <button class="btn_light" id="light2"></button>
        </li>

        <li>
          <button type="submit" name="btn3" id="btnV3" class="btn_vote" onclick="lightOn(3)">  </button>
          <button class="btn_light" id="light3"></button>
        </li>

        <li>
          <button type="submit" name="btn4" id="btnV4" class="btn_vote" onclick="lightOn(4)">  </button>
          <button class="btn_light" id="light4"></button>
        </li>

        <li>
          <button type="submit" name="btn5" id="btnV5" class="btn_vote" onclick="lightOn(5)">  </button>
          <button name="" class="btn_light" id="light5"></button>
        </li>

        <li>
          <button type="submit" name="btn6" id="btnV6" class="btn_vote" onclick="lightOn(6)">  </button>
          <button name="" class="btn_light" id="light6"></button>
        </li>

        <li>
          <button type="submit" name="btn7" id="btnV7" class="btn_vote" onclick="lightOn(7)">  </button>
          <button name="" class="btn_light" id="light7"></button>
        </li>

        <li>
          <button type="submit" name="btn8" id="btnV8" class="btn_vote" onclick="lightOn(8)">  </button>
          <button name="" class="btn_light" id="light8"></button>
        </li>

      </ul>

      <h5 id="msg"></h5>
    </form>


    <script>
      function lightOn(no) {
        document.getElementById("light"+no).style.backgroundColor="red";

        for(a=1;a<=8;a++){

          if(a==no){
            document.getElementById("btnV"+a).disabled = false;
          }
          else {
            document.getElementById("btnV"+a).style.background=" #1a5276";
            document.getElementById("btnV"+a).disabled = true;
          }
        }
      }
    </script>

  </body>
</html>

<?php

for($a=1; $a<=8; $a++){
  if(isset($_POST['btn'.$a.''])){
    $sql ="SELECT * from prt WHERE prtID=$a";

    $result=mysqli_query($con, $sql);

    if(mysqli_num_rows($result)>0){
        while($row = mysqli_fetch_assoc($result)){

            $prtCount = $row['prtVoteTotal'];
            $prtCount=$prtCount+1;

            $sql ="UPDATE prt SET prtVoteTotal = $prtCount WHERE prtID=$a;";

            $query = mysqli_query($con, $sql);

            if($query){

              $sql ="SELECT * from prt WHERE prtID=$a";

              $result=mysqli_query($con, $sql);

              if(mysqli_num_rows($result)>0){
                  while($row = mysqli_fetch_assoc($result)){

                      $prtName = $row['prtNAME'];
                      echo"<script>
                        alert('Thank you for Voting! (".$prtName.")');
                      </script>";
                  }
              }

            }
            else{
                echo"Error:".$sql."<br>".$con->error;
            }
      }
    }
  }
}
?>

css/style.css

@import url(https:fonts.googleapis.com/css/css?family=Open+Sans:400,300,300italic,400italic,600italic,600,700,700italic,800,800italic);

*{
  margin: 0;
  padding: 0;
}

body{
  font-family: 'Open Sans',sans-serif;
}


.main{
  margin: 0 auto;
  height: 500px;
  border: 3px solid  #5dade2;
  background:  #eaeded;
  border-radius: 8px;
  margin-top:4%;
}

.leftBox{
  width: 61%;
  float: left;
  margin-left: 4%;
}

.rightBox{
  float: right;
  width: 25%;
  margin-right: 5%
}

.leftBox , .rightBox{
  margin-top: 8%;
  margin-bottom: 8%;
}

.leftBox{
  border: 2px solid #5dade2;
  border-radius: 2px;
}

.rightBox{
  border: 2px solid #5dade2;
  border-radius: 8px;
}

.leftBox li, .rightBox li{
  list-style: none;
  height: 50px;
}

.leftBox li{
  border-bottom: 1px solid #5dade2;
}

.leftBox li img{
  width: 30px;
  margin: 10px;
}

.leftBox li span{
  font-size: 12px;
  position: absolute;
  line-height: 50px;
}

.rightBox li{
  border-bottom: 1px dashed #5dade2;
}

.btn_vote {
  width: 60px;
  height: 40px;
  margin: 5px;
  background: #3D94F6;
  background-image: -webkit-linear-gradient(top, #3D94F6, #1E62D0);
  background-image: -moz-linear-gradient(top, #3D94F6, #1E62D0);
  background-image: -ms-linear-gradient(top, #3D94F6, #1E62D0);
  background-image: -o-linear-gradient(top, #3D94F6, #1E62D0);
  background-image: linear-gradient(to bottom, #3D94F6, #1E62D0);
  -webkit-border-radius: 20px;
  -moz-border-radius: 20px;
  border-radius: 20px;
  border: solid #337FED 1px;
  cursor: pointer;
}

.btn_vote:hover {
   background: #1E62D0;
   background-image: -webkit-linear-gradient(top, #1E62D0, #3D94F6);
   background-image: -moz-linear-gradient(top, #1E62D0, #3D94F6);
   background-image: -ms-linear-gradient(top, #1E62D0, #3D94F6);
   background-image: -o-linear-gradient(top, #1E62D0, #3D94F6);
   background-image: linear-gradient(to bottom, #1E62D0, #3D94F6);
}

.btn_light {
   width: 12px;
   height: 12px;
   background: #922b21 ;
   -webkit-border-radius: 20px;
   -moz-border-radius: 20px;
   border-radius: 20px;
   display: inline-block;
   border: none;
   margin-left: 2px;
}

#msg{
  margin: 5px;
  text-align: center;
}



@media screen and (min-width: 1125px) {

  .main{
    width: 35%;
  }

}

@media screen and (min-width: 850px) and (max-width: 1124px) {
  .main{
    width: 45%;
  }
}

@media screen and (min-width: 581px) and (max-width: 849px) {
  .main{
    width: 70%;
  }
}

@media only screen and (min-width: 400px)and (max-width: 580px) {
  .main{
    width: 90%;
  }
}

@media only screen and (max-width: 399px) {
  .main{
    width: 98%;
  }

  .leftBox li img{
    width: 25px;
    margin: 8px;
  }

  .btn_vote {
    width: 40px;
    height: 30px;
  }

  .btn_light{
    margin: 0px;
  }

  .leftBox li span{
    font-size: 10.3px;
  }

}

admin/index.php

<?php
  include "dbC.php";

  $run= false;
  $Error= " ";
  $Error2= " ";

  function validate_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    return $data;
  }
?>


<?php
  if($_SERVER['REQUEST_METHOD']==="POST"){

    $usrNam = $_POST['userName'];
    $usrPsd = $_POST['userPassword'];

    if(empty($_POST['userName'])){
      $Error ="Email required!";
      $run = false;
    }
    else {
      $usrNam = validate_input($_POST['userName']);
      $run = true;
    }

    if(empty($_POST['userPassword'])){
      $Error2 ="Password required!";
      $run = false;
    }
    else {
      $usrNam = validate_input($_POST['userPassword']);
      $run = true;
    }

    if($run){
      if(isset($_POST['btnLogin'])){
        $usrNam = $_POST['userName'];
        $usrPsd = $_POST['userPassword'];

        $sql ="SELECT * from admuser WHERE usrName='$usrNam' and usrPassword='$usrPsd' ";

        $result = mysqli_query($con, $sql);

        if(mysqli_num_rows($result)>0){
          while($row = mysqli_fetch_assoc($result)){
            $usrID= $row['usrId'];
            $usrNm= $row['usrName'];
            $usrEml= $row['usrEmail'];
            $usrPssd= $row['usrPassword'];

            session_start();
            $_SESSION['SESS_USER_ID'] = $row['usrId'];
            $_SESSION['SESS_USER_NAME'] = $row['usrName'];
            $_SESSION['SESS_USER_EMAIL'] = $row['usrEmail'];

            header('Location: dash.php');
          }
        }
        else {
          echo"Username and Password Wrong";
        }
  }
}}

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>ADMIN LOGIN - EVM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <div class="main">
      <form class="" action="<?php $_PHP_SELF; ?>" method="POST">

        <div class="usrSec">
          <label for="">Username:</label>
          <input type="text" name="userName">
        </div>



        <div class="psdSec">
          <label for="">Password:</label>
          <input type="password" name="userPassword">
        </div>

        <div class="submit">
          <button type="submit" name="btnLogin">Login</button>
        </div>
        <center><?php echo $Error."<br>".$Error2; ?></center>
      </form>

    </div>
  </body>
</html>

admin/css/style.css

<?php
  include "dbC.php";

  $run= false;
  $Error= " ";
  $Error2= " ";

  function validate_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    return $data;
  }
?>


<?php
  if($_SERVER['REQUEST_METHOD']==="POST"){

    $usrNam = $_POST['userName'];
    $usrPsd = $_POST['userPassword'];

    if(empty($_POST['userName'])){
      $Error ="Email required!";
      $run = false;
    }
    else {
      $usrNam = validate_input($_POST['userName']);
      $run = true;
    }

    if(empty($_POST['userPassword'])){
      $Error2 ="Password required!";
      $run = false;
    }
    else {
      $usrNam = validate_input($_POST['userPassword']);
      $run = true;
    }

    if($run){
      if(isset($_POST['btnLogin'])){
        $usrNam = $_POST['userName'];
        $usrPsd = $_POST['userPassword'];

        $sql ="SELECT * from admuser WHERE usrName='$usrNam' and usrPassword='$usrPsd' ";

        $result = mysqli_query($con, $sql);

        if(mysqli_num_rows($result)>0){
          while($row = mysqli_fetch_assoc($result)){
            $usrID= $row['usrId'];
            $usrNm= $row['usrName'];
            $usrEml= $row['usrEmail'];
            $usrPssd= $row['usrPassword'];

            session_start();
            $_SESSION['SESS_USER_ID'] = $row['usrId'];
            $_SESSION['SESS_USER_NAME'] = $row['usrName'];
            $_SESSION['SESS_USER_EMAIL'] = $row['usrEmail'];

            header('Location: dash.php');
          }
        }
        else {
          echo"Username and Password Wrong";
        }
  }
}}

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>ADMIN LOGIN - EVM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <div class="main">
      <form class="" action="<?php $_PHP_SELF; ?>" method="POST">

        <div class="usrSec">
          <label for="">Username:</label>
          <input type="text" name="userName">
        </div>



        <div class="psdSec">
          <label for="">Password:</label>
          <input type="password" name="userPassword">
        </div>

        <div class="submit">
          <button type="submit" name="btnLogin">Login</button>
        </div>
        <center><?php echo $Error."<br>".$Error2; ?></center>
      </form>

    </div>
  </body>
</html>

dbC.php

<?php
  $server="localhost";
  $username="root";
  $password="";
  $dbname="evm";

  $con= new mysqli($server, $username, $password, $dbname);

  if($con -> connect_error){
    die("Connection Failed: ".$con -> connect_error);
  }
?>

dash.php

<?php
  require('dbC.php');

  session_start();

  if(empty($_SESSION['SESS_USER_ID'])){
    header("Location: index.php");
  }

  $SESS_usrID = $_SESSION['SESS_USER_ID'];
  $SESS_usrNM = $_SESSION['SESS_USER_NAME'];
  $SESS_usrEML = $_SESSION['SESS_USER_EMAIL'];


  for($a=1;$a<=8;$a++){
    $sql ="SELECT * from prt where prtID=$a";

    $result=mysqli_query($con, $sql);

    if(mysqli_num_rows($result)>0){
        while($row = mysqli_fetch_assoc($result)){
            $prtyID[$a]= $row['prtVoteTotal'];
        }
    }
  }




  if(isset($_GET['logout2'])){
      SESSION_UNSET();
      SESSION_DESTROY();
      header("Location:./");
  }

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>Dashboard - EVM</title>
    <link rel="stylesheet" href="../css/style.css">

    <style media="screen">
      .noos{
        line-height: 50px;
      }
    </style>
  </head>
  <body>
    <form class="main">

      <ul class="leftBox">

        <li>
          <img src="../img/bjp.png" alt="">
          <span>Bharatiya Janata Party</span>
        </li>

        <li>
          <img src="../img/inc.png" alt="">
          <span>Indian National Congress</span>
        </li>

        <li>
          <img src="../img/bsp.png" alt="">
          <span>Bahujan Samaj Party</span>
        </li>

        <li  class="lf">
          <img src="../img/aitc.png" alt="">
          <span>All India Trinamool Congress</span>
        </li>


        <li>
          <img src="../img/cpi.png" alt="" >
          <span>Communist Party of India</span>
        </li>

        <li>
          <img src="../img/ncp.png" alt="">
          <span>Nationalist Congress Party</span>
        </li>

        <li>
          <img src="../img/app.jpg" alt="">
          <span>Aam Aadmi Party</span>
        </li>

        <li>
          <img src="../img/bjd.jpg" alt="">
          <span>Biju Janata Dal</span>
        </li>

      </ul>


      <ul class="rightBox">
        <li>
          <center class="noos"><h3><?php echo $prtyID[1];?></h3></center>
        </li>

        <li>
          <center class="noos"><h3><?php echo $prtyID[2];?></h3></center>
        </li>

        <li>
          <center class="noos"><h3><?php echo $prtyID[3];?></h3></center>
        </li>

        <li>
          <center class="noos"><h3><?php echo $prtyID[4];?></h3></center>
        </li>

        <li>
          <center class="noos"><h3><?php echo $prtyID[5];?></h3></center>
        </li>

        <li>
          <center class="noos"><h3><?php echo $prtyID[6];?></h3></center>
        </li>

        <li>
          <center class="noos"><h3><?php echo $prtyID[7];?></h3></center>
        </li>

        <li>
          <center class="noos"><h3><?php echo $prtyID[8];?></h3></center>
        </li>

      </ul>

      <button type="submit" name="logout2"  class="" onclick="">LOGOUT</button>
    </form>

  </body>
</html>

sql.txt

CREATE DATABASE evm;

USE evm;

CREATE TABLE `evm`.`AdmUser` ( `usrId` INT NOT NULL AUTO_INCREMENT , `usrName` VARCHAR(50) NOT NULL , `usrEmail` VARCHAR(50) NOT NULL , `usrPassword` VARCHAR(50) NOT NULL , PRIMARY KEY (`usrId`), UNIQUE (`usrName`)) ENGINE = MyISAM;

CREATE TABLE `evm`.`prt` ( `prtID` INT(11) NOT NULL AUTO_INCREMENT , `prtNAME` VARCHAR(100) NOT NULL , `prtVoteTotal` INT(11) NOT NULL , PRIMARY KEY (`prtID`)) ENGINE = MyISAM;

2 Comments

  1. Simply want to say your article is as astounding. The clarity for your submit is simply nice and i can think you are knowledgeable on this subject. Well together with your permission let me to grasp your feed to keep up to date with coming near near post. Thanks a million and please continue the gratifying work.

  2. Hey are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any coding knowledge to make your own blog? Any help would be really appreciated!

Leave a Reply

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