connect_error) { die("Connection failed: " . $conn->connect_error); } // Fetch users from the database $sql = "SELECT * FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "

Mathematics Results

"; echo ""; echo ""; echo ""; echo ""; // Display a form for each student to enter their Mathematics results while ($row = $result->fetch_array()) { echo ""; echo ""; echo ""; echo ""; // Hidden field to pass the user ID echo ""; echo ""; echo ""; echo ""; } echo "
First NameLast NameMathematics
".$row['first_name']."".$row['last_name'].""; echo "
"; } // Check if the form was submitted for Mathematics marks if ($_SERVER['REQUEST_METHOD'] == "POST") { // Get the user ID and marks from the form submission $user_id = $_POST['user_id']; $math_marks = $_POST['Mathematics']; // Ensure that math_marks is not empty if (!empty($math_marks)) { // Update or insert the user's Mathematics marks in the database // Assuming there is a 'marks' table that stores marks for different subjects // If no record exists for the user in the marks table, you'll insert, otherwise, you'll update // Check if the user already has a record in the marks table $check_sql = "SELECT * FROM marks WHERE user_id = $user_id"; $check_result = $conn->query($check_sql); if ($check_result->num_rows > 0) { // Update existing record $update_sql = "UPDATE marks SET Mathematics = $math_marks WHERE user_id = $user_id"; if ($conn->query($update_sql) === TRUE) { echo "Mathematics marks updated successfully."; } else { echo "Error updating record: " . $conn->error; } } else { // Insert new record $insert_sql = "INSERT INTO marks (user_id, Mathematics) VALUES ($user_id, $math_marks)"; if ($conn->query($insert_sql) === TRUE) { echo "Mathematics marks added successfully."; } else { echo "Error inserting record: " . $conn->error; } } } else { echo "Please enter valid Mathematics marks."; } } // Close the connection $conn->close(); ?>