How to add a delete button to a PHP form that will delete a row from a MySQL table

How to add a delete button to a PHP form that will delete a row from a MySQL table

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

Stefan Bogdanescu

Founder & Senior Architect

Why Your Delete Form Isn't Removing Rows (And How to Fix It)

The core issue in your current implementation is that you are attempting to delete a record based on an id column while passing the wrong variable, and doing so without validating the input. In your delete.php script, the query targets the name field:

$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";

However, your form only sends an empty string for the name parameter because the hidden input is defined as <input type="hidden" name="name" value="">. Even if you had populated that field with a name string, relying on name to delete a user in a table containing multiple users with the same name would lead to data corruption. You should be passing and deleting by the unique primary key, which is clearly your ID column.

Passing the Correct Identifier

To make this work, you must pass the unique identifier of the row you wish to remove directly from the list view. Instead of using a generic hidden input with an empty value, generate a new form for each row that includes the specific ID of that contact.

Update your loop in results.php to include the ID in the POST data:

<tr>
    <td class="contact-name"><?php echo htmlspecialchars($contact['name']); ?></td>
    <td class="contact-email"><?php echo htmlspecialchars($contact['email']); ?></td>
    <td class="contact-telephone"><?php echo htmlspecialchars($contact['telephone']); ?></td>
    <td class="contact-address"><?php echo htmlspecialchars($contact['address']); ?></td>
    <td class="contact-delete">
        <!-- Pass the unique ID -->
        <form action="delete.php" method="post">
            <input type="hidden" name="id" value="<?php echo htmlspecialchars($contact['ID']); ?>">
            <button type="submit" name="delete" class="btn-danger">Delete</button>
        </form>
    </td>
</tr>

Notice the use of htmlspecialchars() here. When echoing data back into a form or HTML, you must escape it to prevent Cross-Site Scripting (XSS) vulnerabilities. An attacker could potentially inject malicious scripts if user names contained special characters.

Securing the Deletion Query

In your delete.php file, you need to switch from targeting the name column to targeting the ID. Furthermore, direct variable interpolation into SQL strings (like {$_POST['id']}) is dangerous and prone to SQL injection attacks.

You should use Prepared Statements. This separates the SQL logic from the user data, ensuring that whatever comes in via POST is treated strictly as data, not executable code.

Here is the corrected delete.php:

<?php
// Establish a secure connection (using mysqli for prepared statements)
$connection = new mysqli("localhost", "username", "password", "database_name");

if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

// Check if the delete button was actually pressed
if (isset($_POST['delete'])) {
    $id = $_POST['id'];

    // Prepare the statement
    $stmt = $connection->prepare("DELETE FROM contacts WHERE ID = ?");
    
    // Bind the parameter. 'i' stands for integer.
    $stmt->bind_param("i", $id);

    if ($stmt->execute()) {
        echo "<strong>Success:</strong> Contact with ID {$id} has been deleted.";
    } else {
        echo "<strong>Error:</strong> Failed to delete contact. " . $stmt->error;
    }

    $stmt->close();
}

$connection->close();
?>

Key Trade-offs and Limitations

The original code failed primarily because it mixed the concept of a form submission with an identifier that wasn't being passed correctly. By moving to ID-based deletion, you ensure that deleting "John" won't accidentally wipe out two different contacts named John who happen to work at different addresses.

Using prepared statements is non-negotiable for any production application. While simple queries might seem easier to read, they require manual escaping logic which is easy to get wrong. The overhead of preparing a statement is negligible for this use case, making it the superior choice for security and maintainability.

If you are maintaining legacy code that relies on mysql_* functions, be aware that these extensions were deprecated in PHP 5.5 and removed in PHP 7.0. You should migrate to mysqli or PDO immediately. The transition to prepared statements is part of that migration path and significantly hardens your application against common attack vectors.

Our network