Home > Article > Backend Development > PHP implements the question supplement and modification functions in the knowledge question and answer website.
PHP implements the question supplement and modification functions in the knowledge question and answer website
In the knowledge question and answer website, users can ask questions and answer them. However, sometimes users may encounter situations where they need to add more information or modify existing questions. Therefore, in order to improve user experience, we need to implement question supplement and modification functions.
First of all, we need a question page so that users can view and modify existing questions. On this page, users can see the details of the question and existing answers. We can implement this question page with the following code example.
<?php // 获取问题和回答的数据 $questionId = $_GET['id']; // 获取问题的ID $question = getQuestionById($questionId); // 根据ID获取问题信息 $answers = getAnswersByQuestionId($questionId); // 根据问题ID获取回答信息 // 显示问题和回答 echo "<h1>{$question['title']}</h1>"; echo "<p>{$question['content']}</p>"; foreach ($answers as $answer) { echo "<h3>回答:</h3>"; echo "<p>{$answer['content']}</p>"; } // 提供修改问题的表单 echo "<h3>修改问题:</h3>"; echo "<form action='update_question.php' method='POST'>"; echo "<input type='hidden' name='id' value='{$question['id']}' />"; echo "<label for='title'>标题:</label><br>"; echo "<input type='text' name='title' id='title' value='{$question['title']}' /><br>"; echo "<label for='content'>内容:</label><br>"; echo "<textarea name='content' id='content'>{$question['content']}</textarea><br>"; echo "<input type='submit' value='保存' />"; echo "</form>"; ?>
In the above code, we first obtain the question and answer data through the getQuestionById()
and getAnswersByQuestionId()
functions. We then use the echo
statement to display the question and answer on the page. Next, we provide a form that allows users to modify the title and content of the question. The form submission address is update_question.php
and is submitted through the POST method.
Next, we need to implement the update_question.php
file to handle the modification of the question.
<?php // 获取修改后的问题数据 $questionId = $_POST['id']; // 获取问题的ID $title = $_POST['title']; // 获取问题的标题 $content = $_POST['content']; // 获取问题的内容 // 更新问题的数据 updateQuestion($questionId, $title, $content); // 更新问题信息 // 跳转回问题页面 header("Location: question.php?id={$questionId}"); ?>
In the above code, we obtain the modified question data through $_POST
. Then, we call the updateQuestion()
function to update the modified question information into the database. Finally, we use the header
function to jump back to the question page.
So far, we have implemented the code for the question supplement and modification functions in the knowledge question and answer website. Users can view questions and answers on the question page, and modify the title and content of the question through the modification form. Code examples can be modified to meet the needs of a specific website.
Summary:
The question supplement and modification functions in the knowledge Q&A website can improve the user experience. Through reasonable code design and implementation, users can easily modify existing problems to better meet their needs. Such functional design helps the development of the community and the improvement of content.
The above is the detailed content of PHP implements the question supplement and modification functions in the knowledge question and answer website.. For more information, please follow other related articles on the PHP Chinese website!