How to make a button delete php

How to create a 'Delete' button with PHP: step-by-step guide with an example code.

Deleting Data with PHP

In order to delete data from a database using PHP, you need to write a script that connects to the database, then sends a DELETE query. This query will delete the specified row from the specified database table.

The following example shows a simple PHP script that deletes a row from a database table. This example assumes that the database connection has already been established, and that a valid ID number has been sent to the script.

// Get the ID number from the URL
$id = $_GET['id'];

// Construct the DELETE query
$query = "DELETE FROM table_name WHERE id = $id";

// Execute the query
$result = mysql_query($query);

// Check for errors
if (!$result) {
    die("Query failed: " . mysql_error());
}

// Redirect to the list page
header("Location: list.php");

This script starts by getting the ID number from the URL (using the $_GET variable). It then constructs the DELETE query and executes it. Finally, it checks for errors and redirects to the list page.

This is just a basic example of how to delete data using PHP. You may need to adjust the query or add additional code depending on your particular application.

For more information on deleting data with PHP, see the PHP manual.

p
php

Answers (0)