-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvote.php
More file actions
115 lines (99 loc) · 3.81 KB
/
Copy pathvote.php
File metadata and controls
115 lines (99 loc) · 3.81 KB
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
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header('Location: login.php');
exit;
}
$msg = "";
if (isset($_SESSION['msg'])) {
$msg = $_SESSION['msg'];
$_SESSION['msg'] = "";
}
include 'functions.php';
// Connect to MySQL
$pdo = pdo_connect_mysql();
// If the GET request "id" exists (poll id)...
if (isset($_GET['id'])) {
// MySQL query that selects the poll records by the GET request "id"
$stmt = $pdo->prepare('SELECT * FROM polls WHERE id = ?');
$stmt->execute([$_GET['id']]);
// Fetch the record
$poll = $stmt->fetch(PDO::FETCH_ASSOC);
// Check if the poll record exists with the id specified
if ($poll) {
// MySQL query that selects all the poll answers
$stmt = $pdo->prepare('SELECT * FROM poll_answers WHERE poll_id = ?');
$stmt->execute([$_GET['id']]);
// Fetch all the poll anwsers
$poll_answers = $stmt->fetchAll(PDO::FETCH_ASSOC);
// check if user already vote
$stmt = $pdo->prepare('SELECT account_id FROM poll_commit WHERE poll_id = ?');
$stmt->execute([$_GET['id']]);
$exists_voter = $stmt->fetchAll(PDO::FETCH_ASSOC);
for ($i = 0; $i < count($exists_voter); $i++) {
if ($exists_voter[$i]["account_id"] == $_SESSION["id"]) {
header('Location: result.php?id=' . $poll['id']);
exit;
}
}
// If the user clicked the "Vote" button...
if (isset($_POST['poll_answer'])) {
// check if user already vote
$stmt = $pdo->prepare('SELECT account_id FROM poll_commit WHERE poll_id = ?');
$stmt->execute([$_GET['id']]);
$exists_voter = $stmt->fetchAll(PDO::FETCH_ASSOC);
for ($i = 0; $i < count($exists_voter); $i++) {
if ($exists_voter[$i]["account_id"] == $_SESSION["id"]) {
$_SESSION['msg'] = 'Kamu sudah vote pada polling "'. $poll['title'] .'"';
header('Location: index.php');
exit;
}
}
// Update and increase the vote for the answer the user voted for
$stmt = $pdo->prepare('UPDATE poll_answers SET votes = votes + 1 WHERE id = ?');
$stmt->execute([$_POST['poll_answer']]);
// Update poll_commit table
if ($stmt = $pdo->prepare('INSERT INTO poll_commit (account_id, poll_id) VALUES (?, ?)')) {
$stmt->execute([$_SESSION['id'], $_GET['id']]);
// Redirect user to the result page
header('Location: result.php?id=' . $_GET['id']);
exit;
} else {
echo 'Could not prepare statement!';
}
}
} else {
die('Poll with that ID does not exist.');
}
} else {
die('No poll ID specified.');
}
?>
<?=template_header('Poll Vote')?>
<div class="container-fluid content poll-vote">
<h2><?=$poll['title']?></h2>
<?php
if ($poll['desc'] != "") {
echo '<p>' . $poll['desc'] . '</p>';
}
?>
<?php
$stmt = $pdo->query('SELECT username FROM accounts WHERE id = ' . $poll['creator_id']);
$creator = $stmt->fetch(PDO::FETCH_ASSOC);
$creator_name = $creator['username'];
?>
<p>Poll created by <?=$creator_name?></p>
<form action="vote.php?id=<?=$_GET['id']?>" method="post">
<?php for ($i = 0; $i < count($poll_answers); $i++): ?>
<label>
<input type="radio" name="poll_answer" value="<?=$poll_answers[$i]['id']?>"<?=$i == 0 ? ' checked' : ''?>>
<?=$poll_answers[$i]['title']?>
</label>
<?php endfor;?>
<div>
<input type="submit" value="Vote">
<a href="result.php?id=<?=$poll['id']?>">View Result</a>
</div>
</form>
</div>
<?=template_footer()?>