Current Location: Home> Latest Articles> How to Implement Question Editing and Deletion in a Knowledge Q&A Website Using PHP

How to Implement Question Editing and Deletion in a Knowledge Q&A Website Using PHP

M66 2025-07-14

Introduction

With the development of the internet, knowledge Q&A websites have become important platforms for acquiring information and solving problems. On these platforms, users can submit questions and receive answers from others. To enhance the user experience, it is essential to provide features that allow users to edit and delete their questions. This article will explain how to implement question editing and deletion functionalities in a knowledge Q&A website using PHP.

Creating the Question Management Page

First, we need to provide a question management page where users can edit or delete their submitted questions. In PHP, data can be retrieved using either GET or POST methods. Assuming we create a page named manage.php, the following code demonstrates how to retrieve the question's ID:

<span class="fun">$question_id = $_GET['id'];</span>

Next, we query the database using the retrieved ID to get the detailed information of the question. Assuming the questions are stored in a table named 'questions', the following code fetches the question's details from the database:

$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);

Displaying the Question and Allowing Edits

In the page, we can display the question's title and content through a form, allowing the user to make edits. Here's a simple form example to display the question and accept user input:

<form action='update.php' method='POST'>
  <input type='hidden' name='id' value='<?php echo $question['id']; ?>'>
  <label for='title'>Title:</label>
  <input type='text' name='title' value='<?php echo $question['title']; ?>'><br>
  <label for='content'>Content:</label>
  <textarea name='content'><?php echo $question['content']; ?></textarea><br>
  <input type='submit' value='Save'>
</form>

Handling Question Updates

Once the user clicks the save button, the form data is submitted to the update.php page. In this page, we retrieve the user input and update the question in the database. Here's the code example for the update.php page:

$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();

Deleting a Question

To implement the question deletion feature, we can add a delete button on the question management page. When the user clicks the button, the question ID is passed to the delete.php page for processing. Here's the code for delete.php that handles deleting the question:

$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();

Conclusion

Through the above code examples, we have successfully implemented question editing and deletion functionalities for a knowledge Q&A website. Users can edit their questions through the management page, and after clicking save, the question details are updated in the database. Additionally, users can delete their questions, removing them from the database. This functionality helps improve the overall user experience and enhances the features of the website.