php - Json deserialize list of objects and save to DB -
im trying insert json format db.
{ "firstname": "brett" }
this format inserting properly.
but if change format below:
[ { "firstname": "brett" }, { "firstname": "john" } ]
for below script insert 1st record.
<?php $data = json_decode(file_get_contents('php://input'), true); /*server credentials*/ $arraykey=array_keys($data); $array=$data[$arraykey[0]]; try { //sql server execute foreach($data $array) { $count = $dbh->exec("insert table(firstname) values ('" . implode("', '", $array) . "')" ) or die(print_r($dbh->errorinfo(), true)); echo count($data); $dbh = null; echo 'data inserted!!<br />'; } } catch(pdoexception $e) { echo $e->getmessage(); }
how insert json records above script.
Comments
Post a Comment