dtm37893 2015-01-27 06:32
浏览 33
已采纳

将JSON解码值插入表中

This is my code of json decode and insert into the table after decoding. I am not getting how to insert implode values into table and nested json values .

include 'config.php'; /* datdabase connection*/
    class User {
public function update()
{
    $jsondata = file_get_contents('data.txt');
    $data = json_decode($jsondata, true);
$name=array();
    foreach($data as $item)
    {
$name[]=$item['v'];

        }
$val=implode(',',$name);
        echo"$val";
        $sql= mysql_query("INSERT INTO incident_main(inc_patientName,inc_patientAge,inc_patientGender,inc_patientMobile,inc_patientAddress,inc_patientWard,inc_type)  VALUES('..........') )");
        if(!$sql)
        {
            echo "Failed to insert the data into table";

    }
}}

$obj=new User(); 
$obj->update()

This is my data.txt file. Here "n" is name of table column and "v" is value which is to be inserted.

{
    "user_id": "user123",
    "time_stamp": "epoch",
    "lat": 52.25,
    "lon": 85.45,
    "mainreport":    [{
         "n": "date_of_diagnosis",
         "v": "epoch"
        },
        {
         "n": "name",
         "v": "abc"
        },
        {
         "n": "age",
         "v": 20
        },
        {
         "n": "gender",
         "v": "male"
        },
        {
         "n": "address",
         "v": "some address"
        },
        {
         "n": "location_gis_Stamp",
         "v": "word-1"
        },
        {
         "n": "mobile_number",
         "v": "some number"
        },
        {
         "n": "type_of_application",
         "v": "other",
         "nested": [{
             "n": "other",
             "v": "reason"
         }]}]}
  • 写回答

3条回答 默认 最新

  • douwo6738 2015-01-27 06:37
    关注

    You have following errors in your query

    1. Proper space between table name and fields incident_main(inc_patientName,inc_patientAge,........
    2. You used an extra ) near values in your query. inc_treatment,inc_untraced) VALUES('..........') )"
    3. Enclose the database value with 'value', it will be fine if you have only integer value so you don't need to add ''.

    Try like this:

    foreach($data as $item)
    {
        $name[]=$item['v'];
    
        // Check and get Nested Values
        if( isset($item['nested']) && is_array($item['nested']) ) {
            foreach($item['nested'] as $val) {
                $name[] = $val['v'];
            }
        }
    
    }
    
    
    $val=implode("','",$name);
    // epoch', 'abc', '20', 'male', 'some address', 'word-1', 'some number', 'other', 'reason
    $sql= mysql_query("INSERT INTO incident_main (inc_patientName,inc_patientAge,inc_patientGender,inc_patientMobile,inc_patientAddress,inc_patientWard,inc_type,inc_diagnosis,inc_recurrence,inc_hospitalized,inc_treatment,inc_untraced) 
            VALUES('".$val."') ");
    

    Please use mysqli or PDO instead of mysql_*

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了