-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_update.php
145 lines (123 loc) · 5.11 KB
/
question_update.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_logged_in(); ?>
<?php is_admin(); ?>
<?php
include_once("includes/form_functions.php");
include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<a href="content.php">Return to Menu</a><br />
<br />
</td>
<td id="page">
<?php
// START FORM PROCESSING
if (isset($_POST['submit'])) { // Form has been submitted.
$errors = array();
// perform validations on the form data
$required_fields = array('question');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
$fields_with_lengths = array('question' => 300 );
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));
$question = trim(mysql_prep($_POST['question']));
if ( empty($errors) ) {
$question_set = mysql_query("SELECT * FROM survey_field", $connection);
if (!$question_set) {
die("Database query failed: " . mysql_error());
}while ($question_final = mysql_fetch_array($question_set)) {
$id_value=$question_final["survey_id"];
}
$query = "UPDATE survey_question SET survey_name = '{$question}', type = '{$_POST['type']}' WHERE question_id = {$_POST['question_id']}";
//echo $query; exit;
$result = mysql_query($query, $connection);
$query = "DELETE FROM survey_question_answers WHERE question_id = {$_POST['question_id']}";
$result = mysql_query($query, $connection);
//echo '<pre/>';
//print_r($_POST); exit;
foreach ($_POST['answer'] as $key => $value) {
if (!empty($value)) {
$query = "INSERT INTO survey_question_answers (
question_id,answer,ans_type
) VALUES (
'{$_POST['question_id']}','{$value}', '{$_POST['type']}'
) ";
$result = mysql_query($query, $connection);
}
}
if ($result) {
$message = "The Question was update successfully.";
//$message = "<a href="content.php">Return to Menu</a>";
} else {
$message = "The Question could not be created.";
$message .= "<br />" . mysql_error();
}
}
else {
if (count($errors) == 1) {
$message = "There was 1 error in the form.";
} else {
$message = "There were " . count($errors) . " errors in the form.";
}
}
if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";}
if (!empty($errors)) { display_errors($errors); }
}
else { // Form has not been submitted.
$question = "";
?>
<h2>Create New Question</h2>
<form action="question_update.php" method="post">
<table>
<?php $question = mysql_query("SELECT * FROM survey_question WHERE question_id = {$_GET['question_id']}", $connection);
$question = mysql_fetch_array($question);
$answers_set = mysql_query("SELECT * FROM survey_question_answers WHERE question_id = {$question['question_id']}", $connection);
?>
<tr>
<td>Question title:</td>
<td><input type="text" name="question" maxlength="300" value="<?php echo htmlentities($question['survey_name']); ?>" />
<input type="hidden" name="question_id" value="<?php echo $question['question_id']; ?>" />
</td>
</tr>
<tr><td>question type: </td>
<td><select name="type">
<?php foreach (['radio' => 'Radio Button', 'checkbox' => 'CheckBox'] as $key => $value) : ?>
<option value="<?php echo $key?>" <?php if ($question['type'] == $key) echo 'selected'; ?>><?php echo $value?></option>
<?php endforeach; ?>
</select> if see radio button just type "yes" or "no":</td>
</tr>
<?php $answer_no = 1;
while ($answer = mysql_fetch_array($answers_set)) : ?>
<tr>
<td>Possible Answer <?php echo $answer_no++ ;?>:</td>
<td><input type="text" name="answer[]" maxlength="300" value="<?php echo $answer['answer']; ?>" /></td>
</tr>
<?php endwhile; ?>
<tr>
<td>Possible Answer <?php echo $answer_no++ ;?> </td>
<td><input type="text" name="answer[]" maxlength="300" value="<?php //echo htmlentities($question); ?>" /></td>
</tr>
<tr>
<td>Possible Answer <?php echo $answer_no++ ;?></td>
<td><input type="text" name="answer[]" maxlength="300" value="<?php //echo htmlentities($question); ?>" /></td>
</tr>
<tr>
<td>Possible Answer <?php echo $answer_no++ ;?></td>
<td><input type="text" name="answer[]" maxlength="300" value="<?php //echo htmlentities($question); ?>" /></td>
</tr>
<tr>
<td>Possible Answer <?php echo $answer_no++ ;?></td>
<td><input type="text" name="answer[]" maxlength="300" value="<?php //echo htmlentities($question); ?>" /></td>
</tr>
<tr>
<td colspan="5"><input type="submit" name="submit" value="Edit question" /></td>
</tr>
</table>
</form>
<?php } ?>
</td>
</tr>
</table>
<?php include("includes/footer.php"); ?>