Ich möchte meine JSON-Daten zu MySQL DB einfügen PHP ...Insert JSON in mehrere Array-Daten in MySQL mit PHP
Diese meine JSON-Daten genannt ist "data.json"
{
"allRoundData": [{
"name": "Animals",
"timeLimitInSeconds": 20,
"pointsAddedForCorrectAnswer": 10,
"questions": [{
"questionText": "Lions are carnivores: true or false?",
"answers": [{
"answerText": "True",
"isCorrect": true
}, {
"answerText": "False",
"isCorrect": false
}]
}, {
"questionText": "What do frogs eat?",
"answers": [{
"answerText": "Pizza",
"isCorrect": false
}, {
"answerText": "Flies",
"isCorrect": true
}]
}, {
"questionText": "Where do mice live?",
"answers": [{
"answerText": "In the sea",
"isCorrect": false
}, {
"answerText": "On the moon",
"isCorrect": false
}, {
"answerText": "On land",
"isCorrect": true
}, {
"answerText": "In a tree",
"isCorrect": false
}]
}]
}]
}
Das ist mein PHP-Skript
<?php
include 'bl_Common.php';
$con = dbConnect();
// use prepare statement for insert query
$st = mysqli_prepare($con, 'INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES (?, ?, ?, ?, ?, ?)');
// bind variables to insert query params
mysqli_stmt_bind_param($st, 'siisss', $name, $time, $points, $question, $answer, $isCorrect);
// read json file
$filename = 'data.json';
$json = file_get_contents($filename);
echo $json;
//convert json object to php associative array
$data = json_decode($json, true);
// loop through the array
foreach ($data as $row) {
// get the employee details
$name = $row["allRoundData"]["name"];
$time = $row['allRoundData']['timeLimitInSeconds'];
$points = $row['allRoundData']['pointsAddedForCorrectAnswer'];
$question = $row['allRoundData']['questions']['questionText'];
$answer = $row['allRoundData']['answers']['answerText'];
$isCorrect = $row['allRoundData']['answers']['isCorrect'];
// execute insert query
mysqli_stmt_execute($st);
}
//close connection
mysqli_close($con);
?>
Ich habe einen Fehler gefunden, der besagt "Hinweis: Undefinierter Index: allRoundData in ..." Aber es erscheint dort.
echo $ name = $ row ["name"]; versuchen Sie diese – JYoThI