Adding a delete button in PHP on each row of a MySQL table

Adding a delete button in PHP on each row of a MySQL table

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Row-Level Deletion in PHP and MySQL

Displaying data from a database is often the first step in building an application, but allowing users to modify or remove that data requires careful implementation. When you loop through a result set using mysqli_fetch_assoc(), you are iterating over rows of data, but simply echoing values doesn't give you a way to interact with them safely. To add a delete button next to each row, you must understand how HTTP requests work: the browser needs to know which specific record to remove and how to tell the server that action occurred.

In your current code, you are fetching rows from $records and outputting them, but there is no mechanism to identify which row corresponds to which database ID when a button is clicked. If you add an HTML <a> tag or a <form> element without passing the unique identifier (like Staff_ID) to the server, any deletion attempt will likely fail or delete the wrong record.

Passing Data via GET Parameters

The most straightforward way to handle this for small datasets is using the HTTP GET method. You can append the Staff ID to the URL of a specific action page. Instead of echoing a simple link, you need to construct an anchor tag that includes the ID as a query parameter.

echo "<td><a href='delete_record.php?id=" . $book['Staff_ID'] . "'>Delete</a></td>";

This code assumes you have a separate PHP file named delete_record.php. When the user clicks the link, the browser navigates to that page with the ID attached in the URL. This approach is simple and requires no server-side state management, making it easy to debug for beginners. However, placing sensitive actions like deletion in the query string is generally discouraged for security reasons, as IDs are visible in browser history and server logs.

Using Forms and POST Requests

For better security practices, especially when dealing with destructive operations, you should use an HTML form that submits via the POST method. You can create a form hidden within the table row or a separate action page that renders the list of rows with embedded forms.

echo '<td>
        <form method="post" action="delete_record.php">
            <input type="hidden" name="id" value="' . $book['Staff_ID'] . '">
            <button type="submit" class="btn-danger">Delete</button>
        </form>
      </td>';

Here, the Staff_ID is passed as a hidden input field within the form. When the user clicks submit, the form sends this data to delete_record.php. In that file, you would retrieve the ID using $_POST['id'] and execute your deletion logic. This method keeps the identifier out of the URL, reducing the risk of accidental exposure.

Executing the Deletion Logic

Once the request reaches the deletion handler, you must verify the data before removing it. Never trust that a number passed in a URL or form is valid without checking. In delete_record.php, you would typically use a prepared statement to prevent SQL injection attacks.

$id = $_POST['id'] ?? null; // Check if ID exists for GET/POST handling

if ($id) {
    $stmt = $mysqli->prepare("DELETE FROM staff_table WHERE Staff_ID = ?");
    $stmt->bind_param("i", $id);
    
    if ($stmt->execute()) {
        // Redirect to prevent form resubmission on refresh
        header("Location: list_page.php?deleted=1");
        exit();
    } else {
        // Handle error appropriately
        echo "Error deleting record.";
    }
}

Using bind_param ensures that the user input is treated strictly as data, not executable SQL code. This is critical because if you concatenate user input directly into a query string, a malicious user could inject arbitrary commands to access or destroy other data.

Handling Conflicts and User Experience

A common pitfall in PHP applications is the "Post-Redirect-Get" pattern. If you execute a deletion on a page and leave the user there, they might refresh their browser using the back button, resubmitting the deletion form. This could lead to multiple deletions or database errors depending on how your table constraints are set up. Always redirect the user to a safe state (like a list view) after processing the request.

Additionally, consider what happens when no records exist. If your loop finishes and $records is empty, ensure your HTML table structure handles that gracefully so you don't display an empty table with just headers.

If you are already using Laravel or plan to migrate your application framework, this logic can be encapsulated within a controller and view using Blade templating. Frameworks like those offered at https://laravelcompany.com provide robust methods for handling form submissions and database interactions securely out of the box, abstracting away much of the manual SQL binding work.

Our network