Documente Academic
Documente Profesional
Documente Cultură
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.
Copy and paste this following SQL command in your MySQL database to create database and table
7
8
10
11
12
// create table
A tutorial video to demonstrate you, how to create a database and tables for this tutorial
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.
The current file structure should look like below if you have followed the steps correctly:
CRUD_PHP_FOLDER
The video displays you to create a project folder and files for this tutorial.
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
8
9
10
11
12
13
14
15
16
17
18
<?php
$localhost = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "php_crud";
// create connection
// check connection
if($connect->connect_error) {
} else {
?>
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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>
<head>
<title>PHP CRUD</title>
<style type="text/css">
.manageMember {
width: 50%;
margin: auto;
table {
width: 100%;
margin-top: 20px;
</style>
</head>
<body>
<div class="manageMember">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Contact</th>
<th>Option</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</body>
</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.
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/index.php
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>
<head>
<title>PHP CRUD</title>
<style type="text/css">
.manageMember {
width: 50%;
margin: auto;
table {
width: 100%;
margin-top: 20px;
</style>
</head>
<body>
<div class="manageMember">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Contact</th>
<th>Option</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</body>
</html>
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.
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.
?
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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html>
<head>
<title>Add Member</title>
<style type="text/css">
fieldset {
margin: auto;
margin-top: 100px;
width: 50%;
table tr th {
padding-top: 20px;
</style>
</head>
<body>
<fieldset>
<legend>Add Member</legend>
<tr>
<th>First Name</th>
</tr>
<tr>
<th>Last Name</th>
</tr>
<tr>
<th>Age</th>
</tr>
<tr>
<th>Contact</th>
</tr>
<tr>
</tr>
</table>
</form>
</fieldset>
</body>
</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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
require_once 'db_connect.php';
if($_POST) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
$contact = $_POST['contact'];
$sql = "INSERT INTO members (fname, lname, contact, age, active) VALUES ('$fname', '$lname',
'$contact', '$age', 1)";
} else {
$connect->close();
?>
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:
create2
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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE html>
<html>
<head>
<title>PHP CRUD</title>
<style type="text/css">
.manageMember {
width: 50%;
margin: auto;
table {
width: 100%;
margin-top: 20px;
</style>
</head>
<body>
<div class="manageMember">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Contact</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php
$result = $connect->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row['fname']." ".$row['lname']."</td>
<td>".$row['age']."</td>
<td>".$row['contact']."</td>
<td>
</td>
</tr>";
} else {
?>
</tbody>
</table>
</div>
</body>
</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.
crud_php/edit.php
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
require_once 'php_action/db_connect.php';
if($_GET['id']) {
$id = $_GET['id'];
$result = $connect->query($sql);
$data = $result->fetch_assoc();
$connect->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Member</title>
<style type="text/css">
fieldset {
margin: auto;
margin-top: 100px;
width: 50%;
table tr th {
padding-top: 20px;
</style>
</head>
<body>
<fieldset>
<legend>Edit Member</legend>
<tr>
<th>First Name</th>
</tr>
<tr>
<th>Last Name</th>
</tr>
<tr>
<th>Age</th>
</tr>
<tr>
<th>Contact</th>
</tr>
<tr>
</tr>
</table>
</form>
</fieldset>
</body>
</html>
<?php
?>
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.
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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
require_once 'db_connect.php';
if($_POST) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
$contact = $_POST['contact'];
$id = $_POST['id'];
$sql = "UPDATE members SET fname = '$fname', lname = '$lname', age = '$age', contact = '$contact'
WHERE id = {$id}";
$connect->close();
?>
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:
update
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
6
7
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
require_once 'php_action/db_connect.php';
if($_GET['id']) {
$id = $_GET['id'];
$result = $connect->query($sql);
$data = $result->fetch_assoc();
$connect->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Remove Member</title>
</head>
<body>
<h3>Do you really want to remove ?</h3>
</form>
</body>
</html>
<?php
?>
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:
remove
php_crud/php_action/remove.php
5
6
10
11
12
13
14
15
16
17
18
19
<?php
require_once 'db_connect.php';
if($_POST) {
$id = $_POST['id'];
$connect->close();
?>
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.