0
I have a document with data of json format that I want to insert in the BD phpmyadmin, I am able to insert the first data in the table that are inside "Results", but I am not able to enter in "catechisms_students_attributes", I leave below and save "education_sponsor_description".
Json document:
{
results: [
{
catechist_description: "Albertina Dinis",
class_year: "2017/2018",
finalized: true,
id: 33,
is_for_adults: false,
name: "catequese 2o",
start_time: "23:00",
week_day: "7",
year: 2,
catechisms_students_attributes: [
{
catechism_id: 33,
created_at: "2017-10-19T15:47:49",
deleted: false,
education_sponsor_description: "José Paulo Almeida Bernardes",
education_sponsor_id: 173486,
entity_description: "Manuel Rocha",
entity_id: 173502,
id: 154,
inscription_date: "2017-10-19",
observations: null,
transferred: false,
updated_at: "2017-10-19T15:47:49"
},
{
catechism_id: 33,
created_at: "2017-11-04T11:04:45",
deleted: false,
education_sponsor_description: "Manuel Rocha",
education_sponsor_id: 173502,
entity_description: "teste",
entity_id: 185503,
id: 156,
inscription_date: "2017-11-04",
observations: null,
transferred: false,
updated_at: "2017-11-04T11:04:45"
}
],
}
The code I’m using:
curl_close($ch3);
$decode_catechisms = json_decode($response3, true);
foreach ($decode_catechisms["results"] as $catechisms_object) {
$id_catechisms = $catechisms_object['id'];
$catechist_description = $catechisms_object['catechist_description'];
$class_year = $catechisms_object['class_year'];
$catechist_name = $catechisms_object['name'];
$start_time = $catechisms_object['start_time'];
$week_day = $catechisms_object['week_day'];
$catechism_year = $catechisms_object['year'];
$education_sponsor_description = $catechisms_object['catechisms_students_attributes']['education_sponsor_description'];
//insert into mysql table
$sql = "INSERT INTO catechisms (id_catequese, catechist_description, class_year, name, start_time, week_day, year, education_sponsor_description)
VALUES('$id_catechisms','$catechism_year', '$class_year', '$catechist_name','$start_time', '$week_day', '$catechism_year', '$education_sponsor_description')
ON DUPLICATE KEY UPDATE
id_catequese='$id_catechisms', catechist_description='$catechist_description', class_year='$class_year', name='$catechist_name',"
. " start_time='$start_time', week_day='$week_day', year='$catechism_year', education_sponsor_description='$education_sponsor_description'";
The mistake you’re making is "Notice: Undefined index: education_sponsor_description" I wonder if you could help me?
Give a var_dump no
$catechisms_object['catechisms_students_attributes']
– Francisco
Good evening, I already made and prints the data correctly as they are inside 'cathechisms_students_attributes' hence not realizing why not insert and give error. array(2) { [0]=> array(12) { ["catechism_id"]=> int(33) ["created_at"]=> string(19) "2017-10-19T15:47:49" ["Deleted"]=> bool(false) ["education_sponsor_description"]=> string(29) "José Paulo Almeida Bernardes" ["education_sponsor_id"]=> int(173486) ["entity_description"]=> string(12) "Manuel Rocha" ["entity_id"]=> int(173502) ["id"]=> int(154) ["inscription_date"]=> string(10) "2017-10-19" ["Observations"]=> ...`
– Sofs
But if you var_dump $catechisms_object['catechisms_students_attributes']['education_sponsor_description'], print NULL
– Sofs
The error is because the data is duplicated, to access this value you have to put an index in front:
$education_sponsor_description = $catechisms_object['catechisms_students_attributes'][0]['education_sponsor_description'];
– Francisco