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:
<code class="php">$contacts = mysql_query(" SELECT * FROM contacts ORDER BY ID ASC") or die(mysql_error());</code>
<code class="php"><table id="contact-list"> <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="post"> <input type="hidden" name="name" value="<?php echo $contact['name']; ?>"> <input type="submit" name="submit" value="Delete"> </form> </td> </tr> <?php endwhile; ?> </tbody> </table></code>
<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>
<code class="php"><form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post"> <input type="hidden" name="name" value="<?php echo $contact['name']; ?>"> <input type="submit" name="submit" value="Delete"> </form></code>
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!