forked from Shalini-Majumdar/Quizzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_quiz.php
More file actions
49 lines (41 loc) · 1.53 KB
/
save_quiz.php
File metadata and controls
49 lines (41 loc) · 1.53 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
<?php
session_start();
include 'db_connect.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get quiz info
$quiz_name = $_POST['name'];
$description = $_POST['description'];
$user_id = $_SESSION['user_id'];
// Insert into quizzes table
$stmt = $conn->prepare("INSERT INTO quizzes (user_id, name, description) VALUES (?, ?, ?)");
$stmt->bind_param("iss", $user_id, $quiz_name, $description);
if ($stmt->execute()) {
$quiz_id = $stmt->insert_id;
// Loop through questions
$questions = $_POST['questions'];
$option_a = $_POST['a'];
$option_b = $_POST['b'];
$option_c = $_POST['c'];
$option_d = $_POST['d'];
$correct_option = $_POST['correct'];
$stmt_q = $conn->prepare("INSERT INTO questions (quiz_id, question, option_a, option_b, option_c, option_d, correct_option) VALUES (?, ?, ?, ?, ?, ?, ?)");
for ($i = 0; $i < count($questions); $i++) {
$q = $questions[$i];
$a = $option_a[$i];
$b = $option_b[$i];
$c = $option_c[$i];
$d = $option_d[$i];
$correct = $correct_option[$i];
$stmt_q->bind_param("issssss", $quiz_id, $q, $a, $b, $c, $d, $correct);
$stmt_q->execute();
}
// Success
header("Location: dashboard.php?success=quiz_created");
exit;
} else {
echo "Failed to create quiz: " . $stmt->error;
}
} else {
echo "Invalid request.";
}
?>