Home >Backend Development >PHP Tutorial >How to use PHP to implement a simple online questionnaire system
Using PHP to implement a simple online questionnaire system
With the popularity of the Internet, more and more institutions and organizations choose to use online questionnaires to collect users Feedback and comments. As a popular server-side scripting language, PHP can be used to build dynamic websites and applications, or it can also be used to build a simple online questionnaire system. In this article, we will discuss how to implement such a system using PHP and provide specific code examples.
First, we need to create a database to store the questionnaire data submitted by users. We can use MySQL as the database management system. Create a database named "questionnaire" in MySQL and create a table named "responses". The table can contain the following fields:
The next step is to design the questionnaire and determine what information needs to be collected. Suppose our questionnaire has only one question, "Are you satisfied with our product?". Users can choose to answer "satisfied" or "dissatisfied".
Use HTML forms to collect user responses. In the form, we need to include a text box for the user to enter their name, a text box for the user to enter their email address, and a radio button group for the user to select a response. The following is a simple HTML form example:
We create a PHP file named "submit.php" to handle users Questionnaire submitted. In the file, we first connect to the database and then get the name, email address, and answer submitted by the user. Next, insert this information into the "responses" table in the database. Here is an example of a simple PHP submission handler:
// Connect to the database
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "questionnaire";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn- >connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the name, email address and answer submitted by the user
$name = $_POST['name'];
$email = $_POST['email'];
$answer = $_POST['answer'];
// Insert information Database
$sql = "INSERT INTO responses (name, email, answer) VALUES ('$name', '$email', '$answer')";
if ($conn->query($ sql) === true) {
echo "Submission successful!";
} else {
echo "Submission failed:" . $conn->error;
}
// Close the database connection
$conn->close();
?>
Please note that the database connection information in the above code snippet needs to be modified according to your own environment .
Finally, we can create a simple PHP file to retrieve the survey results from the database and display them to the user. The following is an example of a simple results display program:
// Connect to the database
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "questionnaire";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn-> ;connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve questionnaire results from the database
$sql = "SELECT COUNT(*) AS total, answer FROM responses GROUP BY answer";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//Output result
while($row = $result->fetch_assoc()) {
echo $row["answer "]. ": " . $row["total"]. "
";
}
} else {
echo "No results yet";
}
//Close the database connection
$conn->close();
?>
Please modify the database connection information in the above code snippet.
In this article, we learned how to use PHP to implement a simple questionnaire system, including creating a database, designing a questionnaire, creating an HTML form, creating a PHP submission handler, and displaying the results. By referring to the above code examples, you can easily start building your own online questionnaire system. Of course, this is just a simple example and you can extend and improve it to suit your needs.
Summary:
Online questionnaire systems can help institutions and organizations better understand user needs and feedback. By using PHP, we can quickly build a simple questionnaire system and obtain useful information from user-submitted data. I hope this article was helpful and I wish you success in building your own questionnaire system!
The above is the detailed content of How to use PHP to implement a simple online questionnaire system. For more information, please follow other related articles on the PHP Chinese website!