在这个数据驱动的时代,问卷调查成为了获取用户反馈和数据分析的重要手段。本文将带你一步步用PHP打造一个简易但功能完备的在线问卷调查工具。
项目准备
首先,确保你的开发环境已经安装了PHP和MySQL。你可以使用XAMPP、WAMP等集成环境来快速搭建。
1. 数据库设计
我们需要一个数据库来存储问卷和用户回答。以下是简单的数据库设计:
CREATE DATABASE survey_tool;
USE survey_tool;
CREATE TABLE surveys (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE questions (
id INT AUTO_INCREMENT PRIMARY KEY,
survey_id INT,
question_text TEXT NOT NULL,
question_type ENUM('text', 'radio', 'checkbox') NOT NULL,
FOREIGN KEY (survey_id) REFERENCES surveys(id)
);
CREATE TABLE options (
id INT AUTO_INCREMENT PRIMARY KEY,
question_id INT,
option_text VARCHAR(255) NOT NULL,
FOREIGN KEY (question_id) REFERENCES questions(id)
);
CREATE TABLE responses (
id INT AUTO_INCREMENT PRIMARY KEY,
survey_id INT,
question_id INT,
response_text TEXT,
FOREIGN KEY (survey_id) REFERENCES surveys(id),
FOREIGN KEY (question_id) REFERENCES questions(id)
);
2. 项目结构
创建以下文件夹和文件结构:
/survey_tool
/includes
db.php
/pages
index.php
create_survey.php
take_survey.php
view_results.php
/styles
style.css
核心功能实现
1. 数据库连接
在includes/db.php
中配置数据库连接:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "survey_tool";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
2. 创建问卷
在create_survey.php
中实现问卷创建功能:
<?php
include '../includes/db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = $_POST['title'];
$description = $_POST['description'];
$sql = "INSERT INTO surveys (title, description) VALUES ('$title', '$description')";
if ($conn->query($sql) === TRUE) {
$survey_id = $conn->insert_id;
header("Location: add_questions.php?survey_id=$survey_id");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
<form method="post">
<label for="title">Title:</label>
<input type="text" name="title" required>
<label for="description">Description:</label>
<textarea name="description"></textarea>
<button type="submit">Create Survey</button>
</form>
3. 添加问题
在add_questions.php
中实现问题添加功能:
<?php
include '../includes/db.php';
$survey_id = $_GET['survey_id'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$question_text = $_POST['question_text'];
$question_type = $_POST['question_type'];
$sql = "INSERT INTO questions (survey_id, question_text, question_type) VALUES ('$survey_id', '$question_text', '$question_type')";
if ($conn->query($sql) === TRUE) {
echo "Question added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
<form method="post">
<label for="question_text">Question:</label>
<input type="text" name="question_text" required>
<label for="question_type">Type:</label>
<select name="question_type">
<option value="text">Text</option>
<option value="radio">Radio</option>
<option value="checkbox">Checkbox</option>
</select>
<button type="submit">Add Question</button>
</form>
4. 参与问卷
在take_survey.php
中实现问卷填写功能:
<?php
include '../includes/db.php';
$survey_id = $_GET['survey_id'];
$sql = "SELECT * FROM questions WHERE survey_id=$survey_id";
$result = $conn->query($sql);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
foreach ($_POST as $question_id => $response) {
$response_text = is_array($response) ? implode(", ", $response) : $
评论(0)