<?php
include('dbcon.php');
session_start();
$output = null;

$studentuser=$_SESSION['user'];
if(!isset($_SESSION['user']))
{
    // not logged in
    header('Location:signin.php');
    exit();
} 
 
$query = $link->query("SELECT * FROM studentdata WHERE email='$studentuser'");
if (mysqli_num_rows($query) != 0) 
{
    while ($row = mysqli_fetch_array($query)) {
        $id=$row['id'];
        $name=$row['name'];
        $email=$row['email'];
        $voted = $row['voted'];
              
                if($voted == "yes"){
                    echo '<script>window.location.href="multiplevote.php"</script>';
                }
                else{
                    if (isset($_POST['submit'])) {
                    $voter=$_POST['voter'];
                    $email2=$_POST['email'];
                    $malecaptain=$_POST['MaleCaptain'];
                    $femalecaptain=$_POST['FemaleCaptain'];
                }
      }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Submit</title>
</head>
<body>
    <h1 align="center">SUCCESS!</h1>
  	<h4>Well done!</h4>

<?php
          $select=$link->query("SELECT * FROM candidates WHERE name='$malecaptain'");
          if($select) {
            while($row=mysqli_fetch_array($select)) {
              $c_id=$row['id'];
              $cand_name=$row['name'];
              $position=$row['position'];
              $nvotes=$row['nvotes'];
            }
            $add_vote = 1;
            $updated_vote = $nvotes + $add_vote;

            $update=$link->query("UPDATE candidates SET nvotes='$updated_vote' WHERE id='$c_id'");
          }
            if($update) {
              $select2=$link->query("SELECT * FROM candidates WHERE name='$femalecaptain'");
              if($select2) {
                while ($row2=mysqli_fetch_array($select2)) {
                  $c_id2=$row2['id'];
                  $cand_name2=$row2['name'];
                  $pos2=$row2['position'];
                  $nvotes2=$row2['nvotes'];
                }
                $new_vote2 = 1;
                $updated_vote2 = $nvotes2 + $new_vote2;
            
            $update2=$link->query("UPDATE candidates SET nvotes='$updated_vote2' WHERE id='$c_id2'");

        }
           if($update2) {
             echo '<p>You have voted '. $malecaptain. ' as your Male Captain.</p>';
             echo '<p>You have voted '. $femalecaptain. ' as your Female Captain.</p>';
             $input=$link->query("UPDATE studentdata SET voted='yes' WHERE id='$id'");

           }
       }
?>

 <a href="signout.php"><button>Exit the election portal</button></a>
