Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
CRUD Operation using JSON File in PHP

CRUD Operation using JSON File in PHP

Posted on March 6, 2020December 17, 2022 By XpertPhp No Comments on CRUD Operation using JSON File in PHP

In this article, We will discuss creating PHP CRUD using JSON File(CRUD Operation using JSON File in PHP). We want to demonstrate how to connect PHP with JSON File, We hope you can learn something new from this article. in our post through how to connect PHP to JSON File, create a new record, fetch a record, update record and delete the record.

Here, We need to convert PHP array to JSON for reading and write data. so we are using an array to JSON PHP in this example.

php_crud

Step 1: fetch all records
Create a new PHP file index.php in this file in add the below code.
The following code shows how to fetch all the records from the database in the index file.

1
2
3
4
5
6
7
8
9
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CRUD Operation on JSON File using PHP</title>
</head>
<body>
<table border="1" align="center">
    <tr>
        <td colspan="7" align="right"><a href="add.php">Add</a></td>
    </tr>
    <tr>
        <td>First Name</td>
        <td>Last Name</td>
        <td>Address</td>
        <td>Email</td>
        <td>Mobile</td>
        <td>Action</td>
    </tr>
    <?php
$data = file_get_contents('users.json');
$data = json_decode($data);
$index = 1;
if(!empty($data)){
foreach($data as $row){
        ?>
        <tr>
            <td><?php echo $row->first_name; ?></td>
            <td><?php echo $row->last_name; ?></td>
            <td><?php echo $row->email; ?></td>
            <td><?php echo $row->address; ?></td>
            <td><?php echo $row->mobile; ?></td>
            <td><a href="update.php?edit_id=<?php echo $index; ?>">Edit</a>&nbsp; &nbsp;<a href="delete.php?delete_id=<?php echo $index; ?>">Delete</a> </td>
        </tr>
        <?php
$index++;
}
    }
    ?>
  
</table>
</body>
</html>

Step 2: Create a new record

Create a PHP file add.php in this file in add the below code.
The code below in the INSERT query through create a new record to the JSON file. This HTML form contains input fields to enter the user’s data to be inserted into the JSON file.

1
2
3
4
5
6
7
8
9
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CRUD Operation using JSON File in PHP</title>
</head>
<body>
<?php
if(isset($_POST['btnadd'])){
$data = file_get_contents('users.json');
$data = json_decode($data, true);
$add_arr = array(
'first_name' => $_POST['txtFname'],
'last_name' => $_POST['txtLname'],
'address' => $_POST['txtAddress'],
'email' => $_POST['txtEmail'],
'mobile' => $_POST['txtMobile']
);
$data[] = $add_arr;
 
$data = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents('users.json', $data);
header('location: index.php');
}
?>
<form method="post" name="frmAdd">
        <table align="center">
            <tr>
                <td colspan="2" align="center">Add Record</td>
            </tr>
 
            <tr>
                <td>First Name</td>
                <td><input type="text" name="txtFname"> </td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td><input type="text" name="txtLname"> </td>
            </tr>
            <tr>
                <td>Address</td>
                <td><textarea name="txtAddress" rows="4" cols="16"></textarea> </td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="text" name="txtEmail"> </td>
            </tr>
            <tr>
                <td>Mobile</td>
                <td><input type="text" name="txtMobile"> </td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Add" name="btnadd"> </td>
            </tr>
        </table>
</form>
</body>
</html>
See also  Codeigniter 4 CRUD Operation With Ajax Example

Step 3: Update a record
Create a new update.php file and in this file to add the below code.
First, we will fetch a particular data record in the update form. On submitting edited user information we form an update query to edit the record with the reference of its edit_id.

1
2
3
4
5
6
7
8
9
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CRUD Operation using JSON File in PHP</title>
</head>
<body>
<?php
$edit_id = $_GET['edit_id'];
//get json data
$data = file_get_contents('users.json');
$data_array = json_decode($data, true);
$row = $data_array[$edit_id];
?>
<?php
if(isset($_POST['btnUpdate']))
{
$update_arr = array(
'id' => $_POST['id'],
'first_name' => $_POST['txtFname'],
'last_name' => $_POST['txtLname'],
'address' => $_POST['txtAddress'],
'email' => $_POST['txtEmail'],
'mobile' => $_POST['txtMobile']
);
$data_array[$edit_id] = $update_arr;
$data = json_encode($data_array, JSON_PRETTY_PRINT);
file_put_contents('users.json', $data);
header('location: index.php');
}
?>
 
<form method="post" name="frmUpdate">
    <table align="center">
        <tr>
            <td colspan="2" align="center">Update Record</td>
        </tr>
 
        <tr>
            <td>First Name</td>
            <td><input type="text" name="txtFname" value="<?php echo $row['first_name'];?>"> </td>
        </tr>
        <tr>
            <td>Last Name</td>
            <td><input type="text" name="txtLname" value="<?php echo $row['last_name'];?>"> </td>
        </tr>
        <tr>
            <td>Address</td>
            <td><textarea name="txtAddress" rows="4" cols="16"><?php echo $row['address'];?></textarea> </td>
        </tr>
        <tr>
            <td>Email</td>
            <td><input type="text" name="txtEmail" value="<?php echo $row['email'];?>"> </td>
        </tr>
        <tr>
            <td>Mobile</td>
            <td><input type="text" name="txtMobile" value="<?php echo $row['mobile'];?>"> </td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="Update" name="btnUpdate"> </td>
        </tr>
    </table>
</form>
</body>
</html>

6. Delete Record from JSON File

Create a new delete.php file in this file to add the below code.
The below code is used to delete a particular record from the database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$delete_id = $_GET['delete_id'];
$data = file_get_contents('users.json');
$data = json_decode($data, true);
unset($data[$delete_id]);
//encode back to json
$data = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents('users.json', $data);
header('location: index.php');
?>

We think would you like this article, so you can click on “Show Demo” button and you can see this demo article.

Show Demo

Php, MySql Tags:array to json php, php array to json, php learn

Post navigation

Previous Post: Generate a PDF file using Puppeteer in Node js
Next Post: how to resize an image to thumbnail in laravel 6

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com