简体   繁体   English

json_decode function php://input return Invalid JSON 格式

[英]json_decode function php://input return Invalid JSON Format

A new Datatype Introduced in mysql which is JSON. mysql 中引入了一种新的数据类型,即 JSON。

文本

now when I get Raw data from header:现在,当我从 header 获取原始数据时:

$_data = json_decode(file_get_contents("php://input"),true);

Result of $data $data的结果

Array
(
[lines] => Array
    (
        [0] => Array
            (
                [id] => 8
                [name] => Panadol Extra
                [qty] => 1
                [price] => 11.00
                [total] => 11.00
            )

        [1] => Array
            (
                [id] => 6
                [name] => Panadol Simple
                [qty] => 1
                [price] => 5.00
                [total] => 5.00
            )

        [2] => Array
            (
                [id] => 2
                [name] => Panadol
                [qty] => 1
                [price] => 1.00
                [total] => 1.00
            )

        [3] => Array
            (
                [id] => 1
                [name] => Panadol Extra
                [qty] => 1
                [price] => 1.00
                [total] => 1.00
            )

    )

[discount] => 0
[date] => 2019-11-22T16:31:21+05:00
[paid] => 18
[user_id] => 1
[serial] => 35

)

am getting this error when i store data in mysql当我将数据存储在 mysql 中时出现此错误

Invalid Query:Invalid JSON text.无效查询:无效的 JSON 文本。 "Invalid value." “无效值。” at position 0 in value for column 'sales.Invoice'. 'sales.Invoice' 列的值在 position 0 处。

here is my query:这是我的查询:

require_once 'config/config.php';
$_json = json_decode(file_get_contents("php://input"),true);

echo "<pre>";
print_r($_json);
exit();

$query = "INSERT INTO `sales` 
(`UserId`,`invoice`,`Discount`,`Paid`,`Serial`) 
Values('','','','','');";

$result = $db->query($query);
if ($result)
echo "Data Inserted Successfully!";
else
echo "Invalid Query!".$db->error;

i need to store discount,paid,user_id,serial and lines(json format) into table.help me to store that data into sales.我需要将折扣、付费、用户 ID、序列号和行(json 格式)存储到表中。帮助我将这些数据存储到销售中。 how do i convert lines into json format.如何将lines转换为 json 格式。 i can store other object discount paid serial using extract function but unbale to convert lines data into json format我可以使用提取 function 存储其他 object 折扣付费序列,但无法将lines数据转换为 json 格式

You are decoding the JSON so it's creating a problem.您正在解码 JSON 所以它会产生问题。 Don't decode it directly store the JSON variable不要解码直接存储JSON变量

Here is the Solution i found.这是我找到的解决方案。

$_json = json_decode(file_get_contents("php://input"),true);
extract($_json);

$invoice = json_encode($lines);

$query = "INSERT INTO `sales` (`UserId`,`invoice`,`Discount`,`Paid`,`Serial`) 
Values('$user_id','$invoice','$discount','$paid','$serial');";

$result = $db->query($query);

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

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