UPDATED2
我生成的txt文件(output.txt)看起来像这样,但是有不同数量的doorGroups数组
{"DGGroups"[{"DoorGroupID":"1","DoorGroupName":"Default","DoorGroupStatus":"Lock"}{"DoorGroupID":"2","DoorGroupName":"Group 1","DoorGroupStatus":"Unlock"}{"DoorGroupID":"3","DoorGroupName":"Group 2","DoorGroupStatus":"Lock"{"DoorGroupID":"4","DoorGroupName":"Group 3","DoorGroupStatus":"Unlock"}{"DoorGroupID":"5","DoorGroupName":"Group 4","DoorGroupStatus":"Lock"}]}
我需要能够翻录文件并将值插入到具有相应列的mysqli db中
id doorGroupID doorGroupName doorGroupStatus
这是我目前的非工作代码 - 我尝试了很多变化。我在字符串上剪掉一些东西,以便更好地使用它。调用jsondecode仍然返回NULL。我按下按钮操作。任何方向将不胜感激。
//Forms posted
if (isset($_POST['submit']))
{
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//read the file contents
$data = file_get_contents('output.txt');
var_dump($data);
echo "<br>";
echo "<br>";
$data = substr($data, 13, strlen($data) - 17); // remove outer ( and )
$data = str_replace('"', "", $data);
$data = str_replace('{', "", $data);
$data = str_replace('}', "|", $data);
var_dump($data);
echo "<br>";
echo "<br>";
$output = json_decode($data, true);
echo "<pre>";
var_dump($output);
$ret = array_map (
function ($_) {return explode (',', $_);},
explode ('|', $data)
);
print_r ($ret);
//insert into mysql table
$sql = "INSERT INTO doorGroups(doorGroupID, doorGroupName, doorGroupStatus)
VALUES('$DoorGroupID', '$DoorGroupName', '$DoorGroupStatus')";
if ($conn->query($sql) === TRUE) {
echo "<h3>updated successfully</h3>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<div class='container' >
<form method='post'>
<div class='container'>
<form class='form-horizontal'>
<div class='form-group'>
</div>
</form>
<button type='submit' name='submit' class='btn btn-primary center-block'>Do It</button>
</div>
</div>
</form>
</div>
</head>
</html>
文件输出到此
string(371) "{"DGGroups"[{"DoorGroupID":"1","DoorGroupName":"Default","DoorGroupStatus":"Lock"}{"DoorGroupID":"2","DoorGroupName":"Group 1","DoorGroupStatus":"Unlock"}{"DoorGroupID":"3","DoorGroupName":"Group 2","DoorGroupStatus":"Lock"}{"DoorGroupID":"4","DoorGroupName":"Group 3","DoorGroupStatus":"Unlock"}{"DoorGroupID":"5","DoorGroupName":"Group 4","DoorGroupStatus":"unLock"}]} "
string(290) "DoorGroupID:1,DoorGroupName:Default,DoorGroupStatus:Lock|DoorGroupID:2,DoorGroupName:Group 1,DoorGroupStatus:Unlock|DoorGroupID:3,DoorGroupName:Group 2,DoorGroupStatus:Lock|DoorGroupID:4,DoorGroupName:Group 3,DoorGroupStatus:Unlock|DoorGroupID:5,DoorGroupName:Group 4,DoorGroupStatus:unLock"
NULL
Array
(
[0] => Array
(
[0] => DoorGroupID:1
[1] => DoorGroupName:Default
[2] => DoorGroupStatus:Lock
)
[1] => Array
(
[0] => DoorGroupID:2
[1] => DoorGroupName:Group 1
[2] => DoorGroupStatus:Unlock
)
[2] => Array
(
[0] => DoorGroupID:3
[1] => DoorGroupName:Group 2
[2] => DoorGroupStatus:Lock
)
[3] => Array
(
[0] => DoorGroupID:4
[1] => DoorGroupName:Group 3
[2] => DoorGroupStatus:Unlock
)
[4] => Array
(
[0] => DoorGroupID:5
[1] => DoorGroupName:Group 4
[2] => DoorGroupStatus:unLock
)
)
updated successfully
所以基本上现在挂断是我可以让字符串爆炸到正确的组但我无法访问值对。它是一组价值对组。我如何分解它以便我在相同的组中获取信息并访问相应的信息?
答案 0 :(得分:1)
这是JSON的一个混蛋版本,但我认为它可以通过正则表达式进行提交。 (是的,我知道,现在我有两个问题......)
同样,您绝对不希望在没有安全措施的情况下将外部数据转储到数据库中。您可能认为它值得信赖,因为它来自您组织中的另一台服务器,但事实并非如此。准备好的语句可以为您提供保护,并且还可以大大减少将这些内容放入数据库所需的开销。
<?php
#set up the database stuff
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "INSERT INTO doorGroups(doorGroupID, doorGroupName, doorGroupStatus)
VALUES(?,?,?)";
$stmt = $conn->prepare($sql);
# the data; one missing } was added from the question
$data = '{"DGGroups"[{"DoorGroupID":"1","DoorGroupName":"Default","DoorGroupStatus":"Lock"}{"DoorGroupID":"2","DoorGroupName":"Group 1","DoorGroupStatus":"Unlock"}{"DoorGroupID":"3","DoorGroupName":"Group 2","DoorGroupStatus":"Lock"}{"DoorGroupID":"4","DoorGroupName":"Group 3","DoorGroupStatus":"Unlock"}{"DoorGroupID":"5","DoorGroupName":"Group 4","DoorGroupStatus":"Lock"}]}';
# grab the members of DGGroups
preg_match('/"DGGroups"\[({.*?}+)\]/', $data, $matches);
# split the members into individual elements
$doorgroups = explode("{", $matches[1]);
# get rid of the empty element at the beginning
array_shift($doorgroups);
# now work with each
foreach ($doorgroups as $dg) {
# decode it, don't forget preg_split stripped off the separator
$dg = json_decode("{" . $dg);
# bind the variables to the statement
$stmt->bind_param("iss", $dg->DoorGroupID, $dg->DoorGroupName, $dg->DoorGroupStatus);
# execute it
$stmt->execute();
}
当然,您要对mysqli::prepare()
,preg_match()
,json_decode()
等的结果进行错误检查。