<?php
// Database connection
header("Content-Type: application/json");
$host = "localhost"; // Database host
$db = "myprojec_patient"; // Your database name
$user = "myprojec_allytec"; // Your database username
$pass = "allytech@2019"; // Your database password

// Create connection
$conn = new mysqli($host, $user, $pass, $db);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$data = json_decode(file_get_contents("php://input"), true);

// Retrieve data from the incoming request
$random_number = $data['random_number'];
$name = $data['Name'];
$emailid = $data['emailid'];
$contactno = $data['contactno'];
$whatsappno = isset($data['whatsappno']) ? $data['whatsappno'] : null;
$address = isset($data['address']) ? $data['address'] : null;
$aadharno = isset($data['aadharno']) ? $data['aadharno'] : null;
$dob = isset($data['dob']) ? $data['dob'] : null;
$password =isset($data['password']) ; // Hash password

// Check if all required fields are present
if (empty($random_number) || empty($name) || empty($emailid) || empty($contactno) || empty($password)) {
    echo json_encode(['message' => 'All fields are required']);
    exit;
}

// Prepare the SQL query to insert the data
$sql = "INSERT INTO newdonar (random_number, name, emailid, contactno, whatsappno, address, aadharno, dob, password)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

$stmt = $conn->prepare($sql);
$stmt->bind_param("sssssssss", $random_number, $name, $emailid, $contactno, $whatsappno, $address, $aadharno, $dob, $password);

// Execute the query and check if it was successful
if ($stmt->execute()) {
    echo json_encode(['message' => 'User registered successfully']);
} else {
    echo json_encode(['message' => 'Error: ' . $stmt->error]);
}

// Close the statement and connection
$stmt->close();
$conn->close();
?>
