简体   繁体   中英

php not inserting json data to mysql

this is my first question; and I am a NOVICE trying to learn. My json looks like 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 my php page looks like this:

<?php
include_once 'db_functions.php';
//Create Object for DB_Functions class 
$db = new DB_Functions(); 
var_dump($_POST);
//Get JSON posted by Android Application
$json = $_POST["usersJSON"];
error_log (var_dump($json));
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json); 
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
for($i=0; $i<count($data) ; $i++)
{
//Store User into 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 back to Android Application
echo json_encode($a);
?>

I have done so much debuggin I am going buggy. I can see in the logs where the php page is accessing my database and attempting to insert data; but this is what the 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 even gone as far as hiring someone from outsource.com. Long story short; I ended up right where I started and I'm down $200 bucks. He basically came back and said "the problem is in your php page..." and even though my ad explicitly said "I need PHP help" the guy then says he doesn't know PHP.

Anyhow; looking for some guidance and any help would be appreciated. Thanks in advance for your time and input.

You have decoded the json so it converts back to a normal array and you are trying to use an associative array as an object. Access it in the normal manner that you normally do to access 2 dimensional associative array.

Please use the below code:

<?php
include_once 'db_functions.php';
//Create Object for DB_Functions class 
$db = new DB_Functions(); 
var_dump($_POST);
//Get JSON posted by Android Application
$json = $_POST["usersJSON"];
error_log (var_dump($json));
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json); 
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
for($i=0; $i<count($data) ; $i++)
{
//Store User into 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 back to Android Application
echo json_encode($a);

?>

I hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM