PHP realizes the question discussion and answer editing functions in the knowledge Q&A website
In today's era of information explosion, the knowledge Q&A website has become an important platform for people to obtain knowledge and exchange experiences. For developers, implementing a question discussion and answer editing function similar to Stack Overflow is a very challenging task. This article explains how to implement such a feature using PHP and provides code examples.
First, we need to design and create a database to store question and answer information. In this example, we will use a MySQL database and create two tables: questions and answers.
CREATE TABLE questions ( id INT(11) AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, content TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE answers ( id INT(11) AUTO_INCREMENT PRIMARY KEY, question_id INT(11) NOT NULL, content TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
The questions table is used to store the title, content and creation time of the question, and the answers table is used to store the content of the answer, the ID of the question and the creation time.
Next, we need to write PHP code to display the issue list and issue details. First, we create a file called index.php that will display the list of questions.
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); // 查询所有问题 $query = "SELECT * FROM questions ORDER BY created_at DESC"; $result = mysqli_query($conn, $query); // 显示问题列表 while ($row = mysqli_fetch_assoc($result)) { echo "<h2><a href='question.php?id={$row['id']}'>{$row['title']}</a></h2>"; echo "<p>{$row['content']}</p>"; echo "<hr>"; } ?>
Then, we create a file called question.php to display the question details and related answers.
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); // 获取问题 ID $id = $_GET['id']; // 查询问题的详细信息 $query = "SELECT * FROM questions WHERE id = $id"; $result = mysqli_query($conn, $query); $question = mysqli_fetch_assoc($result); // 查询问题的答案列表 $query = "SELECT * FROM answers WHERE question_id = $id ORDER BY created_at DESC"; $result = mysqli_query($conn, $query); // 显示问题和答案 echo "<h1>{$question['title']}</h1>"; echo "<p>{$question['content']}</p>"; while ($row = mysqli_fetch_assoc($result)) { echo "<h3>答案</h3>"; echo "<p>{$row['content']}</p>"; echo "<hr>"; } ?>
Finally, we need to write PHP code to handle user-submitted questions and answers. Create a file called submit.php that handles user-submitted questions and answers and saves them to the database.
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 提交问题 if (isset($_POST['question'])) { $title = $_POST['title']; $content = $_POST['content']; $query = "INSERT INTO questions (title, content) VALUES ('$title', '$content')"; mysqli_query($conn, $query); } // 提交答案 if (isset($_POST['answer'])) { $question_id = $_POST['question_id']; $content = $_POST['content']; $query = "INSERT INTO answers (question_id, content) VALUES ($question_id, '$content')"; mysqli_query($conn, $query); } // 重定向到问题详情页面 header("Location: question.php?id=$question_id"); exit(); } ?>
Now, we can display the question list by accessing the index.php page, and display the question details and answers by accessing the question.php page. And, by submitting a form with questions and answers, the data is saved to the database.
Summary
It is not difficult to implement the question discussion and answer editing functions in a knowledge Q&A website through PHP. We only need to create a database to store the question and answer information, and write PHP code to Display and submit data. This article provides a simple example that you can extend and improve based on your own needs. I wish you success in implementing a powerful knowledge question and answer website!
The above is the detailed content of PHP implements the question discussion and answer editing functions in the knowledge Q&A website.. For more information, please follow other related articles on the PHP Chinese website!