You are on page 1of 13

2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder

Coders Folder
Menu 

Simple CRUD with PHP, MYSQLI

Posted on August 3, 2016 by codersfolder

swimming pool slide

$0
Ad Alibaba.com

Learn more

CRUD (Create, Retrieve, Update, Delete) is a usual job in web development. In Later Career, as a web developer,
you’ll encounter lots of CRUD functionality in your career. The design of a CRUD will allow the users to
create/retrieve/update/remove the data from front-end of the system. Generally, with the help of PHP as a server
side programming language, the data will be stored in MYSQL Database. PHP as a server-side language will be
manipulating MYSQL Database tables to perform a particular action triggered from the users.

Table of Content

1. PHP CRUD Part 1 Introduction

2. PHP CRUD Part 2 Configure Database

3. PHP CRUD Part 3 Setting up project folder

4. PHP CRUD Part 4 Database Connection

5. PHP CRUD Part 5 Create

6. PHP CRUD Part 6 Retrieve

7. PHP CRUD Part 7 Update

8. PHP CRUD Part 8 Remove

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 1/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder

1. PHP CRUD Part 1 Introduction

In this section, overall CRUD operation is introduced as well as we are going to develop a simple CRUD (Create,
Retrieve, Update, Delete) PHP operations. In a web application, these are the basic stuff required to create,
retrieve, update and delete the data using PHP with MYSQLI Database. You will be creating the database tables
and inserting the data’s into the database tables without any fatal error. This is an uncomplicated and easy
tutorial to learn a CRUD operation. To understand how CRUD operations work, I recommend you to go through
each part of this tutorial.

Below video illustrates the final system of this tutorial. The source code of this application will be provided at the
end of this tutorial.

Simple CRUD with Procedural PHP, MYSQLI Part 1 Introduction For B…


B…

2. PHP CRUD Part 2 Configure Database

Database Name : php_crud


Table Name : members
Table Column : id, fname, lname, contact, age, active

To create the database in this tutorial, there are two ways.

2.1 First way to create database

Copy and paste this following SQL command in your MySQL database to create database and table

1 CREATE DATABASE `php_crud`; ?

2
3 // create table
4 CREATE TABLE `php_crud`.`members` (
5 `id` int(11) NOT NULL AUTO_INCREMENT,
6 `fname` varchar(255) NOT NULL,
7 `lname` varchar(255) NOT NULL,
8 `contact` varchar(255) NOT NULL,
9 `age` varchar(255) NOT NULL,
https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 2/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder
10 `active` int(11) NOT NULL DEFAULT '0',
11 PRIMARY KEY (`id`)
12 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

2.2 Second way to create database

A tutorial video to demonstrate you, how to create a database and tables for this tutorial

3. PHP CRUD Part 3 Setting up project folder

In this part, we will be creating a project folder and files for this tutorial. First of all, create the project folder as
“crud_php”. After that create the create.php, edit.php, index.php, and remove.php files inside this project folder
as shown [crud_php/create.php], [crud_php/edit.php] and so on.

Second, Create a folder named as “php_action” inside this project folder as [crud_php/php_action] which will
handle the server-side processing actions. In this directory create a create.php, db_connect.php, remove.php and
update.php file as shown [crud_php/php_action/create.php], [crud_php/php_action/db_connect.php], and so on.

swimming pool slide

$0
Ad Alibaba.com

Learn more

The current file structure should look like below if you have followed the steps correctly:

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 3/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder

The video displays you to create a project folder and files for this tutorial.

4. PHP CRUD Part 4 Database Connection

In the db_connect.php file i.e. [php_action/db_connect.php], this file contains a database connection. In this
application, db_connect.php file will be used to perform any action for CRUD. Such as connecting to the
database, executing the query, and closing the connection.

?
db_connect.php
1 <?php
2
3 $localhost = "127.0.0.1";
4 $username = "root";
5 $password = "";
6 $dbname = "php_crud";
7
8 // create connection
9 $connect = new mysqli($localhost, $username, $password, $dbname);
10
11 // check connection
12 if($connect->connect_error) {
13 die("connection failed : " . $connect->connect_error);
14 } else {
15 // echo "Successfully Connected";
16 }
17
18 ?>

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 4/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder

5. PHP CRUD Part 5 Create

5.1 Adding “Add Member” Button and “Action” button

To start with create.php page we need a button which will lead to create.php page. Open index.php file which we
created in Chapter 3 in this tutorial. And add an “Add Member” button at the top of the table, and “Edit”, “Remove”
button at each row of the table.

Now the index.php file’s code should look like below, the highlighted code are what we need to implement in this
tutorial.

?
crud_php/index.php
1 <?php require_once 'php_action/db_connect.php'; ?>
2
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>PHP CRUD</title>
7
8 <style type="text/css">
9 .manageMember {
10 width: 50%;
11 margin: auto;
12 }
13
14 table {
15 width: 100%;
16 margin-top: 20px;
17 }
18
19 </style>
20
21 </head>
22 <body>
23
24 <div class="manageMember">
25 <a href="create.php"><button type="button">Add Member</button></a>
26 <table border="1" cellspacing="0" cellpadding="0">
27 <thead>
28 <tr>
29 <th>Name</th>
30 <th>Age</th>
31 <th>Contact</th>
32 <th>Option</th>
33 </tr>
34 </thead>
35 <tbody>
36
37 </tbody>
38 </table>
39 </div>
40
41 </body>
42 </html>

Now if you go to the index.php page, you’ll see “Add member” button. Eventually, the create.php is created at
Chapter 3 and it will lead to a blank page when you click the “Add Member” button. In next step, we will create a
form to enter the member information into the system and add the member’s information into the database.

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 5/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder

5.2 Creating a Create page

In create.php file which you created in Chapter 3. This file contains an HTML form where user’s input data will
pass to server side and add the information into the database.

The first part of this codes is to create an HTML form with required input field and pass the data to the server.

?
crud_php/create.php
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Add Member</title>
5
6 <style type="text/css">
7 fieldset {
8 margin: auto;
9 margin-top: 100px;
10 width: 50%;
11 }
12
13 table tr th {
14 padding-top: 20px;
15 }
16 </style>
17
18 </head>
19 <body>
20
21 <fieldset>
22 <legend>Add Member</legend>
23
24 <form action="php_action/create.php" method="post">
25 <table cellspacing="0" cellpadding="0">
26 <tr>
27 <th>First Name</th>
28 <td><input type="text" name="fname" placeholder="First Name" /></td>
29 </tr>
30 <tr>
31 <th>Last Name</th>
32 <td><input type="text" name="lname" placeholder="Last Name" /></td>
33 </tr>
34 <tr>
35 <th>Age</th>
36 <td><input type="text" name="age" placeholder="Age" /></td>
37 </tr>
38 <tr>
39 <th>Contact</th>
40 <td><input type="text" name="contact" placeholder="Contact" /></td>
41 </tr>
42 <tr>
43 <td><button type="submit">Save Changes</button></td>
44 <td><a href="index.php"><button type="button">Back</button></a></td>
45 </tr>

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 6/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder
46 </table>
47 </form>
48
49 </fieldset>
50
51 </body>
52 </html>

The second part of this codes insert process happens. Look through the codes and we will go through them
afterwards:

?
crud_php/php_action/create.php
1 <?php
2
3 require_once 'db_connect.php';
4
5 if($_POST) {
6 $fname = $_POST['fname'];
7 $lname = $_POST['lname'];
8 $age = $_POST['age'];
9 $contact = $_POST['contact'];
10
11 $sql = "INSERT INTO members (fname, lname, contact, age, active) VALUES ('$fname'
12 if($connect->query($sql) === TRUE) {
13 echo "<p>New Record Successfully Created</p>";
14 echo "<a href='../create.php'><button type='button'>Back</button></a>";
15 echo "<a href='../index.php'><button type='button'>Home</button></a>";
16 } else {
17 echo "Error " . $sql . ' ' . $connect->connect_error;
18 }
19
20 $connect->close();
21 }
22
23 ?>

Let us look at the beginning of the code. It first checks if the form is submitted by using a $_POST global
variable. If a form is submitted then it inserts the data into the database by using $_POST. And check if the query
is executed successfully then it displays a successful message and link button.

The end result should look like this if you have followed the instruction correctly. Click on Add member at the
index page and enter the member information into the input field. Click on Save Changes button.

create

After creating some records by entering the member information, you should be able to see a CRUD grid as
below:

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 7/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder

6. PHP CRUD Part 6 Retrieve

In index.php page, we are going to retrieve the data that are stored in the database. This part is quite easy. We
need database connection file to execute the SQL command.
The highlighted codes are created for edit and remove button. You can copy all the codes below.

?
crud_php/index.php
1 <?php require_once 'php_action/db_connect.php'; ?>
2
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>PHP CRUD</title>
7
8 <style type="text/css">
9 .manageMember {
10 width: 50%;
11 margin: auto;
12 }
13
14 table {
15 width: 100%;
16 margin-top: 20px;
17 }
18
19 </style>
20
21 </head>
22 <body>
23
24 <div class="manageMember">
25 <a href="create.php"><button type="button">Add Member</button></a>
26 <table border="1" cellspacing="0" cellpadding="0">
27 <thead>
28 <tr>
29 <th>Name</th>
30 <th>Age</th>
31 <th>Contact</th>
32 <th>Option</th>
33 </tr>
34 </thead>
35 <tbody>
36 <?php
37 $sql = "SELECT * FROM members WHERE active = 1";
38 $result = $connect->query($sql);
39
40 if($result->num_rows > 0) {
41 while($row = $result->fetch_assoc()) {
42 echo "<tr>

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 8/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder
43 <td>".$row['fname']." ".$row['lname']."</td>
44 <td>".$row['age']."</td>
45 <td>".$row['contact']."</td>
46 <td>
47 <a href='edit.php?id=".$row['id']."'><button type='button
48 <a href='remove.php?id=".$row['id']."'><button type='butt
49 </td>
50 </tr>";
51 }
52 } else {
53 echo "<tr><td colspan='5'><center>No Data Avaliable</center></td></tr
54 }
55 ?>
56 </tbody>
57 </table>
58 </div>
59
60 </body>
61 </html>

Now if you open this page “index.php”, you should see the members information on the table. As well as you
should notice the “Edit” and “Remove” button at each table row data. They are not functional for this part. In next
part, we are going to learn about the update functionality for this tutorial.

7. PHP CRUD Part 7 Update

Go to edit.php file in [crud_php/edit.php] which was creating in Chapter 3. We will teach by two parts; first part
creating html form, and second part server side processing. The first part of the codes is an HTML form to
update the information of member. But it will not only update the data, it will display the member information.
Copy the code below to edit.php file at [crud_php/edit.php]:

?
crud_php/edit.php
1 <?php
2
3 require_once 'php_action/db_connect.php';
4
5 if($_GET['id']) {
6 $id = $_GET['id'];
7
8 $sql = "SELECT * FROM members WHERE id = {$id}";
9 $result = $connect->query($sql);
10
11 $data = $result->fetch_assoc();
12
13 $connect->close();
14
15 ?>
https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 9/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder
16
17 <!DOCTYPE html>
18 <html>
19 <head>
20 <title>Edit Member</title>
21
22 <style type="text/css">
23 fieldset {
24 margin: auto;
25 margin-top: 100px;
26 width: 50%;
27 }
28
29 table tr th {
30 padding-top: 20px;
31 }
32 </style>
33
34 </head>
35 <body>
36
37 <fieldset>
38 <legend>Edit Member</legend>
39
40 <form action="php_action/update.php" method="post">
41 <table cellspacing="0" cellpadding="0">
42 <tr>
43 <th>First Name</th>
44 <td><input type="text" name="fname" placeholder="First Name" value="<
45 </tr>
46 <tr>
47 <th>Last Name</th>
48 <td><input type="text" name="lname" placeholder="Last Name" value="<?
49 </tr>
50 <tr>
51 <th>Age</th>
52 <td><input type="text" name="age" placeholder="Age" value="<?php echo
53 </tr>
54 <tr>
55 <th>Contact</th>
56 <td><input type="text" name="contact" placeholder="Contact" value="<?
57 </tr>
58 <tr>
59 <input type="hidden" name="id" value="<?php echo $data['id']?>" />
60 <td><button type="submit">Save Changes</button></td>
61 <td><a href="index.php"><button type="button">Back</button></a></td>
62 </tr>
63 </table>
64 </form>
65
66 </fieldset>
67
68 </body>
69 </html>
70
71 <?php
72 }
73 ?>

Let’s look at the code. At the beginning of the code, it catches the $id from a $_GET request. Then it fetches the
information by that $id variable. After the data is retrieved from the database, the information is added in the
input field. As well as the new input field is also appended to a name “id” to match the specific member data
when the form is submitted.

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 10/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder

In Second part, the data update process is being coded here. Open the update.php file in
crud_php/php_action/update.php which was creating in Chapter 3. Open the file and copy and paste these
codes.

?
crud_php/php_action/update.php
1 <?php
2
3 require_once 'db_connect.php';
4
5 if($_POST) {
6 $fname = $_POST['fname'];
7 $lname = $_POST['lname'];
8 $age = $_POST['age'];
9 $contact = $_POST['contact'];
10
11 $id = $_POST['id'];
12
13 $sql = "UPDATE members SET fname = '$fname', lname = '$lname', age = '$age', cont
14 if($connect->query($sql) === TRUE) {
15 echo "<p>Succcessfully Updated</p>";
16 echo "<a href='../edit.php?id=".$id."'><button type='button'>Back</button></a
17 echo "<a href='../index.php'><button type='button'>Home</button></a>";
18 } else {
19 echo "Erorr while updating record : ". $connect->error;
20 }
21
22 $connect->close();
23
24 }
25
26 ?>

Let us look at the beginning of the code. It first checks if the form is submitted by using a $_POST global
variable. If a form is submitted then it updates the data in the database by using $_POST. And check if the query
is executed successfully then it displays a successful message and link button.

If you followed the step correct then your edit.php file should look like below:

8. PHP CRUD Part 8 Remove

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 11/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder

Go to remove.php file in [crud_php/remove.php] which was creating in Chapter 3. The logic is the same as we
learned in Chapter 7 Update . In first part we’ll create html from and second part server side processing file. Copy
the code below to edit.php file at [crud_php/remove.php]:

?
php_crud/remove.php
1 <?php
2
3 require_once 'php_action/db_connect.php';
4
5 if($_GET['id']) {
6 $id = $_GET['id'];
7
8 $sql = "SELECT * FROM members WHERE id = {$id}";
9 $result = $connect->query($sql);
10 $data = $result->fetch_assoc();
11
12 $connect->close();
13 ?>
14
15 <!DOCTYPE html>
16 <html>
17 <head>
18 <title>Remove Member</title>
19 </head>
20 <body>
21
22 <h3>Do you really want to remove ?</h3>
23 <form action="php_action/remove.php" method="post">
24
25 <input type="hidden" name="id" value="<?php echo $data['id'] ?>" />
26 <button type="submit">Save Changes</button>
27 <a href="index.php"><button type="button">Back</button></a>
28 </form>
29
30 </body>
31 </html>
32
33 <?php
34 }
35 ?>

Let’s look at the code. At the beginning of the code, it catches the $id from a $_GET request. Then it fetches the
information by that $id variable. After the data is retrieved from the database, a new input field is also appended
name “id” to match the specific member data when the form is submitted.

If you followed correctly, then the remove.php page should be similar to below:

?
php_crud/php_action/remove.php
1 <?php
2
3 require_once 'db_connect.php';

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 12/18
2/24/2020 Simple CRUD with PHP, MYSQLI - Coders Folder
4
5 if($_POST) {
6 $id = $_POST['id'];
7
8 $sql = "UPDATE members SET active = 2 WHERE id = {$id}";
9 if($connect->query($sql) === TRUE) {
10 echo "<p>Successfully removed!!</p>";
11 echo "<a href='../index.php'><button type='button'>Back</button></a>";
12 } else {
13 echo "Error updating record : " . $connect->error;
14 }
15
16 $connect->close();
17 }
18
19 ?>

Let’s look at the beginning of the code. It first checks if the form is submitted by using a $_POST global variable.
If a form is submitted then it removes the data. And checks if the query is executed successfully then it displays
a successful message and button link.

Free Product Manuals and User Guides


Ad Free O cial Manual get it now
everydaymanuals
Ad
everydaymanuals.com

Learn more

Download Source Code!!!!

 Download Source Code

Download Database Tables !!!!

 Download Database
Posted in PHPTagged CRUD, MYSQLI, PHP

Related posts

https://codersfolder.com/2016/08/simple-crud-with-php-mysqli/ 13/18

You might also like