Delete Data from MySQL using PHP - Web Development and Web Design Codes

Latest

Monday, April 17, 2017

Delete Data from MySQL using PHP

Delete Data from MySQL using PHP

It very easy and simple to Delete data from mysql database using php.
Delete data from mysql database using php
Delete data from mysql database using php
if you are beginner in php you need to learn how to delete data from database using php..
if you don't know to how to delete data from database using php..please follow the below step..absolutely fro beginners
Overview and Structure:
You will need 3 files. 
1. connect.php [To Established Connection with Database]
2. index.php [Displaying Data with 'Delete' Button]
3. delete.php [Deleting Data from database according to ID]
Save all files in same folder.

Database Structure:
Delete data from mysql database using php
Delete data from mysql database using php


 Database Name: tutorial
 Table Name: test
 Creating Table "test"
CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY(id)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;

Dumping data for table 'test':
INSERT INTO `test` (`id`, `name`, `email`) VALUES
(NULL, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(NULL, 'Krishna', 'krishna@mail.com'),
(NULL, 'Rupam Mondal', 'RupomMondal@gmail.com'),
(NULL, 'ANJAN', 'KumarANjan@gmail.com'),
(NULL, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'),
(NULL, 'Hrithik', 'Hrithik@gmail.com'),
(NULL, 'Modhu Bonik', 'Modhu360@gmail.com'),
(NULL, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'),
(NULL, 'Rupa', 'Rupa@hotmail.com'),
(NULL, 'ANJAN', 'Kumar@gmail.com');

connect.php 
You have to create connect.php file to create connection between your server and database..
<?php
$host="localhost"; //Host Name 
$username="root"; //MySQL UserName
$password=""; //MySQL PassWord
$dbname="tutorial"; //Database Name
//Create Connection
$conn=new mysqli($host,$username,$password,$dbname);
if($conn->connect_error){
 echo("Connection Failed:".$conn->connect_error);
}
?>
index.php
You have to create index php file to display data from database with delete button..
Delete data from mysql database using php
Delete data from mysql database using php

<!DOCTYPE html>
<html>
<head>
<title>PHP MySQL Delete Data from Table</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
 
<body>
 
<table style="width:50%">
  <tr>
    <th>ID</th>
 <th>Name</th>
 <th>Email</th>
 <th>Action</th>
  </tr>
   
  <?php 
  include("connect.php");
  $result=mysqli_query($conn,"SELECT*FROM test"); //Selecting Data From "test" Table to Display
  while($row=$result->fetch_array()){?> 
 <tr>
  <td><?php echo $row['id'];?></td>
  <td><?php echo $row['name'];?></td>
  <td><?php echo $row['email'];?></td>
  <td><a href="delete.php?id=<?php echo $row['id'];?>">Delete</a></td>
 </tr>
  <?php } ?>
</table>
<?php
if(isset($_GET['error'])){
 echo $_GET['error'];
}
?>
</body>
</html>

delete.php
You have create delete.php page to delete selected data to from database according to id..
Delete data from mysql database using php
Delete data from mysql database using php

<?php
include("connect.php");
//Getting ID from url
   $id=$_GET['id'];
   //Deleting Data From MySQL table "test" Table According to ID
   $result=mysqli_query($conn,"DELETE FROM test WHERE id='$id'");
   if($result){
    //redirecting user to index page after data successfully deleted
 header('location:index.php');
   }else{
    //generating Error message on Failed
    header('location:index.php?error='.urlencode('Failed to Delete Data From Database'));
   }
?>

That's it how to Delete Data from MySQL using PHP. I hope you Enjoy This article.Please Like and Share with your Friends.
and Keep Visting for More Tutorials.Thank you.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.