[Solved] How to add a button to my PHP form that deletes rows from my MYSQL database [duplicate]

Introduction

Adding a button to a PHP form that deletes rows from a MySQL database can be a useful way to manage data. This tutorial will provide step-by-step instructions on how to add a button to a PHP form that will delete rows from a MySQL database. It will also provide tips on how to ensure that the data is securely deleted from the database. By following this tutorial, you will be able to easily add a button to your PHP form that will delete rows from your MySQL database.

Solution

You can use a delete button in your form to delete rows from your MySQL database.

To do this, you will need to create a form with a delete button. The form should include a hidden field that contains the ID of the row you want to delete.

When the delete button is clicked, you can use a PHP script to execute a MySQL query to delete the row from the database.

For example, the following code will delete a row from a table called ‘users’ where the ID is equal to the value of the hidden field:


This code should be placed in a PHP file and the file should be included in the page where the form is located.


In your html view page some change echo "<td><a href="https://stackoverflow.com/questions/40479421/delete.php?did=".$row["id']."'>Delete</a></td>"; like bellow:

<?php
while($row = mysql_fetch_array($result))
{
    echo "<tr>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['rollnumber'] . "</td>";
        echo "<td>" . $row['address'] . "</td>";
        echo "<td>" . $row['phonenumber'] . "</td>";
        echo "<td><a href="https://stackoverflow.com/questions/40479421/delete.php?did=".$row["id']."'>Delete</a></td>";
    echo "</tr>";
}
?>

PHP delete code :

<?php
if(isset($_GET['did'])) {
    $delete_id = mysql_real_escape_string($_GET['did']);
    $sql = mysql_query("DELETE FROM venu WHERE id = '".$delete_id."'");
    if($sql) {
        echo "<br/><br/><span>deleted successfully...!!</span>";
    } else {
        echo "ERROR";
    }
}
?>

Note : Please avoid mysql_* because mysql_* has beed removed from
PHP 7. Please use mysqli or PDO.

More details about of PDO connection http://php.net/manual/en/pdo.connections.php

And more details about of mysqli http://php.net/manual/en/mysqli.query.php

2

solved How to add a button to my PHP form that deletes rows from my MYSQL database [duplicate]