i want to insert .json data to MySQL. I used code from here(Decode Json data Array and insert in to mysql) but i have a trouble with null values.
json files (example)
[{'counsel_id':'2019-000005', 'big_cate':'Smartphone',
'mid_cate':'Orange', 'small_cate':'AA',
'title':'faceID doesn't work',
'question_date':'2019-08-01',
'question':'faceID doesn't work. how can i fix it?',
'answer-date':'2019-08-01',
'answer':'hello blah blah'
},
{'counsel_id':'2019-000015', 'big_cate':'Smartphone',
'mid_cate':'Star', 'small_cate':'BB',
'title':'Fingerprint Recognition doesn't work',
'question_date':'2019-08-10',
'question':'Fingerprint Recognition doesn't work. how can i fix it?',
'answer-date':'2019-08-11',
'answer':'hello blah blah'
},
{'counsel_id':'2019-000018', 'big_cate':'Smartphone',
'mid_cate':'Orange', 'small_cate':'AA',
'title':'The screen broken',
'question_date':'2019-08-16',
'question':'How much will it cost to fix it??'
}]
MySQL TABLE
CREATE TABLE IF NOT EXISTS `counsel` (
`id` INT NOT NULL AUTO_INCREMENT,
`counsel_id` CHAR(13) NOT NULL,
`big_cate` VARCHAR(15) NOT NULL,
`mid_cate` VARCHAR(15) NOT NULL,
`small_cate` VARCHAR(15) NOT NULL,
`title` VARCHAR(45) NOT NULL,
`question_date` DATETIME NOT NULL,
`question` TEXT NOT NULL,
`answer_date` DATETIME NULL,
`answer` TEXT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
php code
<?php
$jsonFile="final_03.json";
$jsondata = file_get_contents($jsonFile);
$data = json_decode($jsondata, true);
$servername = "localhost";
$username = "root";
$password = "mypassword";
$dbname = "myDBname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($data as $row) {
$sql = "INSERT INTO counsel (counsel_id, big_cate, mid_cate, small_cate,
title, question_date, question)
VALUES ('" . $row["receiptNum"] . "', '" . $row["gooboon"] . "',
'" . $row["itemCode"] . "','" . $row["item"] . "',
'" . $row["title"] . "','" . $row["date"] . "',
'" . $row["question"] . "')";
$conn->query($sql);
}
$conn->close();
?>
I want to insert all of json data to MySQL, but have an error when json file doesn't have 'answer-date', 'answer'. If json list does not have 'answer-date', 'answer', I would like to add a null value. Can anyone help me?
(+) i solved using function (array_key_exists())
if (array_key_exists('answerDate', $row)) {
$sql = "INSERT INTO counsel (counsel_id, big_cate, mid_cate, small_cate, title, question_date, question, answer_date, answer) VALUES ('" . $row["receiptNum"] . "', '" . $row["gooboon"] . "','" . $row["itemCode"] . "','" . $row["item"] . "','" . $row["title"] . "','" . $row["date"] . "','" . $row["question"] . "', '" . $row["answerDate"] . "','" . $row["answer"] . "')";
echo "answer exists!";
} else {
$sql = "INSERT INTO counsel (counsel_id, big_cate, mid_cate, small_cate, title, question_date, question) VALUES ('" . $row["receiptNum"] . "', '" . $row["gooboon"] . "','" . $row["itemCode"] . "','" . $row["item"] . "','" . $row["title"] . "','" . $row["date"] . "','" . $row["question"] . "')";
echo "answer doesn't exists!";
}
$conn->query($sql);