Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Thursday, 27 October 2016

Delete selected row record from database in php

In this example , we are learning about how to delete selected row record from database in php.

Here, we are fetching record from database. If database not available then create a database and add records. then put link of delete record in each row.

Here, I create a database login_tbl  which have 3 column of id,name,molbile.

From first file , If you delete 1st record then id of the first record pass by query string on second page and delete selected record.

Let's see the example :

File name : index.php



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
 $servername= "localhost";
 $username = "root";
 $password="";
 $dbname="demo";
 $conn = new mysqli($servername,$username,$password,$dbname);  
?>
<FORM METHOD="POST" enctype="multipart/form-data">  
  <?php
   $sql = "SELECT *FROM login_tbl";
   $result = $conn->query($sql);

   echo "<table border='1' style='width:50%'><tr><th>ID</th><th>Name</th><th>Mobile</th><th>Delete</th></tr>";
   if ($result->num_rows > 0)
   {
     // output data of each row
     while($row = $result->fetch_assoc())
     {
      echo "<br><tr><td align='center'>".$row["id"]." </td><td align='center'>".$row["name"]."</td><td align='center'>".$row["mobile"]."</td><td align='center'><a href='delete.php?id=".$row["id"]."'>Delete</td><br>";
     }
   }
   else
   {
     echo "No data available";
   }
   echo "</table>";
  ?>
</FORM>


File name : delete.php



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
 $id= $_GET['id'];
 $cn=mysql_connect("localhost","root","");
 mysql_select_db("demo");
 $s1="delete from login_tbl where id=".$id;

 mysql_query($s1) or die("INSERT ".mysql_error());
 mysql_query("commit") or die("commit fail ".mysql_error());
 
 header("Location: index.php");
?>

Output :


  • Before delete record



  • After delete record


0 comments:

Post a Comment

Like us on Facebook

Site Visitor

Powered by Blogger.