-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.php
More file actions
96 lines (77 loc) · 2.7 KB
/
Copy pathcreate.php
File metadata and controls
96 lines (77 loc) · 2.7 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
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header('Location: login.php');
exit;
}
include 'functions.php';
$pdo = pdo_connect_mysql();
$msg = '';
if (isset($_SESSION['msg'])) {
$msg = $_SESSION['msg'];
$_SESSION['msg'] = '';
}
if (!empty($_POST)) {
$title = isset($_POST['title']) ? $_POST['title'] : '';
$desc = isset($_POST['desc']) ? $_POST['desc'] : '';
if ($_SESSION['id'] != $_POST['creator_id']){
$_SESSION['msg'] = 'Invalid ID!';
header('Location: create.php');
exit;
}
// Get the answers and convert the multiline string to an array, so we can add each answer to the "poll_answers" table
$answers = isset($_POST['answers']) ? explode(PHP_EOL, $_POST['answers']) : '';
$filled_answer = 0;
foreach ($answers as $answer) {
if (strlen(trim($answer)) > 0) {
$filled_answer++;
}
}
if ($filled_answer < 2) {
$_SESSION['msg'] = 'Masukkan setidaknya 2 pilihan jawaban';
header('Location: create.php');
exit;
}
if ($filled_answer > 5) {
$_SESSION['msg'] = 'Max pilihan berganda adalah 5 pilihan';
header('Location: create.php');
exit;
}
$stmt = $pdo->prepare('INSERT INTO polls VALUES (NULL, ?, ?, ?)');
$stmt->execute([$title, $desc, $_POST['creator_id']]);
$poll_id = $pdo->lastInsertId();
foreach ($answers as $answer) {
// If the answer is empty there is no need to insert
if (strlen(trim($answer)) <= 0 || empty($answer)) {
continue;
}
$stmt = $pdo->prepare('INSERT INTO poll_answers VALUES (NULL, ?, ?, 0)');
$stmt->execute([$poll_id, $answer]);
}
$msg = 'Created Successfully!';
}
?>
<?=template_header('Create Poll')?>
<div class="container content">
<?php if ($msg): ?>
<p><?=$msg?></p>
<?php endif;?>
<h2>Create Poll</h2>
<form action="create.php" method="post">
<input type="hidden" name="creator_id" id="creator_id" value="<?=$_SESSION['id']?>">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" id="title" required>
</div>
<div class="form-group">
<label for="desc">Description</label>
<input type="text" class="form-control" name="desc" id="desc">
</div>
<div class="form-group">
<label for="answers">Answers (per line), Max 5 answers</label>
<textarea name="answers" class="form-control" id="answers" required></textarea>
</div>
<input type="submit" class="btn btn-success" value="Create">
</form>
</div>
<?=template_footer()?>