How can you create a survey in PHP that sends results via email or stores them in a database?
To create a survey in PHP that sends results via email or stores them in a database, you can use HTML forms to collect survey responses and PHP to process and store/send the results. You can use PHP's built-in mail function to send survey responses via email or connect to a database using MySQLi or PDO to store the results.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process survey responses
$response1 = $_POST['response1'];
$response2 = $_POST['response2'];
// Send results via email
$to = "youremail@example.com";
$subject = "Survey Results";
$message = "Response 1: $response1\nResponse 2: $response2";
mail($to, $subject, $message);
// Or store results in a database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "survey_results";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "INSERT INTO responses (response1, response2) VALUES ('$response1', '$response2')";
if ($conn->query($sql) === TRUE) {
echo "Survey results stored successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
Related Questions
- What role does the PHP version play in ensuring the compatibility of a script across different servers?
- What are some best practices for handling object-oriented XML generation in PHP to avoid common parsing errors in different browsers?
- Are there any security concerns related to directly using user input in SQL queries, as shown in the code example?