<?php
require 'db.php'; // include your database connection

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);

    // Prevent duplicate entries
    $check = $conn->query("SELECT * FROM subscribers WHERE email='$email'");
    if ($check->num_rows > 0) {
        echo "<script>
            alert('You have already subscribed!');
            window.location.href='index.php';
        </script>";
        exit;
    }

    // Insert into database
    $insert = $conn->query("INSERT INTO subscribers (email, phone) VALUES ('$email', '$phone')");

    if ($insert) {
        echo "<script>
            alert('Subscription successful! You will now receive updates.');
            window.location.href='index.php';
        </script>";
    } else {
        echo "<script>
            alert('Something went wrong, try again.');
            window.location.href='index.php';
        </script>";
    }
}
?>
