简体   繁体   English

为什么在解析JSON数据时一开始会得到0?

[英]why i am getting 0 at first, while parsing JSON data?

I am new to json, my aim is to maintain the history of specific columns(which are posted through $_POST in php) on every update in mysql using php. 我是JSON的新手,我的目标是维护使用php进行mysql每次更新时特定列的历史(通过php中的$ _POST发布)。 I took one json array for the history column and placed it in a while loop, after that I appended the variable which i want to merge with the previous one with array_merge() function. 我将一个json数组用于历史列,并将其放置在while循环中,之后,我使用array_merge()函数附加了要与上一个合并的变量。 I am getting the output but starting with 0. Let me know how to append the required fields in a proper json format and also how to retrieve the json data in a div tag. 我正在获取输出,但从0开始。让我知道如何以正确的json格式附加必填字段,以及如何在div标签中检索json数据。 Thanks in advance. 提前致谢。

PHP Code: PHP代码:

<?php
$query = mysqli_query($conn,"SELECT `history` FROM projects WHERE `no` = '$id'");
  $json_data = array();
     while ($js = mysqli_fetch_assoc($query)) 
     {
       $json_data[] = $js['history'];
       $j = $json_data;
     }
?>  

 <?php
 if(isset($_POST['submit'])){
  if(isset($_GET['id'])){
  $id = $_GET['id'];
  $assign = mysqli_real_escape_string($conn,$_POST['assign']);
  $end_date = mysqli_real_escape_string($conn,$_POST['end_date']);
  $comments = mysqli_real_escape_string($conn,$_POST['comments']);

  $end_date = [
      'assigned_to' => $assign,
      'end_date' => $end_date,
      'comments' => $comments
     ];
 $json = array_merge($j,$end_date);
 $js = json_encode($json);
$ins = mysqli_query($conn,"UPDATE `projects` SET `assigned_to`='$assign',`end_date`='$end_date',
 `status`='$status',`comments`='$comments'`history`= '$js'  WHERE 
`episode_no` = '$id'");
}
}
?>  

JSON data in MYSQL : MYSQL中的JSON数据:

{"0":"{"0":"{"0":"","assigned_to":"AAA","end_date":"2018-09-12","comments":"happy"}",
        "assigned_to":"AAA","end_date":"2018-09-12","comments":"jolly"}",
        "assigned_to":"AAA","end_date":"2018-09-12","comments":"xvbcvbdfghdfg"} 

First of all, the answer to your question: you are loading an array of strings in $j , so the array_merge function won't work as expected: 首先,您的问题的答案:您正在$j中加载字符串数组,因此array_merge函数将无法按预期工作:

$j[0] = 'some JSON string from DB';

$json = array_merge($j, $end_date);

the array_merge finds that the second argument is a sparse array, so it merges the keys as strings: array_merge发现第二个参数是一个稀疏数组,因此它将键合并为字符串:

$json = [
  '0' => 'the previous string',
  'assigned_to' => ...
]

For your idea to work you probably need to store the new history item by appending to the array: 为了使您的想法可行,您可能需要通过追加到数组来存储新的历史记录项:

$j[] = $end_date;
$js = json_encode($j);
...

This would solve your issue. 这样可以解决您的问题。

But there is a very major issue here that you need to solve first. 但是这里有一个非常重要的问题,您需要首先解决。 It's a OMG-like WTF-like issue. 这是一个类似OMG的WTF发行版。 You are getting $id from user input (query parameters) and sending it to the DB without any fear. 您将从用户输入(查询参数)中获取$id并将其发送到数据库,而无需担心。 Suppose that the user sends 假设用户发送

https://your.server/some/path?id=';TRUNCATE TABLE projects --'

(propery url-encoded of course). (当然,URL编码正确)。 Now you are sending this to the database: 现在,您将其发送到数据库:

SELECT `history` FROM projects WHERE `no` = '';TRUNCATE TABLE projects --''

Bye bye projects. 再见项目。 A user can do whatever to your database, change passwords, reassign foreign keys, set himself as administrator. 用户可以对数据库执行任何操作,更改密码,重新分配外键,将自己设置为管理员。

Please for the sake of whatever you believe in, use a proper ORM and never pass user input to the DB!!! 为了您的信任,请使用正确的ORM, 切勿将用户输入传递给数据库!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM