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

DB record is created but array with key value pairs data does not get inserted - 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":",000 to ,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 未提供插入代码和错误例程。

感谢大家的帮助,让我找到了解决问题的方向。