-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote.php
65 lines (62 loc) · 2.42 KB
/
vote.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
<?php
include 'includes.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);
// If the user clicked the "Vote" button...
if (isset($_POST['poll_answer'])) {
// 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']]);
// Redirect user to the result page
header ('Location: result.php?id=' . $_GET['id']);
exit;
}
} else {
$msg ='Poll with that ID does not exist.';
}
} else {
$msg ='No poll ID specified.';
}
?>
<?=template_header('Poll Vote')?>
<div class="col-lg-12 col-md-12 col-sm-12">
<?php if ($msg): ?>
<div class="showback">
<div class="alert alert-danger"><b><?=$msg?></b></div>
</div>
<?php else: ?>
<div class="showback">
<h4><i class="fa fa-angle-right"></i> <?=$poll['title']?></h4>
<b><?=$poll['desc']?></b>
<form action="vote.php?id=<?=$_GET['id']?>" method="post">
<?php for ($i = 0; $i < count($poll_answers); $i++): ?>
<div class="radio">
<label>
<input type="radio" name="poll_answer" value="<?=$poll_answers[$i]['id']?>"<?=$i == 0 ? ' checked' : ''?>>
<?=$poll_answers[$i]['title']?>
</label>
</div>
<?php endfor; ?>
<div>
<!-- <input type="submit" value="Vote" class="btn btn-theme"> -->
<a href="result.php?id=<?=$poll['id']?>" class="btn btn-theme02"><i class="fa fa-check"></i>View Result </button></a>
<a href="#"class="btn btn-theme"><i class="fa fa-check"></i>Vote </button></a>
</div>
</form>
</div>
<?php endif; ?>
<?=template_footer()?>