Home >Database >Mysql Tutorial >How to Implement a Delete Button in a PHP Form to Remove Records from a MySQL Table?

How to Implement a Delete Button in a PHP Form to Remove Records from a MySQL Table?

Barbara Streisand
Barbara StreisandOriginal
2024-10-27 21:30:30804browse

How to Implement a Delete Button in a PHP Form to Remove Records from a MySQL Table?

How to Integrate a Delete Button into a PHP Form to Remove Records from a MySQL Table

Problem:

A request to add a "Delete" option to an HTML table populated by a MySQL table has proven unsuccessful. The objective is to create a functional delete link that triggers a form submission and eliminates the corresponding row from the database.

Code Snapshot:

results.php:

<code class="php">// Fetch data from the database
$contacts = mysql_query("SELECT * FROM contacts ORDER BY ID ASC") or die(mysql_error());

// If results exist, display the table
if (mysql_num_rows($contacts) > 0) {
    // Table markup
    echo '<table id="contact-list">';

    // Table head
    echo '<thead><tr><th>Name</th><th>Email</th><th>Telephone</th><th>Address</th><th>Delete</th></tr></thead>';

    // Table body
    while ($contact = mysql_fetch_array($contacts)) {
        echo '<tr>';
        echo '<td>' . $contact['name'] . '</td>';
        echo '<td>' . $contact['email'] . '</td>';
        echo '<td>' . $contact['telephone'] . '</td>';
        echo '<td>' . $contact['address'] . '</td>';

        // Delete form
        echo '<td><form action="delete.php" method="post">';
        echo '<input type="hidden" name="name" value="">';
        echo '<input type="submit" name="submit" value="Delete">';
        echo '</form></td>';

        echo '</tr>';
    }

    // Table foot
    echo '</table>';
}</code>

delete.php:

<code class="php">// Define the deletion query
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";

// Execute the query
mysql_query($query);

// Display a success or failure message based on the number of affected rows
if (mysql_affected_rows() == 1) {
    echo '<strong>Contact Has Been Deleted</strong><br /><br />';
} else {
    echo '<strong>Deletion Failed</strong><br /><br />';
}</code>

Solution:

The issue lies in passing the primary key value (in this case, the name) to the delete.php script. To rectify this, the delete link must include the name value either through a hidden field or as a URL parameter.

Updated Code:

<code class="php">// Within the table body loop in results.php

echo '<td><form action="delete.php?name=' . $contact['name'] . '" method="post">';</code>

The above is the detailed content of How to Implement a Delete Button in a PHP Form to Remove Records from a MySQL Table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn