Are there any best practices or guidelines to follow when setting up a mailing system in PHP that involves storing and retrieving email addresses from a database?
When setting up a mailing system in PHP that involves storing and retrieving email addresses from a database, it is important to sanitize user input to prevent SQL injection attacks. Additionally, it is recommended to use prepared statements to interact with the database to prevent SQL injection vulnerabilities. Lastly, ensure that you have proper error handling in place to handle any potential issues that may arise during the process.
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Sanitize user input
$email = mysqli_real_escape_string($conn, $_POST['email']);
// Prepare and execute SQL statement
$stmt = $conn->prepare("INSERT INTO emails (email) VALUES (?)");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
echo "Email address stored successfully";
} else {
echo "Error storing email address: " . $conn->error;
}
// Close connection
$stmt->close();
$conn->close();
Keywords
Related Questions
- Was sind bewährte Methoden, um sicherzustellen, dass eine Funktion in PHP korrekt aufgerufen und verwendet wird?
- How can PHP developers ensure that the selected option in a drop-down menu remains consistent after form submission?
- What are common resources for finding information on PHP functions like preg_match(), especially in German?