php - How to insert data from a json-object to a database -
i'm pretty new json, i'm trying insert data json database table. keep getting errors: notice: undefined index: subject notice: undefined index: message
<?php $jsondata = '{ "p1" : [ { "subject": "something", "message": "something" }, { "subject": "something111", "message": "something11" } ] }'; $data = json_decode($jsondata, true); $p1 = $data['p1']; $sql = "insert table(subject, message) values('".$p1['subject]."', '".$p1['message]."'"); $result = $mysqli->query($sql); ?>
there many mistakes in code.
1.$p1 multidimensional array treating array
2.you have forget comma(') before ] in query
3.you have close " @ wrong place in query should @ last after values bracket.
use code
$jsondata = '{ "p1" : [ { "subject": "something", "message": "something" }, { "subject": "something111", "message": "something11" } ] }'; $data = json_decode($jsondata, true); $p1 = $data['p1']; foreach($p1 $val) { $sql = "insert table(subject,message) values('".$val['subject']."', '".$val['message']."')"; $result = $mysqli->query($sql); }
Comments
Post a Comment