我有这个嵌套的json数据数组,我正在尝试将特定数据插入MYSQL数据库。但是我得到一个错误,我只是不知道我的代码有什么问题。抱歉还是php / mysql的新手。任何帮助表示赞赏
这是json数组:
[
{
"title": "★ (Blackstar)",
"artist": "David Bowie",
"year": "2016",
"genre": "Jazz",
"media": [
{
"totalDiscs": "1",
"position": "1",
"tracks": [
{
"title": "★ (Blackstar)",
"number": "1",
"artists": []
},
{
"title": "'Tis A Pity She Was A Whore",
"number": "2",
"artists": []
},
{
"title": "Lazarus",
"number": "3",
"artists": []
},
{
"title": "Sue (Or In A Season Of Crime)",
"number": "4",
"artists": []
},
{
"title": "Girl Loves Me",
"number": "5",
"artists": []
},
{
"title": "Dollar Days",
"number": "6",
"artists": []
},
{
"title": "I Can't Give Everything Away",
"number": "7",
"artists": []
}
]
}
],
"score": 1
}
]
这是我的代码:
$json = json_decode($result, true);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "4tracks";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
//echo "connected <br/>";
}
$sql = "INSERT INTO tracks (artist_name)
VALUES ('".$json[0]['artist']."')";
if (array_key_exists('genre',$json[0])){
$sql = "INSERT INTO tracks (track_genre)
VALUES ('".$json[0]['genre']."')";
}
foreach($json[0]['media'] as $key => $values){
foreach($values['tracks'] as $key1 => $values1) {
$sql .= "INSERT INTO tracks (track_name)
VALUES ('".$values1['title']."')";
}
}
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
这是我在wamp上运行.php时的输出:
错误:INSERT INTO曲目(artist_name)VALUES(&#39; David Bowie&#39;);插入 INTO跟踪(track_genre)VALUES(&#39; Jazz&#39;);
INSERT INTO track(track_name)VALUES(&#39;★(Blackstar)&#39;);
INSERT INTO track(track_name)VALUES(&#39;&#39;可惜她是A 妓女&#39);
INSERT INTO track(track_name)VALUES(&#39; Lazarus&#39;);
INSERT INTO track(track_name)VALUES(&#39;苏(或者在一个季节) 犯罪)&#39);
INSERT INTO track(track_name)VALUES(&#39; Girl Loves Me&#39;);
INSERT INTO track(track_name)VALUES(&#39; Dollar Days&#39;);
INSERT INTO track(track_name)VALUES(&#39;我不能给予一切 远&#39);
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近&#39; INSERT INTO曲目(track_genre)VALUES(&#39; Jazz&#39;); INSERT INTO 曲目(track_n&#39;在第2行---
答案 0 :(得分:0)
最明显的问题是您正在构建一个包含多个查询的字符串。虽然使用mysqli_
执行多个查询是不可能的,但不能使用->query()
方法完成,并且可以更加简单地独立执行每个查询。
当您在一个查询中同时将多个列同时插入表中时,您在tracks
表中每列编写一个查询。
然后你需要使用许多循环遍历你的JSONdata结构,foreach循环最适合这个目的。
您也使用参数化查询,"title": "'Tis A Pity She Was A Whore"
等字符串中的引号问题将自动为您处理。
所以我建议将其作为解决方案
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "4tracks";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
echo "Connection failed: " . $conn->connect_error;
exit;
}
$j = file_get_contents('tst.json');
$json = json_decode($j);
if (json_last_error() != 0) {
echo json_last_error_msg();
}
// Notice we prepare the query ONCE, but later execute it many times
// with different data in the parameters
$sql = "INSERT INTO tracks (artist_name, track_genre, track_name) VALUES (?,?,?)";
$stmt = $conn->prepare($sql);
// check the prepare worked, if not report errors and exit
if (! $stmt) {
echo $conn->error;
exit;
}
// bind the variables names to the ? place holders
// the variables at this point do not have to exists, or have data in them
$stmt->bind_param('sss', $artist, $genre, $title);
foreach($json as $cd) {
foreach($cd->media as $media) {
foreach($media->tracks as $track){
// load the bound variables with the data for this insert execution
$artist = $cd->artist;
$genre = $cd->genre;
$title = $track->title;
$result = $stmt->execute();
// check the insert worked, if not report error
if (!$result) {
echo $conn->error;
exit;
}
}
}
}
答案 1 :(得分:-1)
INSERT INTO tracks (track_name) VALUES (''Tis A Pity She Was A Whore');
&#39; Tis - 你需要逃避那个单引号。
$sql = "INSERT INTO tracks (artist_name)
VALUES ('". addslashes ($json[0]['artist']) ."');";
答案 2 :(得分:-1)
转义将确保MySQL不会解析任何特殊符号(如JSON中的&#34;符号)。
要转义查询,请使用$mysqli->real_escape_string($my_json);
。
总是逃避您尝试插入数据库的任何内容。甚至更好 - 使用参数化或准备好的陈述(阅读更多here)。