Ajax -the Max
Ajax -the Max

Reputation: 1069

insert json data using php

i am trying to insert data into json of the form

[{"name":"name", "username": "xyz", "email":"[email protected]", "password":"password0387"}]

into a file in the same folder. Passing the data using post method, and the php inserts the data into the json file. Here is my modified php code:

    <?php


//checking if the script received a post request or not 

if($_SERVER['REQUEST_METHOD']=='POST'){

//Getting post data 

$name = $_POST['name'];

$username = $_POST['username'];

$password = $_POST['password'];

$email = $_POST['email'];


//checking if the received values are blank


if($name == '' || $username == '' || $password == '' || $email == ''){


//giving a message to fill all values if the values are blank


echo 'please fill all values';

        }else{

//If the values are not blank


//Load the file
$contents = file_get_contents('user_doc.json');

//Decode the JSON data into a PHP array.
$json = json_decode($contents, true);

$user = array_search($username, array_column( $json, 'username' ) );

if( $user !== False ) 
$json[$user] = array("name" => $name, "username" => $username, "password" => $password, "email" => $email);
else
$json[] = array("name" => $name, "username" => $username, "password" => $password, "email" => $email);


//Encode the array back into a JSON string.
$json = json_encode($json);

//Save the file.
file_put_contents('user_doc.json', $json);
}
}else{

echo "error";

}

and I am able to successfully post the request but I am getting php error, please help I am new to php..the logcat is shown below:

D/success: <br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><font face='Arial' size='1' color='#000000'><b>PHP Error Message</b></font></td></tr></table><br />

Upvotes: 0

Views: 4008

Answers (1)

fusion3k
fusion3k

Reputation: 11689

You have a JSON in this format:

[
    {"username": "abc", "email":"[email protected]"},
    {"username": "xyz", "email":"[email protected]"}
]

that, decoded, produce this array:

[0][ 'username'=>'abc', 'email'=>'[email protected]' ]
[1][ 'username'=>'xyz', 'email'=>'[email protected]' ]

Your code:

$json[$user] = array("username" => $username, "email" => $email);

$user appears as not defined.

If you want simply add new user to JSON, modify above line in this way:

$json[] = array("username" => $username, "email" => $email);

Otherwise, if you want update an existent user or add it if it doesn't exist, you have first to search for the user existence:

$user = array_search( $username, array_column( $json, 'username' ) );

if( $user !== False ) $json[$user] = array("username" => $username, "email" => $email);
else                  $json[]      = array("username" => $username, "email" => $email);

Edit:

On PHP < 5.5, array_column() is not available.

If you have PHP >= 5.3 you can use this alternative. On php < 5.3, you can use this syntax to create your own array_column:

if( ! function_exists( 'array_column' ) )
{
    function array_column( $array, $column_name )
    {
        function get_column( &$element, $key, $column_name )
        {
            $element = $element[$column_name];
        }
        array_walk( $array, 'get_column', $column_name  );
        return $array;
    }
}

Upvotes: 1

Related Questions