Home  >  Article  >  Backend Development  >  How to develop a simple user feedback function using PHP

How to develop a simple user feedback function using PHP

王林
王林Original
2023-09-21 13:54:111476browse

How to develop a simple user feedback function using PHP

How to use PHP to develop a simple user feedback function

With the continuous development of Internet technology, user feedback functions have become more and more important for the development of websites and applications. important. Through user feedback, developers can understand users' opinions and suggestions on products, and adjust and improve products in a timely manner. In this article, we'll cover how to develop a simple user feedback feature using PHP.

1. Preparation
Before you start, you need to make sure that you have installed PHP and MySQL and are familiar with their basic use. In addition, a MySQL data table containing the following fields is also required:

  • id (self-increasing primary key)
  • name (varchar, user name)
  • email (varchar , user email)
  • content (text, feedback content)
  • created_at (datetime, feedback creation time)

2. Create the feedback page
First , create an HTML form page for users to fill in feedback information. The form contains three input boxes for name, email address and feedback content, as well as a submit button.

<!DOCTYPE html>
<html>
<head>
<title>用户反馈</title>
</head>
<body>
<h2>用户反馈</h2>
<form action="submit_feedback.php" method="post">
<label for="name">姓名:</label>
<input type="text" name="name" required><br><br>
<label for="email">邮箱:</label>
<input type="email" name="email" required><br><br>
<label for="content">反馈内容:</label><br>
<textarea name="content" rows="5" cols="50" required></textarea><br><br>
<input type="submit" value="提交反馈">
</form>
</body>
</html>

3. Processing feedback information
Next, create a PHP file named submit_feedback.php for processing feedback information submitted by users. First, get the data from the form.

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$content = $_POST['content'];

Next, insert the data into the MySQL database.

<?php
// 建立数据库连接
$servername = "localhost";
$username = "root";
$password = "your_password";
$dbname = "your_database";

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

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

// 插入数据到反馈表中
$sql = "INSERT INTO feedback (name, email, content, created_at) VALUES ('$name', '$email', '$content', NOW())";

if ($conn->query($sql) === TRUE) {
    echo "感谢您的反馈!";
} else {
    echo "反馈提交失败:" . $conn->error;
}

// 关闭数据库连接
$conn->close();

?>

4. Display the feedback list
Finally, create a PHP file named feedback_list.php to display the feedback list submitted by the user. Get all feedback records from the database and display them in table form.

<?php
// 建立数据库连接
$servername = "localhost";
$username = "root";
$password = "your_password";
$dbname = "your_database";

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

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

// 查询反馈列表
$sql = "SELECT * FROM feedback";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table>";
    echo "<tr><th>ID</th><th>姓名</th><th>邮箱</th><th>反馈内容</th><th>创建时间</th></tr>";
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row['id']."</td><td>".$row['name']."</td><td>".$row['email']."</td><td>".$row['content']."</td><td>".$row['created_at']."</td></tr>";
    }
    echo "</table>";
} else {
    echo "暂无反馈记录";
}

// 关闭数据库连接
$conn->close();

?>

Visit feedback_list.php in your browser to see the submitted feedback list.

The above are the steps and code examples for using PHP to develop a simple user feedback function. Through this function, you can easily collect users' opinions and suggestions to provide reference for product improvement. Of course, in actual projects, issues such as security and data verification also need to be considered. Here is just a simple example. I hope it will be helpful to your learning and development!

The above is the detailed content of How to develop a simple user feedback function 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