How to delete data from database in PHP using button | E-CODEC



How to delete data from database in PHP using button MySQL

Hello Friends, Today in  this blog you will learn How to delete data from database in PHP using button.

In the earlier blog, I have shared to make Login and Registration form in HTML CSS JavaScript, Now it's time to learn How to delete data from database in PHP using button. 

In this blog we learn How to delete data from database in PHP using button. So Here is the code and my work. I hope you are gonna love this and share whit your friends too. So let's Start learning How to delete data from database in PHP using button from scratch. So Let's start..

I'm happy to post here my insight and methods utilized in PHP and MySQL. As you all mindful that PHP and MySQL are the most broadly utilized open source for sites. the present great many sites and application are utilizing these free software's.

Actually I feel that this could be next level for front end engineers by utilizing PHP and MySQL a HTML designer can make a site more unique. Allow us to talk about How to make a Basic Login form in PHP with database, Its straight forward and exceptionally valuable for an essential site dynamic client dynamic enlistment. 

Each innovative can execute its essential construction to their site. Presently Just follow these basic advances and you will find that your first dynamic functional PHP registration form with database connection entry on every form fill up.

STEP 1: Open any code editor

STEP 2: Create a index.php file with PHP extension

Create a new file index.php, Here is the code

 <?php   
 include 'connection.php';  
 $query = "select * from delData";  
 $run = mysqli_query($conn,$query);  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <head>  
      <meta charset="utf-8">  
      <title>Delete Data From Database in PHP</title>  
      <link rel="stylesheet" type="text/css" href="style.css">  
 </head>  
 <body>  
 <header></header>  
 <table border="1" cellspacing="0" cellpadding="0">  
      <tr class="heading">  
           <th>Sl No</th>  
           <th>ID</th>  
           <th>Student Name</th>  
           <th>Class</th>  
           <th>Father's Name</th>  
           <th>Mother's Name</th>  
           <th>Address</th>  
           <th>Operation</th>  
      </tr>  
      <?php   
      $i=1;  
           if ($num = mysqli_num_rows($run)>0) {  
                while ($result = mysqli_fetch_assoc($run)) {  
                     echo "  
                          <tr class='data'>  
                          <td>".$i++."</td>  
                               <td>".$result['id']."</td>  
                               <td>".$result['stname']."</td>  
                               <td>".$result['class']."</td>  
                               <td>".$result['fname']."</td>  
                               <td>".$result['mname']."</td>  
                               <td>".$result['address']."</td>  
                               <td><a href='delete.php?id=".$result['id']."' id='btn'>Delete</a></td>  
                          </tr>  
                     ";  
                }  
           }  
      ?>  
 </table>  
 </body>  
 </html>  

STEP 3: Create a style.css file with css extension

 *{  
      padding: 0;  
      margin: 0;  
      box-sizing: border-box;  
 }  
 body{  
      width: 100%;  
      height: 100vh;  
      background-color: #34495e;  
      position: relative;  
      font-family: 'verdana',sans-serif;  
 }  
 header{  
      width: 100%;  
      height: 80px;  
      background-color: #2c3e50;  
 }  
 table{  
      width: 75%;  
      position: absolute;  
      top: 50%;  
      left: 50%;  
      transform: translate(-50%,-50%);  
 }  
 .heading{  
      background-color: #FFFF;  
 }  
 .heading th{  
      padding: 10px 0;  
 }  
 .data{  
      text-align: center;  
      color: #FFFF;  
 }  
 .data td{  
      padding: 15px 0;  
 }  
 #btn{  
      text-decoration: none;  
      color: #FFF;  
      background-color: #e74c3c;  
      padding: 5px 20px;  
      border-radius: 3px;  
 }  
 #btn:hover{  
      background-color: #c0392b;  
 }  
STEP 4: Create Database for inserting values
 CREATE TABLE `deldata` (  
  `id` int(11) NOT NULL,  
  `stname` varchar(150) NOT NULL,  
  `class` int(11) NOT NULL,  
  `fname` varchar(150) NOT NULL,  
  `mname` varchar(150) NOT NULL,  
  `address` varchar(255) NOT NULL  
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;  

STEP 4: Create a delete file with PHP extension

 <?php   
 include 'connection.php';  
 if (isset($_GET['id'])) {  
      $id = $_GET['id'];  
      $query = "DELETE FROM `deldata` WHERE id = '$id'";  
      $run = mysqli_query($conn,$query);  
      if ($run) {  
           header('location:index.php');  
      }else{  
           echo "Error: ".mysqli_error($conn);  
      }  
 }  
 ?>  

You Can Also Read:- PHP Registration form in PHP with MySQL database connectivity 
Thank You ! 

Post a Comment

0 Comments