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.
Specific code implementation:
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>
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()); } ?>
Publish questionnaire
Code example:
<a href="survey.php?id=1">点击参与问卷调查</a>
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()); } ?>
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()); } ?>
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()); } ?>
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!