【发布时间】:2021-12-17 10:57:00
【问题描述】:
我很难理解为什么在尝试将 JSON 文件数据插入新表时得到的结果。问题是一个JSON 文件可以正常工作并填充表格,而另一个JSON 文件则不能。我正在使用Xampp phpadmin,但我不知道为什么我的问题仍然存在。表的创建适用于任何JSON 文件,但数据的插入是主要问题。
php 文件:
include("dbCon.php");
$fname=$_POST['fname'];
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CREATE TABLE `".$fname."`(
id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
imgurl VARCHAR(255) NOT NULL,
content VARCHAR(20000) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "Table ".$fname." created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$json = file_get_contents('../jsonFIle/'.$fname.'.json');
$array = json_decode($json, true);
echo var_dump($fname);
foreach($array as $row) {
$sql = "INSERT INTO `".$fname."`(title, imgurl, content) VALUES('".$row["title"]."', '".$row["imgurl"]."', '".$row["content"]."')";
mysqli_query($conn, $sql);
}
echo var_dump($array);
$conn->close();
Json 文件: test.json
[
{
"title":"test1",
"imgurl":"test1",
"content":"test1"
},
{
"title":"test2",
"imgurl":"test2",
"content":"test2"
},
{
"title":"test3",
"imgurl":"test3",
"content":"test3"
}
]
Json 文件: newmainnews.json
[
{
"title":"NASA's record-breaking Lucy asteroid mission gearing up for October launch",
"imgurl":"record.jpg",
"content":"Lucy is scheduled to launch atop a United Launch Alliance Atlas V rocket from Florida's Cape Canaveral Space Force Station on Oct."
},
{
"title":"Mars on the cheap: Scientists working to revolutionize access to the Red Planet",
"imgurl":"mars.jpg",
"content":"Spotting Jupiter is a breeze this week for the naked eye as it reaches its biggest and brightest moment in the night sky. Telescope-hunters will also get a treat looking for moons and atmospheric bands. The gas giant planet will be at opposition today (Aug. 19), meaning it is directly opposite the sun in Earth's sky. Jupiter also makes its closest approach of the year to Earth during opposition. The planet will appear at magnitude -2.9, well within naked-eye range and outshining any star in Earth's sky except, of course, for the sun."
},
{
"title":"Jupiter's winds of change show increased storm speeds in Great Red Spot",
"imgurl":"jupiter.jpg",
"content":"The long-running telescope has been studying the Great Red Spot — a major storm on Jupiter — that is shrinking for mysterious reasons. Alongside that, researchers just uncovered huge changes in wind speeds within the massive storm.Jupiter takes 12 Earth years to orbit the sun. During the Jovian year between 2009 and 2020."
}
]
test.json 的 var_dump 回显:
array(3) { [0]=> array(3) { ["title"]=> string(5) "test1" ["imgurl"]=> string(5) "test1" ["content" ]=> string(5) "test1" } [1]=> array(3) { ["title"]=> string(5) "test2" ["imgurl"]=> string(5) "test2" [ "内容"]=> 字符串(5) "test2" } [2]=> 数组 (3) { ["title"]=> 字符串(5) "test3" ["imgurl"]=> 字符串(5) " test3" ["content"]=> 字符串(5) "test3" } }
newmainnews.json 的 var_dump 回显:
array(3) { [0]=> array(3) { ["title"]=> string(74) "NASA 破纪录的露西小行星任务准备 10 月发射" ["imgurl"]=> string(10) "record.jpg" ["content"]=> string(130) "Lucy 计划于 10 月从佛罗里达州卡纳维拉尔角太空部队站搭乘联合发射联盟 Atlas V 火箭发射。" } [1]=> array(3) { ["title"]=> string(79) "便宜的火星:科学家们致力于彻底改变进入红色星球" ["imgurl"]=> string(8) " mars.jpg" ["content"]=> string(539) "本周发现木星对肉眼来说是一件轻而易举的事,因为它在夜空中达到了它最大和最亮的时刻。望远镜猎人也将得到一种寻找的享受卫星和大气带。这颗气态巨行星将于今天(8 月 19 日)处于冲日状态,这意味着它与地球天空中的太阳直接相对。木星在冲日期间也将在今年最接近地球。这颗行星将出现在星等-2.9,远在肉眼范围内,比地球天空中的任何恒星都要亮,当然,太阳除外。” } [2]=> array(3) { ["title"]=> string(71) "木星的变化之风显示大红斑的风暴速度增加" ["imgurl"]=> string(11) "jupiter. jpg" ["content"]=> string(327) "长期运行的望远镜一直在研究大红斑——木星上的一场大风暴——由于神秘的原因正在缩小。除此之外,研究人员刚刚发现了风的巨大变化在大风暴中的速度。木星绕太阳运行需要 12 个地球年。在 2009 年至 2020 年的木星年期间。 } }
test.json 文件正确填充表,但 newmainnews.json 不插入任何内容。
我怀疑JSON 文件有问题。无论哪种方式,正如我之前所说,我完全一无所知,任何澄清或帮助将不胜感激。
【问题讨论】:
-
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough!
标签: php arrays json datatable xampp