
<?php
session_start();
include "../include/config.php";
$userlogin = $_SESSION['studentid'] ?? '';

// Fetch current mode
$mode = '';
if ($userlogin) {
    $res = mysqli_query($conn, "SELECT mode FROM students WHERE studentid='$userlogin'");
    if ($res && mysqli_num_rows($res) > 0) {
        $row = mysqli_fetch_assoc($res);
        $mode = $row['mode'] ?? '';
    }
}

// Handle AJAX update
if (isset($_POST['mode'])) {
    $mode_post = mysqli_real_escape_string($conn, $_POST['mode']);
    $update = mysqli_query($conn, "UPDATE students SET mode='$mode_post' WHERE studentid='$userlogin'");
    if ($update) {
        header("location:dashboard.php");
    } else {
        echo "Database error. Try again.";
    }
    exit;
}
?>
