Right now I have this form:
<form name='steampaypalinfo' action='profile.php' method='post'>
PayPal Email: <br><input type='email' name='paypal'><br>
Steam Trade URL: <br><input type='text' name='tradeurl'><br><br>
<input type='submit' value='Update' name='submit'>
</form>
I am retrieving the data in PHP like this:
if (isset($_POST['submit'])) {
if (empty($_POST['paypal'])) {
$paypalerror = "PayPal email is required!";
} else {
$paypalemail = $_POST['paypal'];
}
if (empty($_POST['tradeurl'])) {
$tradeurlerror = "Steam Trade URL is required!";
} else {
$tradeurl = $_POST['tradeurl'];
}
As you can see, I am storing the form data (email and link) into two variables called paypalemail and tradeurl.
Now I want to add this data into a JSON file that I have already made. The JSON file looks like this right now:
{
"response": {
"players": [
{
"steamid": "76561198064105349",
"communityvisibilitystate": 3,
"profilestate": 1,
"personaname": "PUGLORD",
"lastlogoff": 1445136051,
"commentpermission": 2,
"profileurl": "http://steamcommunity.com/id/ashland3000/",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f65576bed67efe25134478a63ae51c782b58de65.jpg",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f65576bed67efe25134478a63ae51c782b58de65_medium.jpg",
"avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f65576bed67efe25134478a63ae51c782b58de65_full.jpg",
"personastate": 0,
"realname": "Jakob",
"primaryclanid": "103582791439857810",
"timecreated": 1337817157,
"personastateflags": 0,
"loccountrycode": "US",
"locstatecode": "NY"
}
]
}
}
I want the data paypalemail and tradeurl data to go into the players array. I read about using file_put_contents or fwrite but none of that seemed to work.
Question: How can I add the PayPal and Steam URL Data into the JSON file that is already made? How can I add that data on to the already made data in the JSON file and format it correctly?
Thank you, any help would be great!
EDIT I tried this:
$file = file_get_contents("cache/players/{$steam64}.json");
$json = json_decode($file, true);
$player_array['paypalemail'] = $paypalemail;
$json = json_encode($player_array);
$file = fopen("cache/players/{$steam64}.json", 'w');
fwrite($file, $json);
fclose($file);
It works but it overwrites the data in the JSON file. How can I add it but not overwrite it?