创建了数据库记录,但未插入具有键值对数据的数组 - json php



以下json数组数据被提取到php脚本,最终在MySQL数据库中创建记录:

{"address": "4 Ficticious Ave", 
"city": "Miami", 
"country": "United States", 
"email": "jane_doe@gmail.com", 
"first_name": "Jane", 
"last_name": "Doe", 
"state": "FL", 
"zip_code": "03423", 
"response_data": 
"[{"key":"7122", "value":"37-52"},
{"key":"7123","value":"Female"},
{"key":"7124","value":"$35,000 to $50,000 USD"},
{"key":"6176","value":"Miami"},
{"key":"6177","value":"FL"},
{"key":"6179","value":"United States"}]"}

我得到了以下 php 脚本来从 json 数组中获取数据并通过插入数据在 MySQL 数据库中创建记录。但是,除了来自response_data键|值对的数据之外,所有数据都会被填充 - 关联的MySQL列都是空的:

// Identify the content as json
header("Content-Type: application/json; charset=UTF-8");
// get the contents of the JSON file
$data = file_get_contents("php://input");

//this normalize routine was provided by @Elementary in 
// response to my request on Stack Overflow 09052018...
//begin normalize the json in order to be properly decoded
$start=strpos($data,':',strpos($data,'response_data'));
$get=substr($data,$start+1,strrpos($data,'"')-$start);
$data=str_replace($get,trim(trim($get),'"'),$data);
//end normalize

//decode JSON data to PHP array
$content = json_decode($data, true);
//Fetch the details of customer
$Cust_Fname = $content['first_name'];
$Cust_Lname = $content['last_name'];
$Cust_Email = $content['email'];
$Street_Address = $content['address'];
$City = $content['city'];
$State = $content['state'];
$Country = $content['country'];
$Postal_Code = $content['zip_code'];
//also fetch the appended "array" of key/value fields...
$Response_AgeKey = $content['reponse_data'][0]['key'];
$Response_GenderKey = $content['reponse_data'][1]['key'];
$Response_IncomeKey = $content['reponse_data'][2]['key'];
$Response_CityKey = $content['reponse_data'][3]['key'];
$Response_StateKey = $content['reponse_data'][4]['key'];
$Response_CountryKey = $content['reponse_data'][5]['key'];
$Response_Age = $content['reponse_data'][0]['value'];
$Response_Gender = $content['reponse_data'][1]['value'];
$Response_Income = $content['reponse_data'][2]['value'];
$Response_City = $content['reponse_data'][3]['value'];
$Response_State = $content['reponse_data'][4]['value'];
$Response_Country = $content['reponse_data'][5]['value'];

MySQL 数据库显示已创建记录并包含除来自response_data的数据之外的所有数据字段。 考虑到我的语法可能有问题,我试图用这个替换response_data变量:

//try this syntax instead…
$Response_AgeKey = $content['reponse_data']['key'][0];
$Response_GenderKey = $content['reponse_data']['key'][1];
$Response_IncomeKey = $content['reponse_data']['key'][2];
$Response_CityKey = $content['reponse_data']['key'][3];
$Response_StateKey = $content['reponse_data']['key'][4];
$Response_CountryKey = $content['reponse_data']['key'][5];
$Response_Age = $content['reponse_data']['value'][0];
$Response_Gender = $content['reponse_data']['value'][1];
$Response_Income = $content['reponse_data']['value'][2];
$Response_City = $content['reponse_data']['value'][3];
$Response_State = $content['reponse_data']['value'][4];
$Response_Country = $content['reponse_data']['value'][5];

获得相同的结果 - 在 MySQL 数据库中创建一条记录,但response_data数组字段不会填充关联的 MySQL 列。 我可以使用帮助来学习其他方法来识别和获取response_data数组中的数据。 请注意,我不想将response_data数组作为 json 数组插入 MySQL 中——相反,数组中的数据应该进入关联的 MySQL 列!

我已经解决了这个问题,现在能够从 json 数组中获取所有数据以插入到 MySql 中的关联列中。 解决方案涉及将"response_data"的详细信息提取到 php 中的关联数组中:

//also fetch the appended "array" of key/value fields...
$Response_Array = $content['response_data'];

然后,我查看了 php 数组以验证传递的数据并注意结构:

//look at the array to verify data is fetched
var_dump($Response_Array);

最后,我将键|值对的值引入变量中:

//bring the values of response_data array into variables 
$Response_Age = $Response_Array[0]['value'];
$Response_Gender = $Response_Array[1]['value'];
$Response_Income = $Response_Array[2]['value'];
$Response_City = $Response_Array[3]['value'];
$Response_State = $Response_Array[4]['value'];
$Response_Country = $Response_Array[5]['value'];

现在,当创建 MySQL 记录时,将根据需要插入所有数据 [未提供 MySQL 插入代码和错误例程]。

感谢大家的帮助,让我朝着我需要解决的方向前进。

最新更新