CRUD Operation on JSON File using PHP

Getting Started

In the previous tutorial, How to create JSON File from MySQL Database using PHP, we have discussed on 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 our 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. Create a new file, name it as index.php and paste the codes below.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CRUD Operation on JSON File using PHP</title>
  6. </head>
  7. <body>
  8. <a href="add.php">Add</a>
  9. <table border="1">
  10. <thead>
  11. <th>ID</th>
  12. <th>Firstname</th>
  13. <th>Lastname</th>
  14. <th>Address</th>
  15. <th>Gender</th>
  16. <th>Action</th>
  17. </thead>
  18. <tbody>
  19. <?php
  20. //fetch data from json
  21. $data = file_get_contents('members.json');
  22. //decode into php array
  23. $data = json_decode($data);
  24.  
  25. $index = 0;
  26. foreach($data as $row){
  27. echo "
  28. <tr>
  29. <td>".$row->id."</td>
  30. <td>".$row->firstname."</td>
  31. <td>".$row->lastname."</td>
  32. <td>".$row->address."</td>
  33. <td>".$row->gender."</td>
  34. <td>
  35. <a href='edit.php?index=".$index."'>Edit</a>
  36. <a href='delete.php?index=".$index."'>Delete</a>
  37. </td>
  38. </tr>
  39. ";
  40.  
  41. $index++;
  42. }
  43. ?>
  44. </tbody>
  45. </table>
  46. </body>
  47. </html>

Creating our Add Form and Script

Next, we are going to create our add form with the add script whenever the form is submitted. Create a new file, name it as add.php and paste the codes below.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CRUD Operation on JSON File using PHP</title>
  6. </head>
  7. <body>
  8. <form method="POST">
  9. <a href="index.php">Back</a>
  10. <p>
  11. <label for="id">ID</label>
  12. <input type="text" id="id" name="id">
  13. </p>
  14. <p>
  15. <label for="firstname">Firstname</label>
  16. <input type="text" id="firstname" name="firstname">
  17. </p>
  18. <p>
  19. <label for="lastname">Lastname</label>
  20. <input type="text" id="lastname" name="lastname">
  21. </p>
  22. <p>
  23. <label for="address">Address</label>
  24. <input type="text" id="address" name="address">
  25. </p>
  26. <p>
  27. <label for="gender">Gender</label>
  28. <input type="text" id="gender" name="gender">
  29. </p>
  30. <input type="submit" name="save" value="Save">
  31. </form>
  32.  
  33. <?php
  34. if(isset($_POST['save'])){
  35. //open the json file
  36. $data = file_get_contents('members.json');
  37. $data = json_decode($data);
  38.  
  39. //data in out POST
  40. $input = array(
  41. 'id' => $_POST['id'],
  42. 'firstname' => $_POST['firstname'],
  43. 'lastname' => $_POST['lastname'],
  44. 'address' => $_POST['address'],
  45. 'gender' => $_POST['gender']
  46. );
  47.  
  48. //append the input to our array
  49. $data[] = $input;
  50. //encode back to json
  51. $data = json_encode($data, JSON_PRETTY_PRINT);
  52. file_put_contents('members.json', $data);
  53.  
  54. header('location: index.php');
  55. }
  56. ?>
  57. </body>
  58. </html>

Creating our 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. Create a new file, name it as edit.php and paste the codes below.
  1. <?php
  2. //get the index from URL
  3. $index = $_GET['index'];
  4.  
  5. //get json data
  6. $data = file_get_contents('members.json');
  7. $data_array = json_decode($data);
  8.  
  9. //assign the data to selected index
  10. $row = $data_array[$index];
  11.  
  12. ?>
  13. <!DOCTYPE html>
  14. <html>
  15. <head>
  16. <meta charset="utf-8">
  17. <title>CRUD Operation on JSON File using PHP</title>
  18. </head>
  19. <body>
  20. <form method="POST">
  21. <a href="index.php">Back</a>
  22. <p>
  23. <label for="id">ID</label>
  24. <input type="text" id="id" name="id" value="<?php echo $row->id; ?>">
  25. </p>
  26. <p>
  27. <label for="firstname">Firstname</label>
  28. <input type="text" id="firstname" name="firstname" value="<?php echo $row->firstname; ?>">
  29. </p>
  30. <p>
  31. <label for="lastname">Lastname</label>
  32. <input type="text" id="lastname" name="lastname" value="<?php echo $row->lastname; ?>">
  33. </p>
  34. <p>
  35. <label for="address">Address</label>
  36. <input type="text" id="address" name="address" value="<?php echo $row->address; ?>">
  37. </p>
  38. <p>
  39. <label for="gender">Gender</label>
  40. <input type="text" id="gender" name="gender" value="<?php echo $row->gender; ?>">
  41. </p>
  42. <input type="submit" name="save" value="Save">
  43. </form>
  44.  
  45. <?php
  46. if(isset($_POST['save'])){
  47. //set the updated values
  48. $input = array(
  49. 'id' => $_POST['id'],
  50. 'firstname' => $_POST['firstname'],
  51. 'lastname' => $_POST['lastname'],
  52. 'address' => $_POST['address'],
  53. 'gender' => $_POST['gender']
  54. );
  55.  
  56. //update the selected index
  57. $data_array[$index] = $input;
  58.  
  59. //encode back to json
  60. $data = json_encode($data_array, JSON_PRETTY_PRINT);
  61. file_put_contents('members.json', $data);
  62.  
  63. header('location: index.php');
  64. }
  65. ?>
  66. </body>
  67. </html>

Creating our Delete Script

Lastly, we create the script that deletes the row/item in our json file. Create a new file, name it as delete.php and paste the codes below.
  1. <?php
  2. //get the index
  3. $index = $_GET['index'];
  4.  
  5. //fetch data from json
  6. $data = file_get_contents('members.json');
  7. $data = json_decode($data);
  8.  
  9. //delete the row with the index
  10. unset($data[$index]);
  11.  
  12. //encode back to json
  13. $data = json_encode($data, JSON_PRETTY_PRINT);
  14. file_put_contents('members.json', $data);
  15.  
  16. header('location: index.php');
  17. ?>
That ends this tutorial. Happy Coding :)

Comments

Submitted byArvind Gupta (not verified)on Sat, 03/06/2021 - 17:05

Can you please help with the memers.json file. ?

Add new comment