PHP CRUD Application Using OOP and MYSQL
PHP CRUD Application Using OOP and MYSQL
PHP CRUD Application Using OOP and MYSQL
Develop CRUD operations with PHP and MySQL using the OOP (Object Oriented Programming) technique
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
<?php
class database{
public $que;
private $servername='localhost';
private $username='root';
private $password='admin';
private $dbname='oop_db';
private $result=array();
private $mysqli='';
$result = $this->mysqli->query($sql);
}
$result = $this->mysqli->query($sql);
}
public $sql;
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
<?php
include 'database.php';
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$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'];
$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'");
?>