Du måste lägga till en parameter $conn i din funktion quiz_test() så fungerar det
Prova nedanstående kod
<?php
session_start();
?>
<?php
include('config1.php');
$category_id = 1;
$AnswerID = $_POST['AnswerID'];
$questionid = $_POST['questionid'];
$timetaken = $_POST['timetaken'];
$limit = $_POST['limit'];
echo "$limit";
$bd = "$limit";
switch ($bd) {
case"1":
$sql = "INSERT INTO results (id, user_id, category_id, q_id, answer_id, time_taken)
VALUES (',', '".$_SESSION['id']."', '$category_id', '$questionid', '$AnswerID', '$timetaken')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$last_id = mysqli_insert_id($conn);
echo "Last inserted ID is: " . $last_id;
// Set session variables
$_SESSION["last_id"] = "$last_id";
break;
case"2":
quiz_test($conn);
break;
case"3":
quiz_test($conn);
break;
case"4":
quiz_test($conn);
break;
case"5":
quiz_test($conn);
unset($_SESSION['last_id']);
break;
default:
echo "something is wrong";
}
function quiz_test($conn){
$sql = "SELECT q_id, answer_id, time_taken FROM results WHERE id='" . $_SESSION["last_id"] . "'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$qid=$rows['q_id'];
$ans=$rows['answer_id'];
$time=$rows['time_taken'];
}
}
$conn->query("update results set q_id =('$questionid','$qid'),answer_id = ('$AnswerID','$ans'),time_taken=('$timetaken','$time') where id = '" . $_SESSION["last_id"] . "'");
}
?>