android - php not inserting json data to mysql -
this first question; , novice trying learn. json looks this:
array(1) {["usersjson"]=>string(143) "[{"userid":"1","username":"emilio"},{"userid":"2","username":"andre"}, {"userid":"3","username":"kristina"}, {"userid":"4","username":"damaris"}]"}string(143) "[{"userid":"1","username":"emilio"},{"userid":"2","username":"andre"}, {"userid":"3","username":"kristina"},{"userid":"4","username":"damaris"}]" [{"id":"1","status":"no"},{"id":"2","status":"no"},{"id":"3","status":"no"}, {"id":"4","status":"no"}]
and php page looks this:
<?php include_once 'db_functions.php'; //create object db_functions class $db = new db_functions(); var_dump($_post); //get json posted android application $json = $_post["usersjson"]; error_log (var_dump($json)); //remove slashes if (get_magic_quotes_gpc()){ $json = stripslashes($json); } //decode json array $data = json_decode($json); //util arrays create response json $a=array(); $b=array(); for($i=0; $i<count($data) ; $i++) { //store user mysql db $res = $db->storeuser($data[$i]->userid,$data[$i]->username); //based on inserttion, create json response if($res){ $b["id"] = $data[$i]->userid; $b["status"] = 'yes'; array_push($a,$b); }else{ $b["id"] = $data[$i]->userid; $b["status"] = 'no'; array_push($a,$b); } } //post json response android application echo json_encode($a); ?>
i have done debuggin going buggy. can see in logs php page accessing database , attempting insert data; server log says: 70.193.209.170 - - [14/may/2015:06:37:45 -0500] "post /webservice2/insertuser.php http/1.0" 200 393 "-" "-"
i've gone far hiring outsource.com. long story short; ended right started , i'm down $200 bucks. came , said "the problem in php page..." , though ad explicitly said "i need php help" guy says doesn't know php.
anyhow; looking guidance , appreciated. in advance time , input.
you have decoded json converts normal array , trying use associative array object. access in normal manner access 2 dimensional associative array.
please use below code:
<?php include_once 'db_functions.php'; //create object db_functions class $db = new db_functions(); var_dump($_post); //get json posted android application $json = $_post["usersjson"]; error_log (var_dump($json)); //remove slashes if (get_magic_quotes_gpc()){ $json = stripslashes($json); } //decode json array $data = json_decode($json); //util arrays create response json $a=array(); $b=array(); for($i=0; $i<count($data) ; $i++) { //store user mysql db $res = $db->storeuser($data[$i]['userid'],$data[$i]['username']); //based on inserttion, create json response if($res){ $b["id"] = $data[$i]['userid']; $b["status"] = 'yes'; array_push($a,$b); }else{ $b["id"] = $data[$i]['userid']; $b["status"] = 'no'; array_push($a,$b); } } //post json response android application echo json_encode($a);
?>
i hope helps you.
Comments
Post a Comment