<?php
include "../include/config.php";

if (isset($_POST['update'])) {
    $ids = $_POST['id'];
    $studentIds = $_POST['studentid'];
    $intakes = $_POST['intake'];
    $terms = $_POST['term'];
    $years = $_POST['year'];
    $subjects = $_POST['subject'];
    $cas = $_POST['ca'];
    $cas1 = $_POST['ca1'];
    $cas2 = $_POST['ca2'];
    $exams = isset($_POST['exam']) ? $_POST['exam'] : array_fill(0, count($ids), '');

    for ($i = 0; $i < count($ids); $i++) {
        $id = mysqli_real_escape_string($conn, $ids[$i]);
        $studentID = mysqli_real_escape_string($conn, $studentIds[$i]);
        $intake = mysqli_real_escape_string($conn, $intakes[$i]);
        $term = mysqli_real_escape_string($conn, $terms[$i]);
        $year = mysqli_real_escape_string($conn, $years[$i]);
        $subject = mysqli_real_escape_string($conn, $subjects[$i]);
        $ca = mysqli_real_escape_string($conn, trim($cas[$i]));
        $ca1 = mysqli_real_escape_string($conn, trim($cas1[$i]));
        $ca2 = mysqli_real_escape_string($conn, trim($cas2[$i]));
        $exam = mysqli_real_escape_string($conn, trim($exams[$i]));

        $updateQuery = "UPDATE `results` 
                        SET 
                            `studentid` = '$studentID', 
                            `intake` = '$intake',
                            `term` = '$term',
                            `year` = '$year',
                            `course` = '$subject',
                            `ca` = '$ca', 
                            `ca1` = '$ca1', 
                            `ca2` = '$ca2',
                            `exam` = '$exam'
                        WHERE `id` = '$id'";

        if (!mysqli_query($conn, $updateQuery)) {
            die("Error updating record: " . mysqli_error($conn));
        }
    }

    header("Location: push.php?success=Results successfully updated");
    exit();
}
?>

<?php
include "../include/config.php";

if (isset($_POST['updatestudent'])) {
    $id = intval($_POST['id']);
    $studentid = $_POST['studentid'];
    $studentname = $_POST['studentname'];
    $intake = $_POST['intake'];
    $program = $_POST['program'];
    $mobile = $_POST['mobile'];
    $email = $_POST['email'];
    $pass = trim($_POST['pass']);
    $status = trim($_POST['status']);

    // If password field is not empty, hash and update
    if (!empty($pass)) {
        $hashed = password_hash($pass, PASSWORD_DEFAULT);
        $sql = "UPDATE students SET studentid='$studentid', studentname='$studentname', intake='$intake', program='$program', mobile='$mobile', email='$email', pass='$hashed'WHERE id='$id'";
    } else {
        $sql = "UPDATE students SET studentid='$studentid', studentname='$studentname', intake='$intake', program='$program', mobile='$mobile', email='$email',status='$status'  WHERE id='$id'";
    }

    if (mysqli_query($conn, $sql)) {
        header("Location: students.php?id=$id&success=Student updated successfully");
    } else {
        header("Location: students.php?id=$id&error=Error updating student");
    }
}
?>
