<?php
// Replace with your database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "quietpools";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

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

// Create visitors table if it doesn't exist
$sql = "CREATE TABLE IF NOT EXISTS visitors (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  fullname VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  phone VARCHAR(20) NOT NULL
)";

if ($conn->query($sql) === FALSE) {
  echo "Error creating table: " . $conn->error;
}

// Process form submission
if (isset($_POST['submit'])) {
  // Escape user inputs to prevent SQL injection
  $fullname = mysqli_real_escape_string($conn, $_POST['fullname']);
  $email = mysqli_real_escape_string($conn, $_POST['email']);
  $phone = mysqli_real_escape_string($conn, $_POST['phone']);

  // Insert visitor data into visitors table
  $sql = "INSERT INTO visitors (fullname, email, phone)
          VALUES ('$fullname', '$email', '$phone')";

  if ($conn->query($sql) === FALSE) {
    echo "Error: " . $sql . "<br>" . $conn->error;
  } else {
    echo "Visitor data saved successfully";
  }
}

// Close connection
$conn->close();
?>
