在数据库表中插入部分JSON数据

时间:2017-11-08 18:29:26

标签: php mysql json

我是学生PHP开发人员,我正在制作一个网络应用程序。 所以我在数据库表中插入JSON数据时遇到了问题。

这是我的 JSON 代码:

[
   {
      "HouseCode": "XX-00000-70",
      "MediaV2": [
         {
            "Type": "Photos",
            "TypeContents": [
               {
                  "SequenceNumber": 1,
                  "Tag": "ExteriorSummer",
                  "Versions": [
                     {
                        "Height": 1365,
                        "Width": 2048,
                        "URL": "http://image1.com/2048x1365"
                     },
                     {
                        "Height": 40,
                        "Width": 53,
                        "URL": "http://imagex.com/2048x1365"
                     }
                  ]
               },
               {
                  "SequenceNumber": 2,
                  "Tag": "ExteriorSummer",
                  "Versions": [
                     {
                        "Height": 1365,
                        "Width": 2048,
                        "URL": "http://image2.com/2048x1365"
                     },
                     {
                        "Height": 40,
                        "Width": 53,
                        "URL": "http://imagex.com/2048x1365"
                     }
                  ]
               }
            ]
         }
      ]
   },
   {
      "HouseCode": "XX-00000-71",
      "MediaV2": [
         {
            "Type": "Photos",
            "TypeContents": [
               {
                  "SequenceNumber": 1,
                  "Tag": "ExteriorSummer",
                  "Versions": [
                     {
                        "Height": 1365,
                        "Width": 2048,
                        "URL": "http://image1b/2048x1365"
                     },
                     {
                        "Height": 40,
                        "Width": 53,
                        "URL": "http://via.placeholder.com/53x40"
                     }
                  ]
               },
               {
                  "SequenceNumber": 2,
                  "Tag": "LivingRoom",
                  "Versions": [
                     {
                        "Height": 1365,
                        "Width": 2048,
                        "URL": "http://image2b/2048x1365"
                     },
                     {
                        "Height": 40,
                        "Width": 53,
                        "URL": "http://via.placeholder.com/53x40"
                     }
                  ]
               }
            ]
         }
      ]
   }
]

这是我的数据库表:

+-----------+--------+--------+
| HouseCode | Image1 | Image2 |
+-----------+--------+--------+
|           |        |        |
+-----------+--------+--------+
|           |        |        |
+-----------+--------+--------+

目的是将每个图像的格式:'高度:1365,宽度:2048'导入数据库。

喜欢这样:

+-------------+-----------------------------+-----------------------------+
| HouseCode   | Image1                      | Image2                      |
+-------------+-----------------------------+-----------------------------+
| XX-00000-70 | http://image1.com/2048x1365 | http://image2.com/2048x1365 |
+-------------+-----------------------------+-----------------------------+
| XX-00000-71 | http://image1b/2048x1365    | http://image2b/2048x1365    |
+-------------+-----------------------------+-----------------------------+

我试过了:

//read the json file contents
$jsondata = file_get_contents('import/json/MediaV2.json');

//convert json object to php associative array
$datas = json_decode($jsondata, true);

//Foreach loop
foreach ($datas as $data) {
    $housecode = $data['HouseCode'];
    if($data = (['MediaV2']['TypeContents']['Versions']['Height'] = '1365')){
        $pictures = $data['MediaV2']['TypeContents']['Versions']['URL'];
    }
    //insert into mysql table
    DB::insert("INSERT INTO mediav2(HouseCode, photo1, photo2)
    VALUES('$housecode', '$pictures', '$pictures')");
}

我做错了什么?谁可以帮助我?

提前致谢!

P.S。抱歉我的英文不好..

1 个答案:

答案 0 :(得分:1)

这是胡说八道:

if($data = (['MediaV2']['TypeContents']['Versions']['Height'] = '1365'))
//       ^- this is not how array access works                ^
//                         this is assignment, not comparison ┚

这是正确的方法:

if($data['MediaV2']['TypeContents']['Versions']['Height'] == '1365')