i am trying to export user information about exam details ie all the question and answers the candidate has chosen but it is coming out as follows,
<br />
<b>Notice</b>: Undefined variable: total_score in <b>C:\wamp64\www\online-exam-portal\code\manage-exam_export-to-csv.php</b> on line <b>98</b><br />
QUESTION TYPE QUESTION CORRECT ANSWER CHOSEN OPTION ID FIRST NAME LAST NAME SCORE(out of )
Multi-choice where is the sun located D
Multi-choice why is earth round A
Multi-choice why is earth created A
UserName bledza banda 5
here is the export-csv.php file,
<?php
require_once '../connect.php';
require_once '../session.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Check if the user is logged in
if (!isset($_SESSION['userID'])) {
// Redirect or handle unauthorized access
header('Location: ../session.php');
exit();
}
// Set the content-type to CSV and specify the filename for download
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=user_responses.csv");
header("Pragma: no-cache");
header("Expires: 0");
// Get the user ID from the session
$userID = $_SESSION['userID'];
$examID = $_POST['examID'];
// Fetch user responses to questions
$sql = "
SELECT
'Multi-choice' AS exam_type,
mcq.question AS question_text,
mcq.correct_answer AS correct_answer,
mcqr.response AS chosen_option,
mcqr.score AS marks
FROM
multi_choice_question mcq
LEFT JOIN multi_choice_response mcqr ON mcq.exam_id = mcqr.exam_id AND mcq.question_no = mcqr.question_no AND mcqr.assignee_id = '$userID'
WHERE
mcq.exam_id = $examID
UNION
SELECT
'Fill in the blank' AS exam_type,
fiq.question AS question_text,
NULL AS correct_answer,
fir.response AS chosen_option,
fir.score AS marks
FROM
fill_in_question fiq
LEFT JOIN fill_in_response fir ON fiq.exam_id = fir.exam_id AND fiq.question_no = fir.question_no AND fir.assignee_id = '$userID'
WHERE
fiq.exam_id = $examID
UNION
SELECT
'Theory' AS exam_type,
tq.question AS question_text,
NULL AS correct_answer,
tr.response AS chosen_option,
tr.score AS marks
FROM
theory_question tq
LEFT JOIN theory_response tr ON tq.exam_id = tr.exam_id AND tq.question_no = tr.question_no AND tr.assignee_id = '$userID'
WHERE
tq.exam_id = $examID";
// Add the query to retrieve user information and total score
$sql .= "
UNION
SELECT
'UserName' AS exam_type,
'' AS question_text,
'' AS correct_answer,
CONCAT(user.first_name, ' ', user.last_name) AS chosen_option,
exam_assignment.total_score AS total_score
FROM
user
INNER JOIN exam_assignment ON user.user_id = exam_assignment.assignee_id
WHERE
exam_assignment.status_id = 6
AND exam_assignment.exam_id = $examID";
$result = mysqli_query($conn, $sql);
if (!$result) {
echo mysqli_error($conn);
exit();
}
// Create the output file (without saving it)
$dataFile = fopen('php://output', 'w');
if ($result->num_rows > 0) {
// Output the header
//fputcsv($dataFile, array("QUESTION TYPE", "QUESTION", "CORRECT ANSWER", "CHOSEN OPTION", "SCORE(out of $totalMark)"));
fputcsv($dataFile, array("QUESTION TYPE", "QUESTION", "CORRECT ANSWER", "CHOSEN OPTION","ID", "FIRST NAME", "LAST NAME" , "SCORE(out of $total_score)"));
// Output the header
// fputcsv($dataFile, array("ID", "FIRST NAME", "LAST NAME" ,"SCORE (out of $totalMark)"));
// Output the data
while ($row = $result->fetch_assoc()) {
fputcsv($dataFile, $row);
}
} else {
echo "0 results";
}
// Close the file handle
fclose($dataFile);
// Close the database connection
mysqli_close($conn);
?>
[Mod Edit: added ``` quotes to format the code]