json到mysql只插入最后一条记录

时间:2017-10-17 19:53:00

标签: php mysql json foreach

我正在尝试将JSON插入MYSQL数据库,我已经成功访问​​了json的每个元素,但是我的代码只插入了json的最后一条记录,这里是我的PHP代码:

<?php

$link = mysqli_connect("localhost", "root", "", "Store");

if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

  $filename = "employee_data.json";
 $json = file_get_contents($filename);
 $obj = json_decode($json,true);


    foreach($obj as $item) {
$sql = "INSERT INTO product (barcodeNumber,name,catName) VALUES ('".$item['name']."', '".$item['designation']."', '".$item['gender']."') ";

echo $item['name'];
    }

if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

mysqli_close($link);
?>

正如你在我的代码中看到的,我可以打印json的每个名字,但它只插入最后一个!

和json文件 employee_data.json:

[  
   {  
     "name": "Michael Bruce",  
     "gender": "Male",  
     "designation": "System Architect"  
   },  
   {  
     "name": "Jennifer Winters",  
     "gender": "Female",  
     "designation": "Senior Programmer"  
   },  
   {  
     "name": "Donna Fox",  
     "gender": "Female",  
     "designation": "Office Manager"  
   },  
   {  
     "name": "Howard Hatfield",  
     "gender": "Male",  
     "designation": "Customer Support"  
   },  
   {  
     "name": "Kevin Sanders",  
     "gender": "Male",  
     "designation": "Food service worker"  
   },
   {  
     "name": "Fay K. Whitney",  
     "gender": "Female",  
     "designation": "Bookbinder"  
   },
   {  
     "name": "Heather Hernandez",  
     "gender": "Female",  
     "designation": "Corporate recruiter" 

   },
   {  
     "name": "Rhonda Okelley",  
     "gender": "Female",  
     "designation": "Image designer"  
   },
   {  
     "name": "Sandy Jacobsen",  
     "gender": "Male",  
     "designation": "Publicity agent"  
   },
   {  
     "name": "Marisa Williams",  
     "gender": "Female",  
     "designation": "Molder"  
   },
   {  
     "name": "Neil Simmons",  
     "gender": "Male",  
     "designation": "Engine and other machine assembler"  
   },
   {  
     "name": "Raymond Lara",  
     "gender": "Male",  
     "designation": "Clerical assistant"  
   },
   {  
     "name": "William Carpenter",  
     "gender": "Male",  
     "designation": "Broker"  
   },
   {  
     "name": "Ted Baer",  
     "gender": "Male",  
     "designation": "Conservation worker"  
   }
 ]

它只插入最后一条记录:

 {  
         "name": "Ted Baer",  
         "gender": "Male",  
         "designation": "Conservation worker"  
       }

任何帮助?

1 个答案:

答案 0 :(得分:0)

因为您在循环后插入记录。试试这段代码:

foreach($obj as $item) {
    $sql = "INSERT INTO product (barcodeNumber,name,catName) VALUES ('".$item['name']."', '".$item['designation']."', '".$item['gender']."') ";

    if(mysqli_query($link, $sql)){
        echo "Records added successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}

<强>更新

更好的方法是将每个记录插入到lop use bulk insert中:

$query = "INSERT INTO product (barcodeNumber,name,catName) VALUES "

foreach($obj as $item) {
    $query_values[] = "('".$item["name"]."', '".$item["designation"]."', '".$item["gender"]."')";
}

// Implode the query values array with a coma and execute the query.
$db->query($query . implode(',',$query_values));