<?php
// Connect to your database 
require('shikanisha.kts.php');

// Fetch form data
$ageGroup = $_POST['age_group'];
$cost = $_POST['cost'];

// Check if age_group already exists
$checkQuery = "SELECT age_group FROM swimming WHERE age_group = '$ageGroup'";
$checkResult = $conn->query($checkQuery);

if ($checkResult->num_rows > 0) {
    // Age group already exists, send feedback
    $response = array("success" => false, "message" => "Age group already exists");
} else {
    // Age group does not exist, proceed to insert data
    $insertQuery = "INSERT INTO swimming (age_group, cost) VALUES ('$ageGroup', $cost)";
    
    if ($conn->query($insertQuery) === TRUE) {
        $response = array("success" => true);
    } else {
        $response = array("success" => false, "message" => "Error inserting data");
    }
}

// Close the database connection
$conn->close();

// Send JSON response back to the JavaScript
header('Content-Type: application/json');
echo json_encode($response);
?>
