How to Delete Data in mySql Database using PHP

How to Delete Data in mySql Database using PHP

Today I will explain how to delete data or records in database using PHP  and mySql. This topic is very important part of any web developer at beginner to advance level. I know most of people recommended mysql database because it is free and easy to understand. This post is simple delete data or records in database using php and mysql.

In this post, following file are required.


Follow These Steps
Step 2.   Open view_stu.php File
      Go to where xampp is installed and open htdoc folder then go to tutorials  folder and open view_stu.php and some changes, code shown below

         <script>
            function delete_id(id)
             {
              if(confirm('Are You Sure To Delete This Record ?'))
             {
              window.location.href='delete_stu.php?delete_id='+id;
             }
             }

         </script>

        placed at <thead> section and


        <td><a href=\"javascript:delete_id($row[0]);\">Delete</a></td>

        placed at echo section after that your view_stu.php look like shown below

      <table align="center" class="table " border="1">
       <thead>
        <tr style="background-color: gray; color: white;">
          <th>S.No</th>
          <th>Name</th>
          <th>Gender</th>
          <th>Email</th>
          <th>Phone No</th>
          <th>Address</th>
          <th>Action</th>
        </tr>
       <script>
         function delete_id(id)
           {
            if(confirm('Are You Sure To Delete This Record ?'))
           {
              window.location.href='delete_stu.php?delete_id='+id;
           }
           }

      </script>
      </thead>
      <tbody>
       
       <?php

           include_once('config.php');

           $data="select * from students";
           $data = $conn->query($data);
     
           while ($row = $data->fetch_array())
          {

            $stu_id            = $row['stu_id'];
            $stu_name       = $row['stu_name'];
            $stu_email       = $row['stu_email'];
            $stu_gender     = $row['stu_gender'];
            $stu_phone      = $row['stu_phone'];
            $stu_address    = $row['stu_address'];

            echo("
                     <tr>
                      <td>$stu_id</td>
                      <td>$stu_name</td>
                      <td>$stu_gender</td>
                      <td>$stu_email</td>
                      <td>$stu_phone</td>
                      <td>$stu_address</td>
                      <td>
                           <a href='edit_stu.php?id=".$stu_id."'>Update</a>
                           <a href=\"javascript:delete_id($row[0]);\">Delete</a>
                      </td>
                    </tr>
                 ");
           }

         ?>

      </tbody>
      </table>

Step 3.   Now, go to browser and type localhost/tutorials/view_stu.php, screen shown below


Delete Data mySql Database using PHP

Step 4.   Create Delete File
      Go to where xampp is installed and open htdoc folder then go to tutorials  folder and create a new file for example, delete_stu.php , Code shown below


           <?php
               
                include_once('config.php');
      
if (isset($_GET['delete_id']))
           {
$sql = "delete from student where stu_id=".$_GET['delete_id'];
mysqli_query($conn, $sql);
header("Location:view_stu.php?p=1");
           }

          ?>

Step 5.   Now, go to browser and type localhost/tutorials/view_stu.php, screen shown below


Delete Data mySql Database using PHP

Step 6.   Now click delete link, for example, I will delete s.no 8 whose name is sneha, screen shown below


Delete Data mySql Database using PHP

You can easily seen that data are deleted in table also you can check in database table, go to phpmyadmin(see More How to Insert data in Database using php and mysql), screen shown below


Delete Data mySql Database using PHP



                   I hope, it helped you to understand, how to delete data in database using PHP and mySql.........Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thanks For Visiting Blog...