<?php
require_once '../../public/connection.php'; // Adjust path as needed

if (!isset($_GET['reg_number'])) {
   	$_SESSION['msg'] = "Admission number is required.";
   	$_SESSION['type'] = "error";    
    exit;
}

$reg_number = $_GET['reg_number'];

// Use prepared statement to prevent SQL injection
$stmt = $conn->prepare("SELECT * FROM students WHERE reg_number = ?");
$stmt->bind_param("s", $reg_number);
$stmt->execute();
$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    $response = [
        'success' => true,
        'fullname' => $row['firstname'] . ' ' . $row['othername'] . ' ' . $row['lastname'],
        'guardian_name' => $row['guardian_name'],
        'state' => $row['state'],
        'lga' => $row['lga'],
        'dob' => $row['dob'],
        'class' => $row['class'],
        'level' => $row['level'],
        'passport' => $row['passport']
    ];
    echo json_encode($response);
} else {
   	$_SESSION['msg'] = "No student is found.";
   	$_SESSION['type'] = "error";        
}

$stmt->close();
$conn->close();
?>
