How to Delete data from table using php and mysql

Here is the code to display the data from the table using php and mysql. This simple code is used to delete the record you choose.You can create the database and table. Here i created database name is oops and the table name is called as a oop. And the table fields are name, lname, salary. The below code helps you to display the table information in the format of table with the delete option. When you click the delete it takes the particular id and moves to the delete.php file, In the delete.php file haves the code to delete the particular row based on the id you chosen from the table. I hope this is very useful to you and this is one of the simple concept in php.


delete-data-from-table-php-mysql



index.php

<?php
$con=mysqli_connect("localhost","root","","oops");
$sql="select * from oop";
$result=mysqli_query($con,$sql);
echo "<table><tr><th>Name</th><th>Lname</th><th>Salary</th><th>Edit</th></tr>";
while($row=mysqli_fetch_assoc($result)){

echo "<tr><td>".$row['fname']."</td>";
echo "<td>".$row['lname']."</td>";
echo "<td>".$row['salary']."</td>";
echo "<td><a href='delete.php?id=".$row['id']."'>Delete</a></td>";
}
echo "</tr></table>";
?>



delete.php

<?php
$con=mysqli_connect("localhost","root","","oops");
$id=$_GET['id'];
$query="delete  from oop where id=$id";
$sql=mysqli_query($con,$query);
header("Location: view.php");
?>

Post a Comment

0 Comments