How to use PHP to develop simple online questionnaires and data analysis functions

WBOY
Release: 2023-09-21 12:04:01
Original
1335 people have browsed it

How to use PHP to develop simple online questionnaires and data analysis functions

How to use PHP to develop simple online questionnaires and data analysis functions

Introduction:
With the popularity of the Internet, more and more companies and individuals Start focusing on questionnaires and data analysis. Using PHP as a development tool can quickly implement online questionnaires and data analysis functions. This article will introduce you to how to use PHP to develop simple online questionnaires and data analysis functions, and give specific code examples.

1. Requirements Analysis
Before starting development, we need to clarify the requirements and determine what functions our system should have.

  1. Create questionnaire: Users can create customized questionnaires as needed, including question types, options, etc.
  2. Publish questionnaire: Users can publish the created questionnaire to the Internet for others to fill in.
  3. Collect data: The system can automatically collect the data filled in the questionnaire and save it to the database.
  4. Data analysis: Users can make statistics and analyze the collected data through the data analysis function provided by the system.
  5. Result display: Users can display statistical results through the system, including charts, tables, etc.

Specific code implementation:

  1. Create questionnaire
    Code example:

    <form action="create_survey.php" method="POST">
     <label for="title">问卷标题:</label>
     <input type="text" name="title"><br>
     
     <label for="question1">问题1:</label>
     <input type="text" name="question1"><br>
     
     <input type="submit" value="创建问卷">
    </form>
    Copy after login

    create_survey.php file:

<?php
$title = $_POST['title'];
$question1 = $_POST['question1'];
// 连接数据库
$dsn = 'mysql:host=localhost;dbname=survey';
$username = 'root';
$password = '';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 插入数据
    $sql = "INSERT INTO surveys (title, question1) VALUES (:title, :question1)";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':title', $title);
    $stmt->bindParam(':question1', $question1);
    $stmt->execute();

    echo "调查问卷创建成功!";
} catch (PDOException $e) {
    die("连接数据库失败: " . $e->getMessage());
}
?>
Copy after login
  1. Publish questionnaire
    Code example:

    <a href="survey.php?id=1">点击参与问卷调查</a>
    Copy after login

    survey.php file:

    <?php
    // 获取调查问卷ID
    $id = $_GET['id'];
    
    // 根据ID查询调查问卷标题和问题
    $dsn = 'mysql:host=localhost;dbname=survey';
    $username = 'root';
    $password = '';
    
    try {
     $pdo = new PDO($dsn, $username, $password);
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
     // 查询数据
     $sql = "SELECT * FROM surveys WHERE id = :id";
     $stmt = $pdo->prepare($sql);
     $stmt->bindParam(':id', $id);
     $stmt->execute();
     $survey = $stmt->fetch();
    
     if ($survey) {
         echo "<h1>{$survey['title']}</h1>";
         echo "<h2>问题1:{$survey['question1']}</h2>";
         echo "<form action='submit_survey.php' method='POST'>";
         echo "<input type='hidden' name='id' value='$id'>";
         echo "<label for='answer1'>您的回答:</label>";
         echo "<input type='text' name='answer1'><br>";
         echo "<input type='submit' value='提交'>";
         echo "</form>";
     } else {
         echo "调查问卷不存在!";
     }
    } catch (PDOException $e) {
     die("连接数据库失败: " . $e->getMessage());
    }
    ?>
    Copy after login
  2. Collect data
    submit_survey.php file:

    <?php
    $id = $_POST['id'];
    $answer1 = $_POST['answer1'];
    
    $dsn = 'mysql:host=localhost;dbname=survey';
    $username = 'root';
    $password = '';
    
    try {
     $pdo = new PDO($dsn, $username, $password);
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
     $sql = "INSERT INTO survey_responses (survey_id, answer1) VALUES (:survey_id, :answer1)";
     $stmt = $pdo->prepare($sql);
     $stmt->bindParam(':survey_id', $id);
     $stmt->bindParam(':answer1', $answer1);
     $stmt->execute();
    
     echo "问卷提交成功!";
    } catch (PDOException $e) {
     die("连接数据库失败: " . $e->getMessage());
    }
    ?>
    Copy after login
  3. Data analysis
    analysis.php file:

    <?php
    $dsn = 'mysql:host=localhost;dbname=survey';
    $username = 'root';
    $password = '';
    
    try {
     $pdo = new PDO($dsn, $username, $password);
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
     // 查询所有问卷的数据
     $sql = "SELECT * FROM survey_responses";
     $stmt = $pdo->query($sql);
     $responses = $stmt->fetchAll();
    
     // 统计回答结果
     $answer1_counts = array();
     foreach ($responses as $response) {
         $answer1 = $response['answer1'];
         if (!isset($answer1_counts[$answer1])) {
             $answer1_counts[$answer1] = 0;
         }
         $answer1_counts[$answer1]++;
     }
    
     // 打印统计结果
     echo "<h1>问卷数据分析结果</h1>";
     echo "<h2>问题1:</h2>";
     foreach ($answer1_counts as $answer1 => $count) {
         echo "<p>$answer1: $count</p>";
     }
    } catch (PDOException $e) {
     die("连接数据库失败: " . $e->getMessage());
    }
    ?>
    Copy after login
  4. Result display
    By in analysis. Adding the code of a chart display library (such as Chart.js) to PHP can display statistical results in the form of charts.

Conclusion:
It is very easy to develop simple online questionnaires and data analysis functions using PHP. This article provides code examples for creating questionnaires, publishing questionnaires, collecting data, data analysis and result display. Readers can conduct secondary development based on these sample codes to achieve more complete functions and effects.

Of course, the code example in this article is just an entry-level implementation. In actual projects, more issues such as security, user experience, and data processing may need to be considered. I hope this article can bring some inspiration and guidance to readers and help them better implement online questionnaire and data analysis functions.

The above is the detailed content of How to use PHP to develop simple online questionnaires and data analysis functions. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!