繁体   English   中英

PHP不将JSON数据插入MySQL

[英]php not inserting json data to mysql

这是我的第一个问题; 而且我是尝试学习的新手。 我的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);

?>

我希望这可以帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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