Skip to content
PHP Logo

How can the PHP code be modified to ensure successful deletion of a book from the database upon purchase?

To ensure successful deletion of a book from the database upon purchase, you can modify the PHP code by adding a query to delete the book record from the database after the purchase has been successfully processed. This can be done by executing a DELETE query based on the book ID that was purchased.

php
// Code to process purchase and delete book from database
$book_id = $_POST['book_id']; // Assuming the book ID is passed via POST

// Process purchase code here...

// Delete the book from the database
$delete_query = "DELETE FROM books WHERE id = $book_id";
if(mysqli_query($conn, $delete_query)){
    echo "Book successfully deleted from the database.";
} else{
    echo "Error deleting book: " . mysqli_error($conn);
}