简体   繁体   English

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

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

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? 我正在开发一个应用程序,它通过HttpConnection将JSON发送到PHP服务器,我的脚本是在插入MYSQL数据库之前对其进行接收和解码的脚本,尽管它的图像已转换为base64,并且我确定我做错了很多事情,特别是当我在foreach中放置查询时,实际上这现在无法正常工作,所以请您能帮我解决这个问题,也许能找到一种更好的性能表现方式吗?

I already have tested the application script to create JSON Object and it was fine. 我已经测试了应用程序脚本来创建JSON对象,这很好。 My problem is just with PHP JSON decoding and MYSQL inserting query (Mysql Script connection is also fine). 我的问题仅在于PHP JSON解码和MYSQL插入查询(Mysql Script连接也很好)。

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

This my JSON without images Base64 data. 这是我的JSON,没有图片Base64数据。 please include one with this site: http://www.base64-image.de/ just to test it. 请在此站点中包括一个: 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 is an array here with only 1 element "relatorio" . $relatorio是一个只有1个元素"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. 这就是我解码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);
?>

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. 我不是PHP开发人员,但我相信您必须将第二个参数传递给json_decode ,以便PHP将其视为数组而不是对象。 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. 您将根据所拥有的列在循环中进行构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM