0

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 />
2
  • 2
    What is the problem you are facing with your current code? Commented Apr 9, 2016 at 14:23
  • You didn't ask a question. But $user is not defined, maybe you meant $json[$username] Commented Apr 9, 2016 at 14:27

1 Answer 1

1

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;
    }
}

Sign up to request clarification or add additional context in comments.

7 Comments

updated my question according to your suggestion, now i am getting php error message, How can i know the line where error happened..
What is your php version? array_column is available on php >= 5.5
php files are in free website hosting site as in 000webhost.com, and I cant find anything as logs in the control panel.
php version is 5.2
Here you can find how to emulate array_column
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.