Null值EDITED寫入JSON文件用PHP
問題 - 函數運行後的空值被寫入JSON文件。
期望 - 捕獲數據輸入HTML表格,追加它到一個現有的JSON文件。
一些我使用堆棧資源:
- How do I extract data from JSON with PHP?
- How to loop through JSON array using PHP
- how to write a json file as a datasource in php?
- How to Append JSON File in PHP?
- How to add to json array in .json file with php
- How can I write data in local JSON file using jQuery?
我的JSON文件看起來像這樣:
{
"records": [
{"Date":"05/04/2016","Miles":168081,"Gallons":11.003,"Cost":28.60,"MPG":24.1,"Street":"5500 Mirage St","City":"Yorba Linda","State":"CA","Zip":92807,"Time":"07:04"},
{"Date":"04/18/2016","Miles":167815,"Gallons":10.897,"Cost":27.23,"MPG":25.7,"Street":"5500 Mirage St","City":"Yorba Linda","State":"CA","Zip":92807,"Time":"15:46"}
],
"error" : false,
"status" : 200
}
編輯 -我的PHP腳本看起來像這樣(更新實現齋的代碼):
<?php
function runMyFunction() {
// #####
if ($_SERVER['REQUEST_METHOD'] !== 'POST' && !isset($_POST)) { //You may add $_POST[post_name] for validation
die('I need post method!');
}
// check if file exists
$filename = 'mpg-data-file2.json';
if (! file_exists($filename)) {
echo 'ERROR:' . $filename . ' not found' . '<BR>';
} else {
echo 'OK: ' . $filename . ' exists.' . '<BR>';
$data = array(
"Date"=> $_POST['mpg_date'],
"Miles"=> $_POST['mpg_miles'],
"Gallons"=> $_POST['mpg_gallons'],
"Cost"=> $_POST['mpg_cost'],
"MPG"=> $_POST['mpg_mpg'],
"Street"=> $_POST['mpg_street'],
"City"=> $_POST["mpg_city"],
"State"=> $_POST['mpg_state'],
"Zip"=> $_POST['mpg_zip'],
"Time"=> $_POST['mpg_time']
);
//Load data from json file
$dataFile = file_get_contents("mpg-data-file2.json");
$dataFile = json_decode($str_data,true);
//Merge data from post with data from file
$formattedData = array_merge($dataFile['records'],$data);
$formattedData = json_encode($formattedData);
//If data from file is empty, just use data from post
if (empty($dataFile)) {
$formattedData = json_encode($data);
}
//Set a parent key
$records['records'] = $formattedData;
//Overwites everything
/* $handle = fopen($filename,'a+');
fwrite($handle,$records);
fclose($handle); */
file_put_contents($filename,$records, FILE_APPEND | LOCK_EX);
print_r($formattedData);
echo 'OK: ' . '<BR>' . $records . '<BR>';
}
// #####
}//end runMyFunction
/*
if (isset($_GET['hello'])) {
runMyFunction();
} */
if(isset($_POST['mpg_date'],$_POST['mpg_date'],$_POST['mpg_miles'],$_POST['mpg_gallons'],$_POST['mpg_cost'],$_POST['mpg_mpg'],$_POST['mpg_street'],$_POST["mpg_city"],$_POST['mpg_state'],$_POST['mpg_zip'],$_POST['mpg_time'])) {
runMyFunction($_POST);
}
?>
HTML表單的摘錄看起來像這個:
<form action="_process.php" method="POST" role="form">
<div class="form-group">
<label for="mpg_date">Fuel Date:</label>
<input type="text" class="form-control" id="mpg_date" name="mpg_date" placeholder="04/18/2016">
</div>
<div class="form-group">
<label for="mpg_miles">Odometer (Miles):</label>
<input type="number" min=」0″ step="any" class="form-control" id="mpg_miles" name="mpg_miles" placeholder="167815">
</div>
<!-- And so on... -->
<div>
<a href='_process.php?hello=true'>Run PHP Function</a>
</div>
</form>
你檢查,如果所有這些$ _ POST包含有效的數值?回聲他們看到他們的價值(如'var_dump($ _ POST)')。 –
null在哪裏?整個數組是null還是隻有一個元素? – WillardSolutions
所有元素爲空:{「records」:{「Date」:null,「Miles」:null,「Gallons」:null,「Cost」:null,「MPG」:null,「Street」:null, 「:null,」State「:null,」Zip「:null,」Time「:null}} –