All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Delete Data From Database In PHP Using Button

Last Updated : Jul 1, 2023

How To Delete Data From Database In PHP Using Button

In this article we will show you the solution of how to delete data from database in PHP using button, to delete data from the database in php, first, we have to create a database on the localhost.

We have to create the connection with the HTML file. Then we have to display it as a HTML table. Then using the DELETE statement in php to delete data from database.

Step By Step Guide On How To Delete Data From Database In PHP Using Button :-

PHP Code for Connecting with The Database:

<?php
$conn = mysqli_connect('localhost', 'root', '') ;
?>
  1. At first <?php tag to write the php.
  2. Create a variable named $conn for mysqli_connect() with hostname, username, and password to make a connection with the database.
  3. close the php code with ?> tag

PHP Code for Delete Data:

<?php
include "connection.php" ;
if (isset($_GET['id'])) {
    $id = $_GET['id'] ;
    $delete= mysqli_query($conn,"DELETE FROM `data1` WHERE `id`= `$id`") ;
}
$select = "select * from data1" ;
$query = mysqli_query($conn, $select) ;
?>
<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title>how to delete data from database in php using button </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> how to delete data from database in php using button </h2>
    <br>
    <table>
        <thead>
            <tr>
              <th scope="col"> SL No. </th>
              <th scope="col">First Name</th>
              <th scope="col">Last Name</th>
              <th scope="col">Address</th>
              <th scope="col">Email</th>
            </tr>
          </thead>
          <?php
          $num = mysqli_num_rows($query) ;
          if ($num > 0) {
                while ($result = mysqli_fetch_assoc($query)) {
                    echo "<tr>
                            <td>".$result['id']."</td>
                            <td>".$result['fname']."</td>
                            <td>".$result['lname']."</td>
                            <td>".$result['address']."</td>
                            <td>".$result['email']."</td>
                            <td>
                                <a href='index.php?id=".$result['id']."'> DELETE </a>
                            </td>
                        </tr>
                    " ;
                }
          }
          ?>
    </table>
</body>
</html>
  1. At first <?php tag to write the php.
  2. First include the php file used for the connection
  3. Create an if statement with isset() function with $_GET variable
  4. Create a variable for ‘id’
  5. $delete with mysqli_query() with the delete statement.
  6. Using SELECT for selecting the particular database we want to use here.
  7. $query with the mysqli_query().
  8. close the php code with ?> tag
  9. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  10. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  11. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  12. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  13. Attach an external CSS file with a <link> tag to style the table.
  14. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  15. <h1> tag used to add heading here and also adding the inline CSS here.
  16. <table> tag to create a table. <thead> tag and <th> tag to add some heading as per the table heading in the table in the database
  17. Then create a <?php tag to write php
  18. Using mysqli_num_rows() within the variable $name
  19. If the number of rows is not zero then use a while loop with mysqli_fetch_assoc() to fetch data from the database and display it to the table
  20. Then create a link as a button, by clicking this we can delete data from the database.
  21. Close the HTML tag with </html>

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to delete data from database in php using button.

I hope this article on how to delete data from database in PHP using button helps you and the steps and method mentioned above are easy to follow and implement.