簡體   English   中英

如何使用Android從Android應用程序解碼JSON並將多行插入MYSQL

[英]How to decode a JSON from Android application and insert multiple rows into MYSQL using PHP

我正在開發一個應用程序,它通過HttpConnection將JSON發送到PHP服務器,我的腳本是在插入MYSQL數據庫之前對其進行接收和解碼的腳本,盡管它的圖像已轉換為base64,並且我確定我做錯了很多事情,特別是當我在foreach中放置查詢時,實際上這現在無法正常工作,所以請您能幫我解決這個問題,也許能找到一種更好的性能表現方式嗎?

我已經測試了應用程序腳本來創建JSON對象,這很好。 我的問題僅在於PHP JSON解碼和MYSQL插入查詢(Mysql Script連接也很好)。

感謝你們對我的幫助。

require('mysqli.php');
if(strcmp('send-json', $_POST['method']) == 0){
$MySQLi = new MySQLi($MySQL['servidor'], $MySQL['usuario'], $MySQL['senha'], $MySQL['banco']);
$MySQLi->set_charset('utf8');

$relatorio = utf8_encode($_POST['json']);
$relatorio = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $relatorio);
$relatorio = json_decode($relatorio);
$i = 0;
foreach ( $relatorio as $r ){
    if($r->{'img-antes'} != ""){
        $binary = base64_decode($r->{'img-antes'});
        $file_antes = fopen('img/'.$r->{'id'}.'_FOTO_ANTES.jpg','wb');
        fwrite($file_antes, $binary);
        fclose($file_antes);
        $url_antes="img/".$r->{'id'}."_FOTO_ANTES.jpg";
    }else{
        $url_antes="";
        }
    if($r->{'img-depois'} != ""){
        $binary = base64_decode($r->{'img-depois'});
        $file_depois = fopen('img/'.$r->{'id'}.'_FOTO_DEPOIS.jpg','wb');
        fwrite($file_depois, $binary);
        fclose($file_depois);
        $url_depois="img/".$r->{'id'}."_FOTO_DEPOIS.jpg";
    }else{
        $url_depois="";
        }
    $insert = "INSERT INTO Relatorio (id,Latitude,Longitude,URL_Antes,URL_Depois) 
    VALUES ('".$r->{'id'}."',
    '".$r->{'Latitude'}."',
    '".$r->{'Latitude'}."',
    '".$url_antes."',
    '".$url_depois."')";

    $send = $MySQLi->query($insert) OR trigger_error($MySQLi->error, E_USER_ERROR);
    if($send){$i++;}
    //$send->free();
}//fim do LOOP
if($i > 0){
    echo '1';//This a Good Answer to send back to my Application
    }
else{
    echo '2';//This a Bad Answer to send back to my Application
}

這是我的JSON,沒有圖片Base64數據。 請在此站點中包括一個: http : //www.base64-image.de/只是為了對其進行測試。

{  
"relatorio":[  
 {  
 "id":"F001EVLA366666LO129999",
 "Longitude":"21.61312634634566",
 "Latitude":"36.6623457906766",
 "img-antes":"please include an imageBASE64 here",
 "img-depois":""
   }
 {  
 "id":"F001EVLA468888LO129888",
 "Longitude":"55.65623213165487",
 "Latitude":"23.95626265922322",
 "img-antes":"please include an imageBASE64 here",
 "img-depois":"please include an imageBASE64 here"
   }
]
}
$relatorio = json_decode($relatorio, true); //true makes the array associative

$relatorio是一個只有1個元素"relatorio"的數組。 您需要訪問該元素,然后嘗試循環

$arr = $relatorio["relatorio"];

foreach ( $arr as $r ){...}

這就是我解碼json字符串的方式。

<?php
require "init.php";

$branches = $_POST["json"];

// Sample json Data
/*{
    "branchdata":[
        {"branch_name":"Computer Engineering","branch_code":"CE"},
        {"branch_name":"Information Technology","branch_code":"IT"},
        {"branch_name":"Electronics & Communication","branch_code":"EC"},
        {"branch_name":"Electrical Engineering","branch_code":"EE"},
        {"branch_name":"Bio Medical Engineering","branch_code":"BM"},
        {"branch_name":"Mechanical Engineering","branch_code":"ME"}
        ]
};*/


$array = json_decode($branches, true);

foreach ($array['branchdata'] as $item)
{
    $branch_name =  $item['branch_name'];
    $branch_code =  $item['branch_code'];

    mysqli_query($connection,"INSERT INTO `departments` VALUES ('$branch_name', '$branch_code')");
}
mysqli_close($connection);
?>

我不是PHP開發人員,但我相信您必須將第二個參數傳遞給json_decode ,以便PHP將其視為數組而不是對象。 一旦有了這些,您需要做的是循環數組並構建一個字符串以在數據庫中插入一次,而不是進行多次插入,它看起來像這樣:

INSERT INTO my_table(col1,col2,col3...)
VALUES
    (val1,val2,val3...),
    (val1,val2,val3...),
    (val1,val2,val3...)

這就是多記錄插入的樣子。 您將根據所擁有的列在循環中進行構建。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM