I’m trying to set up a subscriber emailing list on my website. The php code appears to work on my localhost but not on the live server.
Nothing is appearing in the DB????
Here’s my code:
<?php
if (isset($_POST['Subscribe'])) {
// Assuming the submit button/hidden field name is "Subscribe"
$userEmail = $_POST['email'];
// Validate email address (optional but recommended)
if (filter_var($userEmail, FILTER_VALIDATE_EMAIL)) {
// Database configuration (replace sensitive details with environment variables)
$servername = "db5016690051.hosting-data.io";
$username = "dbu2398034";
$password = "xxxxxxxxxxx";
$dbname = "dbs13515622";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . htmlspecialchars($conn->connect_error));
}
// Prepare and execute an SQL statement to insert the email into the database
$sql = "INSERT INTO `subscriber` (email) VALUES (?)";
$stmt = $conn->prepare($sql);
if ($stmt) {
$stmt->bind_param("s", $userEmail);
try {
$stmt->execute();
echo '<p>Email address successfully added to the list.</p>';
} catch (Exception $e) {
echo '<p>Error adding email address: ' . htmlspecialchars($e->getMessage()) . '</p>';
}
$stmt->close();
} else {
echo '<p>Failed to prepare the SQL statement.</p>';
}
// Close the connection
$conn->close();
} else {
echo '<p>Invalid email address. Please try again.</p>';
}
}
?>
I have tried different codes, Ionos help line and nothing is working!