Home > Database > Mysql Tutorial > body text

How to Implement a Delete Button to Remove Rows from a MySQL Table Using PHP?

DDD
Release: 2024-10-28 04:25:01
Original
301 people have browsed it

How to Implement a Delete Button to Remove Rows from a MySQL Table Using PHP?

Add a Delete Button to Remove Rows from MySQL Table Using PHP

To add a delete option to a MySQL table row displayed in an HTML table, you can follow these steps:

  1. Retrieve MySQL Data: Fetch the data from the contacts table using a query like:
<code class="php">$contacts = mysql_query("
        SELECT * FROM contacts ORDER BY ID ASC") or die(mysql_error());</code>
Copy after login
  1. Create a Table with Delete Buttons: Output the table with the data, adding a delete button in the last column:
<code class="php"><table id=&quot;contact-list&quot;>
    <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Telephone</th>
            <th>Address</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody>
        //Iterate results and display data
        <?php while( $contact = mysql_fetch_array($contacts) ) : ?>
            <tr>
                <td><?php echo $contact['name']; ?></td>
                <td><?php echo $contact['email']; ?></td>
                <td><?php echo $contact['telephone']; ?></td>
                <td><?php echo $contact['address']; ?></td>
                <td>
                    <form action='delete.php' method=&quot;post&quot;>
                        <input type=&quot;hidden&quot; name=&quot;name&quot; value=&quot;<?php echo $contact['name']; ?>&quot;>
                        <input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Delete&quot;>
                    </form>
                </td>                
            </tr>
        <?php endwhile; ?>
    </tbody>
</table></code>
Copy after login
  1. Create a Delete Script: In delete.php, process the submitted data:
<code class="php">$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
// Execute the query to delete the record
mysql_query ($query);

// Handle success/failure scenarios
if (mysql_affected_rows() == 1) { 
    // Success: Contact deleted
    echo "<strong>Contact Has Been Deleted</strong><br /><br />";
} else { 
    // Failure: Deletion failed
    echo "<strong>Deletion Failed</strong><br /><br />";
} </code>
Copy after login
  1. Pass Name Value: In the HTML table, pass the name value to the delete.php script using a hidden field or pass it in the URL:
<code class="php"><form action='delete.php?name=&quot;<?php echo $contact['name']; ?>&quot;' method=&quot;post&quot;>
    <input type=&quot;hidden&quot; name=&quot;name&quot; value=&quot;<?php echo $contact['name']; ?>&quot;>
    <input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Delete&quot;>
</form></code>
Copy after login

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!