OXIESEC PANEL
- Current Dir:
/
/
var
/
www
/
cream
/
quiz
/
assets
/
process
Server IP: 139.59.38.164
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
02/15/2025 11:20:33 AM
rwxr-xr-x
📄
quiz_submit.php
1.28 KB
05/19/2025 10:07:16 AM
rw-r--r--
📄
quiz_submit_test.php
3 KB
05/19/2025 10:07:16 AM
rw-r--r--
📄
temp_answer.php
2.07 KB
05/19/2025 10:07:16 AM
rw-r--r--
Editing: quiz_submit_test.php
Close
<?php include('../php/db_config.php'); $user_id = intval($_POST['user_id']); $quiz_id = intval($_POST['quiz_id']); if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { // Start a transaction to ensure consistency $conn->begin_transaction(); // Retrieve all answers for the user and quiz from `temp_answer` $fetchTempAnswersQuery = "SELECT que_id, answer FROM temp_answer WHERE user_id = ? AND quiz_id = ?"; $fetchTempAnswersStmt = $conn->prepare($fetchTempAnswersQuery); $fetchTempAnswersStmt->bind_param('ii', $user_id, $quiz_id); $fetchTempAnswersStmt->execute(); $tempAnswersResult = $fetchTempAnswersStmt->get_result(); if ($tempAnswersResult->num_rows > 0) { // Loop through the temp answers and insert into `candidate_answer` while ($row = $tempAnswersResult->fetch_assoc()) { $question_id = $row['que_id']; $answer = $row['answer']; // Insert into `candidate_answer` $insertQuery = "INSERT INTO candidate_answer (que_id, user_id, quiz_id, answer) VALUES (?, ?, ?, ?)"; $insertStmt = $conn->prepare($insertQuery); $insertStmt->bind_param('iiis', $question_id, $user_id, $quiz_id, $answer); if (!$insertStmt->execute()) { throw new Exception("Error inserting answer for question ID: $question_id"); } } // Delete the answers from `temp_answer` after successful insertion $deleteTempAnswersQuery = "DELETE FROM temp_answer WHERE user_id = ? AND quiz_id = ?"; $deleteTempAnswersStmt = $conn->prepare($deleteTempAnswersQuery); $deleteTempAnswersStmt->bind_param('ii', $user_id, $quiz_id); if (!$deleteTempAnswersStmt->execute()) { throw new Exception("Error deleting temp answers."); } $status = "Y"; $quizStatusQuery = "UPDATE quiz_status SET quiz_submition = ? WHERE user_id = ? AND quiz_id = ?"; $quizStatusStmt = $conn->prepare($quizStatusQuery); $quizStatusStmt->bind_param('sii', $status, $user_id, $quiz_id); if (!$quizStatusStmt->execute()) { throw new Exception("Error In setting the status."); } // Commit the transaction $conn->commit(); echo json_encode(['status' => 'success', 'message' => 'Quiz submitted successfully!']); } else { echo json_encode(['status' => 'error', 'message' => 'No answers found to submit.']); } } catch (Exception $e) { // Rollback the transaction on error $conn->rollback(); error_log($e->getMessage()); echo json_encode(['status' => 'error', 'message' => $e->getMessage()]); } } else { echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']); }