-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.php
130 lines (116 loc) · 3.21 KB
/
add.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
<?php include 'database.php'; ?>
<?php if(isset($_POST['submit'])){
//Get post variables
$question_number = $_POST['question_number'];
$question_text = $_POST['question_text'];
$correct_choice = $_POST['correct_choice'];
//choices array
$choices = array();
$choices[1] =$_POST['choice1'];
$choices[2] =$_POST['choice2'];
$choices[3] =$_POST['choice3'];
$choices[4] =$_POST['choice4'];
$choices[5] =$_POST['choice5'];
//Question query
$query = "INSERT INTO `questions` (question_number, text)
VALUES('$question_number','$question_text')";
//RUN query
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
//validate insert
if($insert_row){
foreach($choices as $choice => $value){
if($value != ''){
if($correct_choice == $choice){
$is_correct = 1;
} else {
$is_correct = 0;
}
//choice query
$query = "INSERT INTO `choices` (question_number, is_correct, text)
VALUES ('$question_number',' $is_correct','$value')";
//RUN query
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);
//validate insert
if($insert_row){
continue;
} else {
die('Error : ('.$mysqli->error.') '. $mysqli->error);
}
}
}
$msg = 'Question has been added';
}
}
/*
* Get total questions
*/
$query = "SELECT * FROM `questions`";
//Get The Results
$questions = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $questions->num_rows;
$next = $total+1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" media="screen" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css">
<title>PHP Quizzer</title>
<link rel="stylesheet" href="css/style.css">
<!-- -->
<style>
</style>
</head>
<body>
<header>
<div class="container">
<h1>PHP Quizzer</h1>
</div>
</header>
<main>
<div class="container">
<h2>Add A Question</h2>
<?php
if(isset($msg)){
echo '<p>'.$msg.'</p>';
}
?>
<form method="post" action="add.php">
<p><label> Question Number</label>
<input type="number" value="<?php echo $next; ?>" name="question_number"></p>
<p><label>Question Text:</label>
<input type="text" name="question_text">
</p>
<p><label>Choice #1:</label>
<input type="text" name="choice1">
</p>
<p><label>Choice #2:</label>
<input type="text" name="choice2">
</p>
<p><label>Choice #3:</label>
<input type="text" name="choice3">
</p>
<p><label>Choice #4:</label>
<input type="text" name="choice4">
</p>
<p><label>Choice #5:</label>
<input type="text" name="choice5">
</p>
<p><label>Correct Choice Number</label>
<input type="number" name="correct_choice"></p>
<p><input type="submit" name="submit" value="submit"></p>
</form>
</div>
</main>
<footer>
<div class="container">
Copyright © 2019, PHP Quizzer
</div>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
</script>
</body>
</html>