-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_question_easy.php
68 lines (57 loc) · 2.9 KB
/
save_question_easy.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
<?php
// Database connection details
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "floraquiz";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(json_encode(["status" => "error", "message" => "Connection failed: " . $conn->connect_error]));
}
// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Set parameters with error checking
$category = isset($_POST['category']) ? $conn->real_escape_string($_POST['category']) : '';
$correctAnswer = isset($_POST['answer']) ? $conn->real_escape_string($_POST['answer']) : '';
$point = isset($_POST['points']) ? intval($_POST['points']) : 0;
$question = isset($_POST['question']) ? $conn->real_escape_string($_POST['question']) : '';
$typeQuestion = isset($_POST['questionType']) ? $conn->real_escape_string($_POST['questionType']) : '';
$option1 = isset($_POST['option1']) ? $conn->real_escape_string($_POST['option1']) : '';
$option2 = isset($_POST['option2']) ? $conn->real_escape_string($_POST['option2']) : '';
$option3 = isset($_POST['option3']) ? $conn->real_escape_string($_POST['option3']) : '';
$option4 = isset($_POST['option4']) ? $conn->real_escape_string($_POST['option4']) : '';
// Handle image upload
$imgUpload = '';
if (isset($_FILES['imageUpload']) && $_FILES['imageUpload']['error'] == 0) {
$target_dir = "uploads/easy/";
$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
$imgUpload = $target_file;
}
}
// For fill in the blanks, use fillInAnswer
if ($typeQuestion == 'fillInTheBlanks') {
$correctAnswer = isset($_POST['fillInAnswer']) ? $conn->real_escape_string($_POST['fillInAnswer']) : '';
}
// Prepare and bind
$stmt = $conn->prepare("INSERT INTO easy (Ecategory, Ecorrectanswer, Eimgupload, Epoint, Equestion, EtypeQuestion, option1, option2, option3, option4) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
if ($stmt === false) {
die(json_encode(["status" => "error", "message" => "Error preparing statement: " . $conn->error]));
}
// Corrected bind_param call
if (!$stmt->bind_param("sssissssss", $category, $correctAnswer, $imgUpload, $point, $question, $typeQuestion, $option1, $option2, $option3, $option4)) {
die(json_encode(["status" => "error", "message" => "Binding parameters failed: " . $stmt->error]));
}
if ($stmt->execute()) {
echo json_encode(["status" => "success", "message" => "New record created successfully"]);
} else {
echo json_encode(["status" => "error", "message" => "Error: " . $stmt->error]);
}
$stmt->close();
} else {
echo json_encode(["status" => "error", "message" => "Form was not submitted."]);
}
$conn->close();
?>