Javascript AJAX jQuery HTML OOPS PHP PHP Example MORE

Delete Data from MySQL Using PHP OOPS Concept

For delete data from MySQL first we have to create a table in data base.

Here we using 2 file For delete data from MySQL:

Crud.php:For connecting data base and function

view.php:For get the data and show in a table

delete_process.php:For Delete process in backend

CREATE TABLE `employee` (
	`userid` int(8) NOT NULL AUTO_INCREMENT,
	`first_name` varchar(55) NOT NULL,
	`last_name` varchar(55) NOT NULL,
	`city_name` varchar(55) NOT NULL,
	`email` varchar(50) NOT NULL,
    `datetime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

crud.php

<?php
    class Crud
    {
        private $servername = "localhost";
		private $username   = "root";
		private $password   = "";
		private $dbname = "oops_db";
        public $con;
        public $employeeTable = "employee";
		public function __construct()
		{
			try {
				$this->con = new mysqli($this->servername, $this->username, $this->password, $this->dbname);	
			} catch (Exception $e) {
				echo $e->getMessage();
			}
		}
        /* Fetch employee records for show listing */
		public function displayRecord()
		{
			$sql = "SELECT * FROM $this->employeeTable";
			$query = $this->con->query($sql);
			$data = array();
			if ($query->num_rows > 0) {
				while ($row = $query->fetch_assoc()) {
					$data[] = $row;
				}
				return $data;
			}else{
				return false;
			}
        }
        public function deleteRecord($id)
		{
			$sql = "DELETE from $this->employeeTable where userid=$id";
			$query = $this->con->query($sql);
			$data = array();
			if ($query) {
				return true;
			}else{
				return false;
			}
		}
        
    }
?>

view.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<?php
include_once("Crud.php");     
$viewdata=new Crud();  
$customers = $viewdata->displayRecord();
$output ="";
$output .="<table class='table table-striped table-hover'>
            <thead>
                <tr>
                    <th>Id</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>City Name</th>
                    <th>Email</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>";
    foreach ($customers as $customer) {
            $output.="<tr>
                <td>".$customer['userid']."</td>
                <td>".$customer['first_name']."</td>
                <td>".$customer['last_name']."</td>
                <td>".$customer['city_name']."</td>
                <td>".$customer['email']."</td>
                <td><a href='delete_process.php?id=".$customer['userid']."'>Delete</a></td>
            </tr>";
        }
$output .= "</tbody>
</table>";
echo $output;	
?>
</body>
</html>

crud.php

<?php
include_once("Crud.php");
$insertdata=new Crud();
$id = $_GET['id'];
$sql=$insertdata->deleteRecord($id);
if($sql)
{
    echo "Data deleted successfully !";
}
else
{
    echo "Error ! Please try again";
}

 ?>