【问题标题】:php not inserting json data to mysqlphp没有将json数据插入mysql
【发布时间】:2015-05-15 05:05:44
【问题描述】:

这是我的第一个问题;我是一个试图学习的新手。我的 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" 200 393 "-" "-"

我什至从 outsource.com 雇佣了一个人。长话短说;我最终在我开始的地方结束了,我减少了 200 美元。他基本上回来说“问题出在你的 php 页面...”,尽管我的广告明确表示“我需要 PHP 帮助”,但他却说他不懂 PHP。

无论如何;寻求一些指导和任何帮助将不胜感激。提前感谢您的时间和投入。

【问题讨论】:

  • 你可以加我Skype我在线sinan.artun

标签: php android mysql sqlite


【解决方案1】:

您已将 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);

?>

希望对你有帮助。

【讨论】:

  • 嗯,取得了一些进展!非常感谢回答的两位。我还在同一条船上;但至少我收到了这条消息:“发生错误 [服务器的 JSON 响应可能无效]!”
  • @dgore,如果你被困在任何地方,我想知道你是否想获得更多帮助。
  • 嗯....我想我正在取得更大的进步。查看我的 logcat(顺便说一句,我使用的是 eclipse luna)后,发送的数据字符串与外包人员所说的不匹配。 logcat 看起来像这样:
  • 05-15 02:10:22.635: I/System.out(9226): ["usersJSON"]=> 05-15 02:10:22.635: I/System.out(9226) : string(67) "[{"userName":"dede","userId":"1"},{"userName":"gggg","userId":"2"}]" 05-15 02:10 :22.635: I/System.out(9226): } 05-15 02:10:22.635: I/System.out(9226): string(67) "[{"userName":"dede","userId": "1"},{"userName":"gggg","userId":"2"}]" 05-15 02:10:22.635: I/System.out(9226): [{"id":"1 ","status":"no"},{"id":"2","status":"no"}]
  • 那太好了@Sourabh
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
相关资源
最近更新 更多