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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -