Home >Backend Development >PHP Tutorial >Use PHP to develop question modification and deletion functions in the knowledge question and answer website.

Use PHP to develop question modification and deletion functions in the knowledge question and answer website.

王林
王林Original
2023-08-04 20:43:45645browse

Use PHP to develop the question modification and deletion functions in the knowledge question and answer website

With the development of the Internet, the knowledge question and answer website has become an important way for people to obtain knowledge and solve problems. In a knowledge question and answer website, users can ask questions and other users can answer questions, thus forming a platform for sharing knowledge. In order to improve the user experience and functional perfection of the website, we need to provide users with the ability to modify and delete problems. This article will introduce how to use PHP to develop question modification and deletion functions in a knowledge question and answer website.

First, we need to create a problem management page where users can modify and delete problems. In PHP, we can obtain the data passed by the page through the GET or POST method. Assuming that our issue management page is manage.php, we can get the issue ID through the following code:

$question_id = $_GET['id'];

Next, we need to query the database according to the issue ID to obtain the issue Details. Assuming that our questions are stored in the questions table of the database, we can use the following code to query the questions:

$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$statement = $pdo->prepare('SELECT * FROM questions WHERE id = :id');
$statement->bindParam(':id', $question_id);
$statement->execute();
$question = $statement->fetch(PDO::FETCH_ASSOC);

In the page, we can display the details of the question through a form, and Allow users to make modifications. Assuming that our form contains two fields: title and content, we can use the following code to display questions and allow users to make modifications:

<form action="update.php" method="POST">
  <input type="hidden" name="id" value="<?php echo $question['id']; ?>">
  <label for="title">标题:</label>
  <input type="text" name="title" value="<?php echo $question['title']; ?>"><br>
  <label for="content">内容:</label>
  <textarea name="content"><?php echo $question['content']; ?></textarea><br>
  <input type="submit" value="保存">
</form>

After the user clicks the save button, the form data will be submitted to update.php page. In the update.php page, we can get the data submitted by the form and update the question through the following code:

$question_id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];

$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$statement = $pdo->prepare('UPDATE questions SET title = :title, content = :content WHERE id = :id');
$statement->bindParam(':id', $question_id);
$statement->bindParam(':title', $title);
$statement->bindParam(':content', $content);
$statement->execute();

The above code will update the title and content of the question to the database.

Next, let’s implement the problem deletion function. In the issue management page, we can add a delete button that, when clicked by the user, will pass the ID of the issue to the delete.php page. In the delete.php page, we can use the following code to delete the issue:

$question_id = $_GET['id'];

$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$statement = $pdo->prepare('DELETE FROM questions WHERE id = :id');
$statement->bindParam(':id', $question_id);
$statement->execute();

The above code will delete the issue from the database.

Through the above code examples, we can implement the question modification and deletion functions in the knowledge Q&A website. Users can modify the problem on the problem management page. After clicking the save button, the problem information will be updated to the database. Users can also delete questions. After clicking the delete button, the question will be deleted from the database. In this way, we can improve our Q&A website and improve the user experience and functionality.

The above is the detailed content of Use PHP to develop question modification and deletion functions in the knowledge question and answer website.. 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