Home  >  Article  >  Backend Development  >  How to implement a simple online survey and feedback system using PHP

How to implement a simple online survey and feedback system using PHP

王林
王林Original
2023-09-24 16:07:411448browse

How to implement a simple online survey and feedback system using PHP

How to use PHP to implement a simple online survey and feedback system

In the Internet era, survey and feedback systems are a way for companies and organizations to obtain user opinions and understand user needs. Important tool. This article will introduce how to use PHP language to implement a simple online survey and feedback system.

1. Create a database

First, we need to create a MySQL database to store user surveys and feedback information. You can use the following SQL statement to create a database named "survey" and create a table named "feedback" in it:

CREATE DATABASE survey;
USE survey;
CREATE TABLE feedback (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(50),
    message TEXT
);

2. Write an HTML form

Next, we You need to write an HTML form to allow users to enter survey and feedback information. The following is a simple form example:

<form action="submit.php" method="post">
    <label for="name">姓名:</label>
    <input type="text" name="name" id="name" required><br>
    
    <label for="email">邮箱:</label>
    <input type="email" name="email" id="email" required><br>
    
    <label for="message">留言:</label>
    <textarea name="message" id="message" rows="5" required></textarea><br>
    
    <input type="submit" value="提交">
</form>

3. Write PHP code

Next, we need to write PHP code to process the form data submitted by the user and store it in the database. The following is a simple example:

<?php
// 获取表单数据
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "survey";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 插入数据
$sql = "INSERT INTO feedback (name, email, message) VALUES ('$name', '$email', '$message')";

if ($conn->query($sql) === TRUE) {
    echo "提交成功!";
} else {
    echo "提交失败: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

In the above code, we first obtain the form data submitted by the user, then connect to the MySQL database and insert the form data into the "feedback" table.

4. Display the survey and feedback results

In order to display the survey and feedback results, we can write a PHP page to query the database and display the results. The following is a simple example:

<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "survey";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 查询数据
$sql = "SELECT name, email, message FROM feedback";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "姓名: " . $row["name"]. " - 邮箱: " . $row["email"]. " - 留言: " . $row["message"]. "<br>";
    }
} else {
    echo "暂无调查和反馈结果!";
}

$conn->close();
?>

The above code will query the survey and feedback results from the "feedback" table and display them on the page in the form of a list.

Summary:

Through the above steps, we can use PHP to implement a simple online survey and feedback system, allowing users to fill in the survey form and submit data, and at the same time display the results of the survey and feedback . Of course, this is just a simple example and you can extend and optimize it according to your needs.

The above is the detailed content of How to implement a simple online survey and feedback system using PHP. 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