CRUD Operation on JSON File using PHP

February 23, 2021
PHP
CRUD Operation on json file

Tutorial: CRUD Operation on JSON File using PHP

Getting Started on CRUD Operation of JSON File using PHP

This This is In the previous tutorial, How to create JSON File from MySQL Database using PHP, we have discussed how to create a JSON file from MySQL Database.

This time, we are going to create a CRUD on JSON file, and we’re going to use the JSON file generated in the previous tutorial as our sample JSON file.

Displaying a Data

First, we display the data in our JSON file in the form of a table, and we are going to add the three actions: add, edit and delete.

Please create a new file, name it as index.php and paste the codes below.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>CRUD Operation on JSON File using PHP</title>
</head>
<body>
<a href="add.php">Add</a>
<table border="1">
    <thead>
        <th>ID</th>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Address</th>
        <th>Gender</th>
        <th>Action</th>
    </thead>
    <tbody>
        <?php
            //fetch data from json
            $data = file_get_contents('members.json');
            //decode into php array
            $data = json_decode($data);
 
            $index = 0;
            foreach($data as $row){
                echo "
                    <tr>
                        <td>".$row->id."</td>
                        <td>".$row->firstname."</td>
                        <td>".$row->lastname."</td>
                        <td>".$row->address."</td>
                        <td>".$row->gender."</td>
                        <td>
                            <a href='edit.php?index=".$index."'>Edit</a>
                            <a href='delete.php?index=".$index."'>Delete</a>
                        </td>
                    </tr>
                ";
 
                $index++;
            }
        ?>
    </tbody>
</table>
</body>
</html>

Creating an Add Form and Script

Next, we are going to create our add form with the add script whenever the form is submitted. Please create a new file, name it as add.php and paste the codes below.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>CRUD Operation on JSON File using PHP</title>
</head>
<body>
<form method="POST">
    <a href="index.php">Back</a>
    <p>
        <label for="id">ID</label>
        <input type="text" id="id" name="id">
    </p>
    <p>
        <label for="firstname">Firstname</label>
        <input type="text" id="firstname" name="firstname">
    </p>
    <p>
        <label for="lastname">Lastname</label>
        <input type="text" id="lastname" name="lastname">
    </p>
    <p>
        <label for="address">Address</label>
        <input type="text" id="address" name="address">
    </p>
    <p>
        <label for="gender">Gender</label>
        <input type="text" id="gender" name="gender">
    </p>
    <input type="submit" name="save" value="Save">
</form>
 
<?php
    if(isset($_POST['save'])){
        //open the json file
        $data = file_get_contents('members.json');
        $data = json_decode($data);
 
        //data in out POST
        $input = array(
            'id' => $_POST['id'],
            'firstname' => $_POST['firstname'],
            'lastname' => $_POST['lastname'],
            'address' => $_POST['address'],
            'gender' => $_POST['gender']
        );
 
        //append the input to our array
        $data[] = $input;
        //encode back to json
        $data = json_encode($data, JSON_PRETTY_PRINT);
        file_put_contents('members.json', $data);
 
        header('location: index.php');
    }
?>
</body>
</html>

Creating an Edit Form and Script

Next, we create our edit form containing the data of selected row/item in our data with the script to update our JSON if the form is submitted.

Please create a new file, name it as edit.php and paste the codes below.

<?php
    //get the index from URL
    $index = $_GET['index'];
 
    //get json data
    $data = file_get_contents('members.json');
    $data_array = json_decode($data);
 
    //assign the data to selected index
    $row = $data_array[$index];
 
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>CRUD Operation on JSON File using PHP</title>
</head>
<body>
<form method="POST">
    <a href="index.php">Back</a>
    <p>
        <label for="id">ID</label>
        <input type="text" id="id" name="id" value="<?php echo $row->id; ?>">
    </p>
    <p>
        <label for="firstname">Firstname</label>
        <input type="text" id="firstname" name="firstname" value="<?php echo $row->firstname; ?>">
    </p>
    <p>
        <label for="lastname">Lastname</label>
        <input type="text" id="lastname" name="lastname" value="<?php echo $row->lastname; ?>">
    </p>
    <p>
        <label for="address">Address</label>
        <input type="text" id="address" name="address" value="<?php echo $row->address; ?>">
    </p>
    <p>
        <label for="gender">Gender</label>
        <input type="text" id="gender" name="gender" value="<?php echo $row->gender; ?>">
    </p>
    <input type="submit" name="save" value="Save">
</form>
 
<?php
    if(isset($_POST['save'])){
        //set the updated values
        $input = array(
            'id' => $_POST['id'],
            'firstname' => $_POST['firstname'],
            'lastname' => $_POST['lastname'],
            'address' => $_POST['address'],
            'gender' => $_POST['gender']
        );
 
        //update the selected index
        $data_array[$index] = $input;
 
        //encode back to json
        $data = json_encode($data_array, JSON_PRETTY_PRINT);
        file_put_contents('members.json', $data);
 
        header('location: index.php');
    }
?>
</body>
</html>

Creating a Delete Script

Lastly, we create the script that deletes the row/item in our JSON file. Please create a new record, name it as delete.php and paste the codes below.

<?php
    //get the index
    $index = $_GET['index'];
 
    //fetch data from json
    $data = file_get_contents('members.json');
    $data = json_decode($data);
 
    //delete the row with the index
    unset($data[$index]);
 
    //encode back to json
    $data = json_encode($data, JSON_PRETTY_PRINT);
    file_put_contents('members.json', $data);
 
    header('location: index.php');
?>
CRUD Operation on json file using php

CRUD Operation

That ends this tutorial. Happy Coding!


Leave a Reply

Your email address will not be published. Required fields are marked *