【问题标题】:How to decode a JSON from Android application and insert multiple rows into MYSQL using PHP如何从 Android 应用程序解码 JSON 并使用 PHP 将多行插入 MYSQL
【发布时间】:2015-08-11 13:48:28
【问题描述】:

我正在开发一个应用程序,它通过 HttpConnection 将 JSON 发送到 PHP 服务器,我的脚本在插入到 MYSQL 数据库之前接收并解码它,尽管它已将图像转换为 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"
   }
]
}

【问题讨论】:

  • 不要使用 utf 编码和 preg_replace 破坏 json。您可能会引入语法错误,使 json 无效并且解码失败。你很容易受到sql injection attacks

标签: php android mysql json base64


【解决方案1】:
$relatorio = json_decode($relatorio, true); //true makes the array associative

$relatorio 是一个数组,这里只有一个元素 "relatorio"。您需要访问该元素,然后尝试循环

$arr = $relatorio["relatorio"];

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

【讨论】:

    【解决方案2】:

    这就是我解码 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);
    ?>
    

    【讨论】:

      【解决方案3】:

      我不是 PHP 开发人员,但我相信您必须将第二个参数传递给 json_decode,以便 PHP 将其视为数组而不是对象。一旦你有了它,你需要做的是循环数组并构建一个字符串以在数据库中插入一次,而不是多次插入,它看起来像这样:

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

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

      【讨论】:

      • 在这种情况下,json 是一个 json 对象而不是一个数组
      • 它在技术上是 php 中的一个数组,但 json 本身是一个对象,因此问题是他们试图循环 1 个条目而不是 relatorio 下的条目
      猜你喜欢
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      相关资源
      最近更新 更多