Press "Enter" to skip to content

PHP MySQL Delete Data

Html template.

For making the delete functionality no need to make other html template. This functionality will work in read page. So, for that i will not the html template here. Only i will give the read page link, for the link click here…

Config file code will be same as read file but inside the read file above the body tag paste this line of code.

<?php
$db = new Database();
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $db->delete($id);
}
?>

Database file creation.

Here we follow full php oop method so, we use data insert file and the file name will be Database.php. This file will include the config.php file.

<?php
class Database{
	public $host   = DB_HOST;
	public $user   = DB_USER;
	public $pass   = DB_PASS;
	public $dbname = DB_NAME;
	
	public $link;
	public $error;
	
	public function __construct(){
		$this->connectdb();
	}
	
	private function connectdb(){
		$this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
		if(!$this->link){
			$this->error = "connection fail".$this->link->connect_error;
			return false;
		}
	}

// Delete Data
	public function delete($id) {
        $query = "DELETE FROM tbl_user WHERE id = $id";
        $delete_row = mysqli_query($this->link, $query) or die($this->link->error . __LINE__);
        if ($delete_row) {
            header("Location: index.php?msg=" . urlencode('Data Deleted Successfully.'));
            exit();
        } else {
            die("Error : (" . $this->link->errno . ")" . $this->link->error);
        }
    }

At last i give some picture of file and after the code.

Files folder picture.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *