Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

PHP CRUD Application Using OOP and MYSQL

Develop CRUD operations with PHP and MySQL using the OOP (Object Oriented Programming) technique

Following file Structure

 database.php
 index.php
 form.php
 view.php
 insert.php
 edit.php
 update.php
 delete_data.php
 css/bootstrap.min.css
 css/style.css
 css/sweetalert.min.js
 js/jquery.min.js

1. Create MySQL Database Table

CREATE TABLE `user` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
`email` varchar(200) DEFAULT NULL,
`subject` varchar(200) DEFAULT NULL,
`message` varchar(200) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`updated` datetime DEFAULT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

/*Data for the table `user` */

insert into `user`(`id`,`name`,`phone`,`email`,`subject`,`message`,`created`,`updated`)


values (1,'Tess','09176566608','tdimalibot@gmail.com','Website Development and
Workshop','Perform crud operations such as create, read, update, and delete in PHP
OOP and MySQL.','2023-02-03 07:17:11','2023-02-03 01:04:03');
2. Database Configuration
Add your database configuration in database.php file

<?php
class database{
public $que;
private $servername='localhost';
private $username='root';
private $password='admin';
private $dbname='oop_db';
private $result=array();
private $mysqli='';

public function __construct(){


$this->mysqli = new mysqli($this->servername,$this->username,$this->password,$this->dbname);
}

public function insert($table,$para=array()){


$table_columns = implode(',', array_keys($para));
$table_value = implode("','", $para);
echo "<script>alert('SAMPLE');</script>";

$sql="INSERT INTO $table($table_columns) VALUES('$table_value')";

$result = $this->mysqli->query($sql);
}

public function update($table,$para=array(),$id){


$args = array();

foreach ($para as $key => $value) {


$args[] = "$key = '$value'";
}

$sql="UPDATE $table SET " . implode(',', $args);

$sql .=" WHERE $id";

$result = $this->mysqli->query($sql);
}

public function delete($table,$id){


$sql="DELETE FROM $table";
$sql .=" WHERE $id ";
$sql;
$result = $this->mysqli->query($sql);
}

public $sql;

public function select($table,$rows="*",$where = null){


if ($where != null) {
$sql="SELECT $rows FROM $table WHERE $where";
}else{
$sql="SELECT $rows FROM $table";
}

$this->sql = $result = $this->mysqli->query($sql);


}

public function __destruct(){


$this->mysqli->close();
}
}
?>
3. Create PHP File

index.php

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="SemiColonWeb" />
<link href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/bootstrap.min.css"/>
<script src="css//sweetalert.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Login</title>
<style>
.hed{
background: #ccc;
color: blue;
}
.hed h1{
padding-top: 20px;
padding-bottom: 25px;
}
.form{
margin-top: 50px;
background: #ccc;
}
.table{
margin-top: 50px;
}
</style>
</head>
<body class="stretched">
<div id="wrapper" class="clearfix">
<section id="page-title">
<div class="container clearfix hed">
<center>
</center>
</div>
</section>
<div class="col-md-12 hed">
<center>
<h1>Insert Data</h1>
</center>
</div>
<section id="content">
<div class="content-wrap">
<div class="container clearfix">
<div class="row">
<div class="col-md-12" id="hide">
<form class="row form" action="insert.php" method="post">
<?php include 'form.php'; ?>
<div class="col-12 form-group">
<input type="submit" class="btn btn-dark" name="submit" value="Insert">
</div>
</form>
</div>
<div class="col-md-12 p-0">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Phone</th>
<th scope="col">Subject</th>
<th scope="col">Message</th>
<th scope="col">Created on</th>
<th scope="col">Update on</th>
<th scope="col" colspan="3">Action</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$b = new database();
$b->select("user","*");
$result = $b->sql;
?>
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['message']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['updated']; ?></td>
<td>
<a href="view.php?id=<?php echo $row['id']; ?>" type="button" class="btn btn-success btn-
sm">View</a>
</td>
<td>
<a href="edit.php?id=<?php echo $row['id']; ?>" type="button" class="btn btn-primary btn-
sm">Edit</a>
</td>
<td>
<a href="" type="button" data-toggle="modal" data-id="<?php echo $row['id']; ?>" data-
target="#myModal" id="del" class="btn btn-danger btn-sm">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).on('click',"#del",function(e) {
e.preventDefault();
var del = $(this).data('id');

swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
$.ajax({
url : "delete_data.php",
type : "POST",
data : {id:del},
success : function() {
swal({
title: "Sweet!",
text: "Delete data",
imageUrl: 'thumbs-up.jpg'
});
}
});
swal("Poof! Your imaginary file has been deleted!", {
icon: "success",
});

} else {
swal("Your imaginary file is safe!");
}
});
});
});
</script>
</body>
</html>
4. form.php

<div class="col-md-6 col-sm-12 form-group">


<label for="register-form-name">Username:</label>
<input type="text" name="name" value="<?php if(isset($row)) { echo $row['name']; } ?>" class="form-control" /
required>
</div>
<div class="col-md-6 col-sm-12 form-group">
<label for="register-form-name">Email:</label>
<input type="email" name="email" value="<?php if(isset($row)) { echo $row['email']; } ?>" class="form-control" /
required>
</div>
<div class="col-md-6 col-sm-12 form-group">
<label for="register-form-name">Phone:</label>
<input type="text" name="phone" value="<?php if(isset($row)) { echo $row['phone']; } ?>" class="form-control" /
required>
</div>
<div class="col-md-6 col-sm-12 form-group">
<label for="register-form-name">Subject:</label>
<input type="text" name="subject" value="<?php if(isset($row)) { echo $row['subject']; } ?>" class="form-control" /
required>
</div>
<div class="col-md-12 col-sm-12 form-group">
<label for="register-form-name">Message:</label>
<textarea class="form-control" name="message" placeholder="Message..."><?php if(isset($row)) { echo
$row['message']; } ?></textarea>
</div>
5. insert.php

<?php
include 'database.php';
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$date = date("Y-m-d h:i:s A");

$a = new database();
$a->insert('user',
['name'=>$name,'email'=>$email,'phone'=>$phone,'subject'=>$subject,'message'=>$message,'created'=>$date,'updat
ed'=>$date]);
if ($a == true) {
header('location:index.php');
}
}
?>
6. edit.php

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<link href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/bootstrap.min.css"/>
<script src="js/jquery.min.js"></script>
<title>Edit</title>
<style>
.hed{
background: #ccc;
color: blue;
}
.hed h1{
padding-top: 20px;
padding-bottom: 25px;
}
.form{
margin-top: 50px;
background: #ccc;
}
.table{
margin-top: 50px;
}
</style>
</head>
<body class="stretched">
<div class="col-md-12 hed">
<center>
<h1>Edit Data</h1>
</center>
</div>
<section id="content">
<div class="content-wrap">
<div class="container clearfix">
<div class="row">
<?php
include 'database.php';

$id = $_GET['id'];

$b = new database();
$b->select("user","*","id='$id'");
$result = $b->sql;

$row = mysqli_fetch_assoc($result);
?>
<div class="col-md-12" id="hide">
<form class="row form" action="update.php" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<?php include 'form.php'; ?>
<div class="col-12 form-group">
<input type="submit" class="btn btn-dark" name="submit" value="Update">
</div>
</form>
</div>
</div>
</div>
</div>
</section>
</body>
</html>
7. update.php

<?php
include 'database.php';

if (isset($_POST['submit'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$date = date("Y-m-d h:i:s A");

$a = new database();
$a = new database();
$a->update('user',
['name'=>$name,'email'=>$email,'phone'=>$phone,'subject'=>$subject,'message'=>$message,'updated'=>$date],"id='
$id'");
if ($a == true) {
header('location:index.php');
}
}
?>>
8. view.php

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<link href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/bootstrap.min.css"/>
<script src="js/jquery.min.js"></script>
<title>Show</title>
<style>
.hed{
background: #ccc;
color: blue;
}
.hed h1{
padding-top: 20px;
padding-bottom: 25px;
}
.form{
margin-top: 50px;
background: #ccc;
}
.table{
margin-top: 50px;
}
</style>
</head>
<body class="stretched">
<div class="col-md-12 hed">
<center>
<h1>View Data</h1>
</center>
</div>
<section id="content">
<div class="content-wrap">
<div class="container clearfix">
<div class="row">
<div class="col-md-12 p-0">
<table class="table table-dark">
<?php
include 'database.php';

$id = $_GET['id'];
$b = new database();
$b->select("user","*","id='$id'");
$result = $b->sql;

$row = mysqli_fetch_assoc($result);
?>
<tbody>
<tr>
<th>Id</th>
<td><?php echo $row['id']; ?></td>
</tr>
<tr>
<th>Name</th>
<td><?php echo $row['name']; ?></td>
</tr>
<tr>
<th>Email</th>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<th>Phone</th>
<td><?php echo $row['phone']; ?></td>
</tr>
<tr>
<th>Subject</th>
<td><?php echo $row['subject']; ?></td>
</tr>
<tr>
<th>Message</th>
<td><?php echo $row['message']; ?></td>
</tr>
<tr>
<th>Created Time & Date</th>
<td><?php echo $row['created']; ?></td>
</tr>
<tr>
<th>Updated Time & Date</th>
<td><?php echo $row['updated']; ?></td>
</tr>
<tr>
<th>Back To Home</th>
<td><a href="index.php" type="button" class="btn btn-primary">Back</a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
</body>
</html>
9. delete_data.php

<?php
include 'database.php';

$id = $_POST['id'];

$a = new database();
$a->delete('user',"id='$id'");
?>

You might also like