如何从Android应用程序解码JSON并使用PHP将多行插入MYSQL

I am developing an app which sends a JSON by HttpConnection to a PHP server where is my script which receives and decodes it before inserts to a MYSQL database, although it has images converted to base64 and i am sure i'm doing many things wrong, specially when i put a query inside a foreach, in fact this is not working right now, so please could you help me to solve that and perhaps get a better way to performace?

I already have tested the application script to create JSON Object and it was fine. My problem is just with PHP JSON decoding and MYSQL inserting query (Mysql Script connection is also fine).

Thank you for helping me.

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("#(/\*([^*]|[
]|(\*+([^*/]|[
])))*\*+/)|([\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
}

This my JSON without images Base64 data. please include one with this site: http://www.base64-image.de/ just to test it.

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

I'm not a PHP developer, but I believe you have to pass a second argument to json_decode in order for PHP to treat it as an array instead of an object. Once you have that, what you need to do is loop the array and build a string to insert once in the database rather than doing multiple inserts, it would look like this:

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

That's what a multi-record insert looks like. You would build that in your loop according to the columns that you have.

$relatorio = json_decode($relatorio, true); //true makes the array associative

$relatorio is an array here with only 1 element "relatorio". You need to access that element and then try looping

$arr = $relatorio["relatorio"];

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

This is how i decoded the json string.

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