How to use PHP to develop a simple online questionnaire function

王林
Release: 2023-09-20 13:42:02
Original
1124 people have browsed it

How to use PHP to develop a simple online questionnaire function

How to use PHP to develop a simple online questionnaire function

In modern society, questionnaires are a common method of data collection, whether in academic research or market Surveys and user feedback are both inseparable from questionnaires. With the popularization of the Internet, more and more questionnaires have begun to shift to online forms, which not only saves the cost of paper questionnaires, but also makes it easier to count and analyze data. This article will introduce how to use PHP to develop a simple online questionnaire function and provide specific code examples.

1. Database design

First, we need to design a database to store the questions and answers of the questionnaire. Database tables can be divided into two: questions table (questions) and answers table (answers).

The fields of the question table are designed as follows:

id - the unique identifier of the question
question - the content of the question
created_at - the creation time of the question

Answer The fields of the table are designed as follows:

id - the unique identifier of the answer
question_id - the id of the associated question table
answer - the content of the answer
created_at - the creation time of the answer

2. Create a questionnaire page

In PHP, we can use HTML and CSS to create the layout and style of the questionnaire page. The following is a simple example:

<!DOCTYPE html>
<html>
<head>
  <title>在线调查问卷</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

  <h1>在线调查问卷</h1>

  <form action="submit.php" method="post">
    <p>问题一:</p>
    <input type="text" name="question1">

    <p>问题二:</p>
    <input type="text" name="question2">

    <p>问题三:</p>
    <input type="text" name="question3">

    <p><input type="submit" value="提交"></p>
  </form>

</body>
</html>
Copy after login

3. Processing questionnaire submission

In the above questionnaire page, we set up a form and specified the action of the submit button as submit.php. Next, we need to create the submit.php file to handle the submission of the questionnaire.

<?php
// 连接数据库
$db = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// 获取问卷数据并插入到数据库
$question1 = $_POST['question1'];
$question2 = $_POST['question2'];
$question3 = $_POST['question3'];

// 插入问题数据
$stmt1 = $db->prepare('INSERT INTO questions (question) VALUES (?)');
$stmt1->execute([$question1]);

$stmt2 = $db->prepare('INSERT INTO questions (question) VALUES (?)');
$stmt2->execute([$question2]);

$stmt3 = $db->prepare('INSERT INTO questions (question) VALUES (?)');
$stmt3->execute([$question3]);

// 获取最新插入问题的id
$questionId1 = $db->lastInsertId();
$questionId2 = $db->lastInsertId();
$questionId3 = $db->lastInsertId();

// 处理答案数据
$answer1 = $_POST['answer1'];
$answer2 = $_POST['answer2'];
$answer3 = $_POST['answer3'];

// 插入答案数据
$stmt4 = $db->prepare('INSERT INTO answers (question_id, answer) VALUES (?, ?)');
$stmt4->execute([$questionId1, $answer1]);

$stmt5 = $db->prepare('INSERT INTO answers (question_id, answer) VALUES (?, ?)');
$stmt5->execute([$questionId2, $answer2]);

$stmt6 = $db->prepare('INSERT INTO answers (question_id, answer) VALUES (?, ?)');
$stmt6->execute([$questionId3, $answer3]);

// 提交成功提示
echo "问卷提交成功!";
?>
Copy after login

4. Display the questionnaire results

We can use PHP to query the database and display the questionnaire results in the form of a bar chart. The following is a simple sample code:

<?php
// 连接数据库
$db = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// 查询问题数据
$stmt = $db->query('SELECT * FROM questions');

// 打印问卷问题
foreach ($stmt as $row) {
  echo "<p>问题:".$row['question']."</p>";

  // 查询答案数据
  $stmt2 = $db->prepare('SELECT * FROM answers WHERE question_id = ?');
  $stmt2->execute([$row['id']]);

  // 打印答案数量
  $count = 0;
  foreach ($stmt2 as $row2) {
    $count++;
  }

  echo "<p>答案数量:".$count."</p>";
}
?>
Copy after login

Summary

Through the above steps, we have implemented a simple online questionnaire function. When the user submits the questionnaire, the data will be stored in the database. We can also query the database through PHP and display the questionnaire results. Of course, this is just a simple example. In actual applications, the functions can be expanded and optimized according to needs. I hope this article will be helpful to beginners who use PHP to develop online questionnaire functions.

The above is the detailed content of How to use PHP to develop a simple online questionnaire function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template