簡體   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