Delete Data from Mysql Table using PHP OOP - Web Development and Web Design Codes

Latest

Sunday, February 11, 2018

Delete Data from Mysql Table using PHP OOP

Delete Data from Mysql Table using PHP OOP

Delete Data from Mysql Table using PHP OOP
Delete Data from Mysql Table using PHP OOP

In this lesson we are going to learn how to delete data from MySQL table using php oop..
just follow the below Example to lean how to delete data from mysql table using php oop concepts..

=>Table and Database:
Database Name: test
Table name : user
SQL codes for table user

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
Insert some rows in user table using below sql codes


INSERT INTO `user` (`id`, `username`, `email`) VALUES
(NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'Supriya Gain', 'SupriyaStar@Yahoo.com'),
(NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'ANJANBD', 'biswasshiuli608@gmail.com');

Create 3 files named index.php , database.php and delete.php

1. index.php

<?php
include "database.php";
?>
<?php
if(isset($_GET['msg'])){
 echo $_GET['msg'];
}

?>
<?php
$db= new Database();
$query="SELECT*FROM user";
$result=$db->select($query);
?>
<table width="500px" cellspacing="0" border="1px">
  <tr>
     <th>ID</th>
   <th>UserName</th>
    <th>Email</th>
    <th>Action</th>
  </tr>
  <?php if($result) { ?>
  <?php while($row=$result->fetch_assoc()){?>
  <tr>
     <td><?php echo $row['id'];?></td>
   <td><?php echo $row['username'];?></td>
    <td><?php echo $row['email'];?></td>
    <td><a href="delete.php?id=<?php echo $row['id'];?>">Delete</a></td>
  </tr>
<?php }}?>
</table>

2. database.php

<?php
class Database{
 public $host='localhost';
 public $user='root';
 public $pass='';
 public $dbname='codenair.com';

 public $link;
 public $error;
 
 public function __construct(){
  $this->connectDB();
 }
 //Connecting Database
 private function connectDB(){
  $this->link = new mysqli($this->host,$this->user,$this->pass,$this->dbname);
  if(!$this->link){
   $this->error= "Connection Failed".$this->link->connect_error;
   return false;
  }
 }
 //Fetching Data
 public function select($query){
  $result= $this->link->query($query) or die ($this->link->error.__LINE__);
  if($result->num_rows>0){
     return $result;   
  }else{
   return false;
  }
 }
 //Delete Data
 public function delete($query){
  $delete=$this->link->query($query) or die($this->link->error.__LINE__);
  if($delete){
   header('location:index.php?msg='.urlencode('Data Deleted Successfull.'));
   
  }
 }
 }

3. delete.php
<?php
require('database.php');
$db=new Database();
if(isset($_GET['id'])){
$id=$_GET['id'];
$query="DELETE FROM user WHERE id='$id'";
$result=$db->delete($query);
}else{
 echo "Direct Access Not Allowed";
}
?>
Practice This codes to learn Delete data from database in php oop concepts..
Keep Visiting This Blog for More Codes ..Thank You...

No comments:

Post a Comment

Thank You for Your Comment

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