-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedit.php
More file actions
202 lines (151 loc) · 6.78 KB
/
edit.php
File metadata and controls
202 lines (151 loc) · 6.78 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php require 'db.php';
if (isset($_POST) && !empty($_POST)) {
// keep track validation errors
$questionidError = null;
$nameError = null;
$choice1 = null;
$choice2 = null;
$choice3 = null;
$answer = null;
// keep track post values
$questionid = $_GET['id'];
$name = $_POST['name'];
$choice1 = $_POST['choice1'];
$choice2 = $_POST['choice2'];
$choice3 = $_POST['choice3'];
$answer = $_POST['answer'];
// validate input
$valid = true;
if (empty($questionid)) {
$questionidError = 'Please write question';
$valid = false;
}
if (empty($name)) {
$nameError = 'Please write question';
$valid = false;
}
if (empty($choice1)) {
$choice1Error = 'Please write incorrect answer';
$valid = false;
}
if (empty($choice2)) {
$choice2Error = 'Please write incorrect answer';
$valid = false;
}
if (empty($choice3)) {
$choice3Error = 'Please write incorrect answer';
$valid = false;
}
if (empty($answer)) {
$answerError = 'Please write correct answer';
$valid = false;
}
// update data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE quiz SET name = ?, choice1 = ?, choice2 = ?, choice3 = ?, answer = ? WHERE questionid = ? ";
$q = $pdo->prepare($sql);
$q->execute(array($name,$choice1,$choice2,$choice3,$answer,$questionid));
header("Location: manage.php");
}
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM quiz where questionid = ?";
$q = $pdo->prepare($sql);
$q->execute(array($_GET['id']));
$data = $q->fetch(PDO::FETCH_ASSOC);
$questionid = $data['questionid'];
$name = $data['name'];
$choice1 = $data['choice1'];
$choice2 = $data['choice2'];
$choice3 = $data['choice3'];
$answer = $data['answer'];
}
?>
<?php echo file_get_contents("head.php"); ?>
<?php echo file_get_contents("header.php");?>
<div class="container">
<!-- Jumbotron Header -->
<header class="jumbotron fp-jt">
<h1>Edit Quiz</h1>
<div class="col-sm-6 col-md-6 text-right">
<div class="btn-group inline" role="group">
<a href="manage.php" class="btn btn-danger">Go back</a>
</div>
</div>
</header>
<hr>
<!--Quiz -->
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-md-offset-2 col-md-8">
<form class="form-horizontal" method="post">
<div class="control-group <?php echo !empty($nameError)?'error':'';?>">
<label class="control-label">Question</label>
<div class="controls">
<input name="name" type="text" class="form-control" placeholder="name" value="<?php echo !empty($name)?$name:'';?>">
<?php if (!empty($nameError)): ?>
<span class="help-inline"><?php echo $nameError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($choice1Error)?'error':'';?>">
<label class="control-label">Choice1</label>
<div class="controls">
<input name="choice1" type="text" class="form-control" placeholder="choice1" value="<?php echo !empty($choice1)?$choice1:'';?>">
<?php if (!empty($choice1Error)): ?>
<span class="help-inline"><?php echo $choice1Error;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($choice2Error)?'error':'';?>">
<label class="control-label">Choice2</label>
<div class="controls">
<input name="choice2" type="text" class="form-control" placeholder="choice2" value="<?php echo !empty($choice2)?$choice2:'';?>">
<?php if (!empty($choice2Error)): ?>
<span class="help-inline"><?php echo $choice2Error;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($choice3Error)?'error':'';?>">
<label class="control-label">Choice3</label>
<div class="controls">
<input name="choice3" type="text" class="form-control" placeholder="choice3" value="<?php echo !empty($choice3)?$choice3:'';?>">
<?php if (!empty($choice3Error)): ?>
<span class="help-inline"><?php echo $choice3Error;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($answerError)?'error':'';?>">
<label class="control-label">Answer</label>
<div class="controls">
<input name="answer" type="text" class="form-control" placeholder="answer" value="<?php echo !empty($answer)?$answer:'';?>">
<?php if (!empty($answerError)): ?>
<span class="help-inline"><?php echo $answerError;?></span>
<?php endif; ?>
</div>
</div>
<div class="form-actions">
<br>
<button type="submit" class="btn btn-success">Edit</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- /.row -->
<!-- Quiz-->
<!-- /.row -->
<hr>
</div>
<!-- Footer -->
<?php
echo file_get_contents("pagefooter.php");
?>
<?php
echo file_get_contents("footer.php");
?>