Home  >  Article  >  Backend Development  >  How to use PHP to implement a simple online questionnaire system

How to use PHP to implement a simple online questionnaire system

WBOY
WBOYOriginal
2023-09-25 09:16:41704browse

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.

  1. Create database

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:

  • id: integer type, auto-incrementing primary key.
  • name: String type, used to store the user's name.
  • email: String type, used to store the user's email address.
  • answer: String type, used to store the user's answer to the question.
  1. Design the questionnaire

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".

  1. Create HTML forms

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:




Online Questionnaire System


Please answer the following questions:













Satisfied

Not satisfied




> ;
  1. Create a PHP submission handler

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 .

  1. Display the results

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn