What is CRUD?
CRUD is an acronym for Create, Read, Update, and Delete. CRUD operations are basic data manipulation for databases. We’ve already learned how to perform create (i.e. insert), read (i.e. select), update and delete operations in previous chapters. In this tutorial, we’ll create a simple PHP application to perform all these operations on a MySQL database table in one place.
In this session, we will learn about the package of crud applications in an object-oriented way.
Database creation.
First, for that we need to create a database in mysql then under the database create a table. Here I named the database is db_crud and the name of the table is tbl_user.
If you can not create a MySQL database you can get help from youtube.
File and folder configuration.
Second, as here I use an object-oriented way so, I create a folder in htdocs give the folder name phpcrud, then open the folder and create some file names header.php, footer.php, index.php, update.php, and create.php
Third, then open the header.php file and paste this line of codes…
<?php
$fonts = "verdana";
$bgcolor = "#444";
$fontcolor = "#fff";
?>
<!doctype html>
<html>
<head>
<title>PHP OOP CRUD</title>
<style>
body{font-family:<?php echo $fonts;?>}
.phpcoding{width:900px; margin: 0 auto;
background:<?php echo "#ddd" ?>;}
.headeroption, .footeroption{background:<?php echo $bgcolor; ?>;
color:<?php echo $fontcolor; ?>;text-align:center;padding:20px;}
.headeroption h2, .footeroption h2{margin:0;font-size:24px}
.maincontent{min-height:400px;padding:20px;font-size:18px}
p{margin:0}
input[type="text"]{width:238px;padding:5px;}
select{font-size:18px;padding:2px 5px;width:250px;}
.tblone{width:100%;border:1px solid #fff;margin:20px 0}
.tblone td{padding:5px 10px;text-align:center;}
table.tblone tr:nth-child(2n+1){background:#fff;height:30px;}
table.tblone tr:nth-child(2n){background:#f1f1f1;height:30px;}
#myform{width:400px;border:1px solid #fff;padding:10px;}
</style>
</head>
<body>
<div class="phpcoding">
<section class="headeroption">
<h2><?php echo "CRUD Using OOP PHP and MYSQLi"; ?></h2>
</section>
<section class="maincontent">
Fourth, then open the footer.php file and paste this line of codes…
</section>
<section class="footeroption">
<p>© <?php echo date("Y"); ?> I Am a PHP Learner</p>
<h2><?php echo "www.programsolve.com"; ?></h2>
</section>
</div>
</body>
</html>
Fifth, then open the index.php file and paste this line of codes…, to view the data
<?php
include 'header.php';
include "config.php";
include "Database.php";
?>
<?php
$db = new Database();
$read = $db->select();
?>
<?php
$db = new Database();
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
$db->delete($id);
}
?>
<?php
if (isset($_GET['msg'])) {
echo "<span style='color:green'>" . $_GET['msg'] . "</span>";
}
?>
<table class="tblone">
<tr>
<th width="10%">Serial</th>
<th width="35%">Name</th>
<th width="25%">Email</th>
<th width="15%">Skill</th>
<th width="15%">Action</th>
</tr>
<?php if($read){?>
<?php
$i=1;
while($row = $read->fetch_assoc()){
?>
<tr>
<td><?php echo $i++ ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['skill']; ?></td>
<td>
<a href="update.php?id=<?php echo urlencode($row['id']); ?>">Edit</a>
<a href="?delete=<?php echo urlencode($row['id']); ?>">Delete</a>
</td>
</tr>
<?php } ?>
<?php } else { ?>
<p>Data is not available !!</p>
<?php } ?>
</table>
<a href="create.php">Create</a>
<?php include 'footer.php'; ?>
Sixth, then open the create.php file and paste this line of codes…, For the insert data
<?php
include 'header.php';
include "config.php";
include "Database.php";
?>
<?php
$db = new Database();
if(isset($_POST['submit'])){
$error = $db->insert($_POST);
}
?>
<?php
if(isset($error)){
echo "<span style='color:red'>".$error."</span>";
}
?>
<form action="create.php" method="post">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" placeholder="Please enter
name"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" placeholder="Please enter
email"/></td>
</tr>
<tr>
<td>Skill</td>
<td><input type="text" name="skill" placeholder="Please enter
Skill"/></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="submit" value="Submit"/>
<input type="reset" Value="Cancel" />
</td>
</tr>
</table>
</form>
<a href="index.php">Go Back</a>
<?php include 'footer.php'; ?>
Seventh, then open the update.php file and paste this line of codes…, For the updated data
<?php
include 'header.php';
include "config.php";
include "Database.php";
?>
<?php
$id = $_GET['id'];
$db = new Database();
$query = "SELECT * FROM tbl_user WHERE id = $id";
$getData = $getData = mysqli_query($db->link, $query)->fetch_assoc();
if (isset($_POST['update'])) {
$error = $db->update($_POST);
}
?>
<?php
if(isset($error)){
echo "<span style='color:red'>".$error."</span>";
}
?>
<form action="update.php?id=<?php echo $id; ?>" method="post">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $getData['name'] ?>"/></td>
<td><input type="hidden" name="id" value="<?php echo $getData['id'] ?>"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $getData['email'] ?>"/></td>
</tr>
<tr>
<td>Skill</td>
<td><input type="text" name="skill" value="<?php echo $getData['skill'] ?>"/></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="update" value="Update" />
</td>
</tr>
</table>
</form>
<a href="index.php">Go Back</a>
<?php include 'footer.php'; ?>
Eight, open the folder and create two file name config.php and Database.php, Note: All file extension will be .php , then open the config file and paste this line of codes…
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "db_crud");
Ninth, open the Database.php file and paste this line of codes…,For the connection of database and the all crud operation like create, read and update all data.
<?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;
}
}
// Insert Data
public function insert($data) {
$name = mysqli_real_escape_string($this->link, $data['name']);
$email = mysqli_real_escape_string($this->link, $data['email']);
$skill = mysqli_real_escape_string($this->link, $data['skill']);
if ($name == '' || $email == '' || $skill == '') {
return $error = "Field must not be Empty !!";
} else {
$query = "INSERT INTO tbl_user(name, email, skill) values('$name', '$email', '$skill') ";
$insert_row = mysqli_query($this->link ,$query) or die($this->link->error . __LINE__);
if ($insert_row) {
header("Location: index.php?msg=" . urlencode('Data Inserted Successfully.'));
exit();
} else {
die("Error : (" . $this->link->errno . ")" . $this->link->error);
}
}
}
// Select Data
public function select() {
$query = "SELECT * FROM tbl_user";
$result = mysqli_query($this->link ,$query) or die($this->link->error . __LINE__);
if ($result->num_rows > 0) {
return $result;
} else {
return false;
}
}
// Update Data
public function update($data) {
$id = mysqli_real_escape_string($this->link, $data['id']);
$name = mysqli_real_escape_string($this->link, $data['name']);
$email = mysqli_real_escape_string($this->link, $data['email']);
$skill = mysqli_real_escape_string($this->link, $data['skill']);
if ($name == '' || $email == '' || $skill == '') {
return $error = "Field must not be Empty !!";
} else {
$query = "UPDATE tbl_user SET name = '$name', email = '$email', skill = '$skill' WHERE id = '$id' ";
$insert_row = mysqli_query($this->link, $query) or die($this->link->error . __LINE__);
if ($insert_row) {
header("Location: index.php?msg=" . urlencode('Data Updated Successfully.'));
exit();
} else {
die("Error : (" . $this->link->errno . ")" . $this->link->error);
}
}
}
// 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);
}
}
}
Finally, save all the file and run code, after run the file first open the index.php file and interface look like.
Then click the create link and go to the data insertion page and the page look like…
Then fill all the fields and click the submit button.
From click the edit and delete button you can edit and delete, I am giving the two picture after edit and delete.
I thin k it is very easy way to learn the crud operation in php using MySQL database.
3 Comments
Do you mind if I quote a couple of your posts as long as I provide credit and sources back
to your webpage? My website is in the exact same niche as yours and my visitors would genuinely benefit from some of the
information you provide here. Please let me know if this ok with
you. Thanks a lot!
I am genuinely pleased to glance at this weblog posts which includes
tons of helpful information, thanks for providing these data.
Hello! This is kind of off topic but I need some advice from an established blog.
Is it very hard to set up your own blog? I’m not very techincal
but I can figure things out pretty quick. I’m thinking
about setting up my own but I’m not sure where to begin. Do you have any ideas or suggestions?
Thank you