-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.php
135 lines (120 loc) · 4.72 KB
/
admin.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
session_start(); // Have to start session before html
?>
<html>
<head>
<title>Secret Voting - SHHHHH</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="/favicon.ico?" type="image/x-icon">
<style>
table {
border-collapse: collapse;
width: 100%;
}
th,td {
border: 1px solid #ddd;
padding: 15px;
}
tr-nth-child(even) {
background-color: #f2f2f2;
}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: black;
color: white;
}
</style>
</head>
<body>
<h1>Secret Votings - SHHHHH ITS SECRET</h1>
<?php if ($_SESSION["isadmin"] == true){
$servername = "127.0.0.1";
$username = "secretVote";
$password = "test";
$dbname = "voting";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: ");
} else {
$checkForCurrentPollSql = "SELECT * from current_poll;";
$result = $conn->query($checkForCurrentPollSql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$name = $row["name"];
echo "<h3>We are currently voting on: <strong>" . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . "</strong></h3>";
echo "<br>";
echo '
<form action="admincommand.php" method="post">
<input type="hidden" name="command" value="stopvote">
<input type="submit" value="STOP POLL">
</form>
';
} else {
echo '
Start a poll (If you are revoting on someone add a 1(2,3,... and so on for every time) after the name or people wont be able to vote.):
<form action="admincommand.php" method="post">
<input type="hidden" name="command" value="startvote">
<input type="text" name="name">
<input type="submit">
</form>
';
}
// Gets all the names of the people who have votes
$nameSql = "SELECT DISTINCT name FROM votes ORDER BY name ASC;";
$nameResult = $conn->query($nameSql);
if ($nameResult->num_rows > 0) {
// Clear votes button
echo '
<form action="admincommand.php" method="post">
<input type="hidden" name="command" value="clearvotes">
<input type="submit" value="CLEAR ALL VOTES!">
</form>
';
// Sets up the table header
echo '
<table>
<tr>
<th>Name</th>
<th>YES</th>
<th>NO</th>
<th>ABSTAIN</th>
<th>Total</th>
</tr>
';
// Loops over each row of names
while ($row = $nameResult->fetch_assoc()) {
$rowName = $conn->escape_string($row["name"]);
// These all count the results of the votes by each type and then the grand total and then fetching the result
$yesVotesResult = $conn->query("SELECT COUNT(*) AS total FROM votes WHERE name='". $rowName . "' AND vote='YES';") or die($conn->error);
$yesVotes = $yesVotesResult->fetch_assoc()["total"];
$noVotesResult = $conn->query("SELECT COUNT(*) AS total FROM votes WHERE name='". $rowName . "' AND vote='NO';");
$noVotes = $noVotesResult->fetch_assoc()["total"];
$abstainVotesResult = $conn->query("SELECT COUNT(*) AS total FROM votes WHERE name='". $rowName . "' AND vote='ABSTAIN';");
$abstainVotes = $abstainVotesResult->fetch_assoc()["total"];
$totalVotesResult = $conn->query("SELECT COUNT(*) AS total FROM votes WHERE name='". $rowName . "';");
$totalVotes = $totalVotesResult->fetch_assoc()["total"];
// Sets up the table row.
echo '<tr>';
echo '<td>'.$row["name"].'</td>';
echo '<td>'.$yesVotes.'</td>';
echo '<td>'.$noVotes.'</td>';
echo '<td>'.$abstainVotes.'</td>';
echo '<td>'.$totalVotes.'</td>';
echo '</tr>';
}
// Closes the table started above
echo '</table>';
} else {
echo "You currently have no votes :(";
}
}
} else { ?>
<form action="adminlogin.php" method="post">
Password: <input type="password" name="password">
<input type="submit">
</form>
<?php } ?>
</body>
</html>