-
Notifications
You must be signed in to change notification settings - Fork 31
/
submit_ballot.php
60 lines (48 loc) · 1.41 KB
/
submit_ballot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
include 'includes/session.php';
include 'includes/slugify.php';
if(isset($_POST['vote'])){
if(count($_POST) == 1){
$_SESSION['error'][] = 'Please vote atleast one candidate';
}
else{
$_SESSION['post'] = $_POST;
$sql = "SELECT * FROM positions";
$query = $conn->query($sql);
$error = false;
$sql_array = array();
while($row = $query->fetch_assoc()){
$position = slugify($row['description']);
$pos_id = $row['id'];
if(isset($_POST[$position])){
if($row['max_vote'] > 1){
if(count($_POST[$position]) > $row['max_vote']){
$error = true;
$_SESSION['error'][] = 'You can only choose '.$row['max_vote'].' candidates for '.$row['description'];
}
else{
foreach($_POST[$position] as $key => $values){
$sql_array[] = "INSERT INTO votes (voters_id, candidate_id, position_id) VALUES ('".$voter['id']."', '$values', '$pos_id')";
}
}
}
else{
$candidate = $_POST[$position];
$sql_array[] = "INSERT INTO votes (voters_id, candidate_id, position_id) VALUES ('".$voter['id']."', '$candidate', '$pos_id')";
}
}
}
if(!$error){
foreach($sql_array as $sql_row){
$conn->query($sql_row);
}
unset($_SESSION['post']);
$_SESSION['success'] = 'Ballot Submitted';
}
}
}
else{
$_SESSION['error'][] = 'Select candidates to vote first';
}
header('location: home.php');
?>