这是我的第一个问题;而且我正在尝试学习。我的json看起来像这样:

    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"}]


和我的PHP页面看起来像这样:

<?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);
?>


我已经做了很多调试工作,但我要越野车了。我可以在日志中看到php页面正在访问我的数据库并尝试插入数据的位置;但这是服务器日志显示的内容:
    70.193.209.170--[14 / May / 2015:06:37:45 -0500]“ POST /webservice2/insertuser.php HTTP / 1.0” 200393“-”“-”

我什至可以从outsource.com雇用某人。长话短说;我在刚开始的地方就结束了,我损失了200美元。他基本上回来了,并说“问题出在您的php页上……”,尽管我的广告明确说“我需要PHP的帮助”,但那个家伙然后说他不了解PHP。

无论如何;寻找一些指导,任何帮助将不胜感激。在此先感谢您的时间和投入。

最佳答案

您已经对json进行了解码,因此它可以转换回普通数组,并且您正在尝试将关联数组用作对象。以通常的方式访问它,以访问二维关联数组。

请使用以下代码:

<?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);


?>

我希望这可以帮助你。

关于php - PHP不将JSON数据插入MySQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30251855/

10-11 02:43
查看更多