connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
// Function to sanitize input data
function sanitizeData($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Check if the form is submitted
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
// Get the email from the form
$email = sanitizeData($_POST[“email”]);
// Prepare and execute the SQL statement
$stmt = $conn->prepare(“INSERT INTO subscribers (email) VALUES (?)”);
$stmt->bind_param(“s”, $email);
$stmt->execute();
if ($stmt->affected_rows > 0) {
// Subscription successful
echo “Thank you for subscribing to our newsletter!”;
} else {
// Subscription failed
echo “Sorry, there was an error subscribing to the newsletter. Please try again.”;
}
// Close the statement
$stmt->close();
}
// Close the connection
$conn->close();
?>