CRUD Operation using JSON File in PHP – onlinecode

CRUD Operation using JSON File in PHP – onlinecode

In this post we will give you information about CRUD Operation using JSON File in PHP – onlinecode. Hear we will give you detail about CRUD Operation using JSON File in PHP – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this article, We will discuss creating PHP CRUD using JSON File. 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.

Step 1: fetch all recordsCreate 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.

<!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.

<!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><pre name="txtAddress" rows="4" cols="16"></pre> </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>

Step 3: Update a recordCreate 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.

<!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><pre name="txtAddress" rows="4" cols="16"><?php echo $row['address'];?></pre> </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.

<?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

Please follow and like us:

Hope this code and post will helped you for implement CRUD Operation using JSON File in PHP – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

30  +    =  32

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US