<?php
    include('session.php');

    if ($_POST['action'] === 'saveRecord') {
        $strname= strtoupper($_POST['name']);
        $strnric = strtoupper($_POST['nric']);
        $strage= strtoupper($_POST['age']);
        $stremail = strtoupper($_POST['email']);
        $strcontactno = strtoupper($_POST['contactno']);
        $strschool = strtoupper($_POST['school']);
        $strcat= strtoupper($_POST['category']);
        
        // Check if patient with same NRIC and branch already exists
        $sqlCheck = "SELECT * FROM $tblquizict2026 WHERE NRICMyKid = ?";
        $stmtCheck = $conn->prepare($sqlCheck);
        $stmtCheck->bind_param("s", $strnric);
        $stmtCheck->execute();
        $resultCheck = $stmtCheck->get_result();
    
        if ($resultCheck->num_rows > 0) {
            echo "Record available!";
        } else {

            $sql = "INSERT INTO $tblquizict2026 (FullName,NRICMyKid,Age,EmailAdd,ContactNo,SchoolName,Category) VALUES (?, ?, ?, ?, ?, ?, ?)";
            $stmtInsert = $conn->prepare($sql);
            $stmtInsert->bind_param("sssssss", $strname, $strnric, $strage,$stremail,$strcontactno,$strschool,$strcat);
    
            if ($stmtInsert->execute()) {
                echo "Record saved!";
            } else {
                echo "ERROR : " . $stmtInsert->error;
            }
        }

        $conn->close();
    }

?>
