如何解码来自Android应用程序的JSON,并使用PHP将多行插入MYSQL


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

我正在开发一个应用程序,它通过HttpConnection将JSON发送到PHP服务器,这是我的脚本,它在插入MYSQL数据库之前接收并解码JSON,尽管它的图像已转换为base64,我确信我做错了很多事情,特别是当我将查询放入foreach时,事实上这现在不起作用,所以请你帮我解决这个问题,也许能找到更好的表演方式吗?

我已经测试了创建JSON对象的应用程序脚本,它很好。我的问题只是PHP JSON解码和MYSQL插入查询(MYSQL脚本连接也很好(。

谢谢你帮我。

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

这里,CCD_ 1是仅具有1个元素CCD_ 2的阵列。您需要访问该元素,然后尝试循环

$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...)

这就是多记录插入的样子。您可以根据所拥有的列在循环中构建它。