Here, you can learn the step-by-step process of how to create CRUD operations in PHP.
We will discuss how to read, insert, modify, and remove data from the MySQL database.
Again, this CRUD operation in PHP using OOP (Object-Oriented Programming) is a significant help for those who are starting to learn PHP with a MySQL database.
CRUD is an acronym for Create, Read, Update, and Delete.
As the name suggests, these operations change data in a database that is important to any web application’s basic functionality.
We can do these things with a PHP application and a MySQL database.
These are the steps on how to run CRUD Operations in PHP with Source Code
-
Download the source code.
-
Extract file.
After you finish downloading the source code, extract the zip file.
- Copy the project folder.
Copy the project folder and paste it into the xampp/htdocs folder.
- Open the xampp.
Open the xampp and start apache and MySQL.
- Open the browser.
Open a browser and go to the URL "http://localhost/phpmyadmin/"
- Create a database.
Click on the databases tab and Create a database naming “peopledb”.
- Import "peopledb.sql".
Click on browse file and select "peopledb.sql" file which is inside the "database" folder, and after import click "go".
- Open the browser and type the folder name.
Open a browser and go to the URL "http://localhost/phpcrud/".
- Explore manipulating.
Final steps, you are free to download the source code and explore manipulating a CRUD Operation.
To create a PHP CRUD Operations make sure that you have already XAMPP and SUBLIME TEXT EDITOR installed on your computer.
- Create Database
We will create a sample database named “peopledb” and then create a table inside it named “people”. The elements will be simple, including a people_id, first_name, last_name, mid_name, address, contact and comment.
CREATE TABLE IF NOT EXISTS `people` (
`people_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`mid_name` varchar(30) NOT NULL,
`address` varchar(30) NOT NULL,
`contact` varchar(30) NOT NULL,
`comment` text NOT NULL,
PRIMARY KEY (`people_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
- Create a Connection Under PHP and MySQL
Next, we will make a config.php file that will connect to the database server.
<?php
$db = mysqli_connect('localhost', 'root', '') or
die ('Unable to connect. Check your connection parameters.');
mysqli_select_db($db, 'peopledb' ) or die(mysqli_error($db));
?>
- Create a Landing Page
We will create a landing page for viewing user information. This file is called "index.php."
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>CRUD Using PHP/MySQL</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/sb-admin.css" rel="stylesheet">
<!-- Morris Charts CSS -->
<link href="css/plugins/morris.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<div id="wrapper">
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">CRUD Using PHP/MySQL</a>
</div>
<!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="active">
<a href="index.php"><i class="fa fa-fw fa-dashboard"></i> Dashboard</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</nav>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
PHP CRUD <small>Create, Read, Update and Delete</small>
</h1>
</div>
</div>
<!-- /.row -->
<div class="col-lg-12">
<h2>User Information's</h2>
<div class="col-lg-6">
<?php include 'config.php'; ?>
<?php $results = mysqli_query($db, "SELECT * FROM people"); ?>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Middle Name</th>
<th>Address</th>
<th>Contact Number</th>
<th>Comment</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['mid_name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><?php echo $row['comment']; ?></td>
<td>
<a href="edit.php?id=<?php echo $row['people_id']; ?>" class="edit_btn" >Edit</a>
</td>
<td>
<a href="del.php?id=<?php echo $row['people_id']; ?>" class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Morris Charts JavaScript -->
<script src="js/plugins/morris/raphael.min.js"></script>
<script src="js/plugins/morris/morris.min.js"></script>
<script src="js/plugins/morris/morris-data.js"></script>
</body>
</html>
- Create a PHP File For Adding Data
We will use a PHP file to make a form where we will do the CRUD operations. This file is called add.php. With the code below, you can make a simple form that saves a user’s "first_name," "last_name," "mid_name," "address," "contact," and "comment".
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>CRUD Using PHP/MySQL</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/sb-admin.css" rel="stylesheet">
<!-- Morris Charts CSS -->
<link href="css/plugins/morris.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<div id="wrapper">
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">CRUD Using PHP/MySQL</a>
</div>
<!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="active">
<a href="index.php"><i class="fa fa-fw fa-dashboard"></i> Dashboard</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</nav>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
PHP CRUD <small>Create, Read, Update and Delete</small>
</h1>
</div>
</div>
<!-- /.row -->
<div class="col-lg-12">
<h2>Add new Records</h2>
<div class="col-lg-6">
<form role="form" method="post" action="transac.php?action=add">
<div class="form-group">
<input class="form-control" placeholder="First Name" name="firstname">
</div>
<div class="form-group">
<input class="form-control" placeholder="Last Name" name="lastname">
</div>
<div class="form-group">
<input class="form-control" placeholder="Middle Name" name="Middlename">
</div>
<div class="form-group">
<input class="form-control" placeholder="Address" name="Address">
</div>
<div class="form-group">
<input class="form-control" placeholder="Contact" name="Contact">
</div>
<div class="form-group">
<label>Comment</label>
<textarea class="form-control" rows="3" name="comment"></textarea>
</div>
<button type="submit" class="btn btn-default">Save Record</button>
<button type="reset" class="btn btn-default">Clear Entry</button>
</form>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Morris Charts JavaScript -->
<script src="js/plugins/morris/raphael.min.js"></script>
<script src="js/plugins/morris/morris.min.js"></script>
<script src="js/plugins/morris/morris-data.js"></script>
</body>
</html>
- Create Executable Codes for Adding Information
We will create an executable code for adding user information into the mysql database.
This file is called "transac.php"
<?php
include('config.php');
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$mname = $_POST['Middlename'];
$address = $_POST['Address'];
$contct = $_POST['Contact'];
$comment = $_POST['comment'];
switch ($_GET['action'])
{
case 'add':
$query = "INSERT INTO people
(people_id,first_name, last_name, mid_name, address,contact, comment)
VALUES ('Null','" . $fname . "','" . $lname . "','" . $mname . "','" . $address . "','$contct','" . $comment . "')";
mysqli_query($db, $query) or die('Error in updating Database');
break;
}
?>
<script type="text/javascript">
alert("Successfully added.");
window.location = "index.php";
</script>
- Create a PHP File For Updating Data
We will create a PHP file for updating User Information, this file is called “edit.php“
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>CRUD Using PHP/MySQL</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/sb-admin.css" rel="stylesheet">
<!-- Morris Charts CSS -->
<link href="css/plugins/morris.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<div id="wrapper">
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">CRUD Using PHP/MySQL</a>
</div>
<!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="active">
<a href="index.php"><i class="fa fa-fw fa-dashboard"></i> Dashboard</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</nav>
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
PHP CRUD <small>Create, Read, Update and Delete</small>
</h1>
</div>
</div>
<!-- /.row -->
<?php
include "config.php";
$query = 'SELECT * FROM people
WHERE
people_id ='.$_GET['id'];
$result = mysqli_query($db, $query) or die(mysqli_error($db));
while($row = mysqli_fetch_array($result))
{
$zz= $row['people_id'];
$i= $row['first_name'];
$a=$row['last_name'];
$b=$row['mid_name'];
$c=$row['address'];
$d=$row['contact'];
$e=$row['comment'];
}
$id = $_GET['id'];
?>
<div class="col-lg-12">
<h2>Edit Records</h2>
<div class="col-lg-6">
<form role="form" method="post" action="edit1.php">
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $zz; ?>" />
</div>
<div class="form-group">
<input class="form-control" placeholder="First Name" name="firstname" value="<?php echo $i; ?>">
</div>
<div class="form-group">
<input class="form-control" placeholder="Last Name" name="lastname" value="<?php echo $a; ?>">
</div>
<div class="form-group">
<input class="form-control" placeholder="Middle Name" name="Middlename" value="<?php echo $b; ?>">
</div>
<div class="form-group">
<input class="form-control" placeholder="Address" name="Address" value="<?php echo $c; ?>">
</div>
<div class="form-group">
<input class="form-control" placeholder="Contact" name="Contact" value="<?php echo $d; ?>">
</div>
<div class="form-group">
<label>Comment</label>
<textarea class="form-control" rows="3" name="comment"><?php echo $e; ?></textarea>
</div>
<button type="submit" class="btn btn-default">Update Record</button>
</form>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Morris Charts JavaScript -->
<script src="js/plugins/morris/raphael.min.js"></script>
<script src="js/plugins/morris/morris.min.js"></script>
<script src="js/plugins/morris/morris-data.js"></script>
</body>
</html>