使用php解析数据并将值插入mysql数据库?

时间:2015-12-02 07:34:59

标签: php mysql json

  

用于检查json数据是否来自android代码的Php代码。

<?php
require "init.php";
if($_POST){
 print_r($_POST);
}else{
 echo "Nothing came from android code";
}
?>

作为回应,我得到的json数据如下: -

  

(   [jsonarray] =&gt; [{&#34; custInfo&#34;:&#34; Ujwal 9975022560&#34;,&#34; rate&#34;:&#34; 24000&#34;,&#34; weight&#34;:&# 34; 21.00000&#34;,&#34; desc&#34;:&#34; GENT ANGTHI 22k NO STONE&#34;,&#34; makingAmt&#34;:&#34; 200&#34;,&# 34; sum_total&#34;:&#34; RS.156283.38&#34;&#34;缸&#34;:&#34; RS.3064.38&#34;&#34; itemTotal&#34;:& #34; 51073&#34;&#34;条形码&#34;:&#34; BQSP78BB&#34;&#34; net_rate&#34;:&#34; 24200&#34;&#34;日期&# 34;:&#34; 2015年12月2日&#34;&#34; invoiceNo&#34;:&#34; 1&#34;&#34; bill_type&#34;:&#34;发票&#34 ;},{&#34; custInfo&#34;:&#34; Ujwal 9975022560&#34;,&#34; rate&#34;:&#34; 24000&#34;,&#34; weight&#34;: &#34; 21.00000&#34;,&#34; desc&#34;:&#34; GENT ANGTHI 22k NO STONE&#34;,&#34; makingAmt&#34;:&#34; 200&#34;, &#34; sum_total&#34;:&#34; RS.156283.38&#34;&#34;缸&#34;:&#34; RS.3064.38&#34;&#34; itemTotal&#34; :&#34; 51073&#34;&#34;条形码&#34;:&#34; BQSP78BB&#34;&#34; net_rate&#34;:&#34; 24200&#34;&#34;日期&#34;:&#34; 2015年12月2日&#34;&#34; invoiceNo&#34;:&#34; 1&#34;&#34; bill_type&#34;:&#34;发票& #34;},{&#34; custInf o&#34;:&#34; Ujwal 9975022560&#34;,&#34; rate&#34;:&#34; 24000&#34;,&#34; weight&#34;:&#34; 21.00000&#34 ;,&#34; desc&#34;:&#34; GENT ANGTHI 22k NO STONE&#34;,&#34; makingAmt&#34;:&#34; 200&#34;,&#34; sum_total&#34; :&#34; RS.156283.38&#34;&#34;缸&#34;:&#34; RS.3064.38&#34;&#34; itemTotal&#34;:&#34; 51073&#34 ;,&#34;条形码&#34;:&#34; BQSP78BB&#34;&#34; net_rate&#34;:&#34; 24200&#34;&#34;日期&#34;:&#34 ; 2015年12月2日&#34;&#34; invoiceNo&#34;:&#34; 1&#34;&#34; bill_type&#34;:&#34;发票&#34;}]   )

我想要做的是解析这个响应并将数据插入到mysql数据库中。我想是这样的。

foreach ( $data as $inv ) {
    $custInfo = $inv->custInfo;
    $rate =     $inv->rate;
    $weight=    $inv->weight;
    $desc=      $inv->desc;
    $makingAmt= $inv->makingAmt;
    $vat=       $inv->vat;
    $itemTotal= $inv->itemTotal;
    $sum_total= $inv->sum_total;
    $barcode=   $inv->barcode;
    $net_rate=  $inv->net_rate;
    $date=      $inv->date;
    $invoiceNo= $inv->invoiceNo;
    $bill_type= $inv->bill_type;
    $sql = "INSERT INTO selected_items 
             (custInfo, invoiceNo, barcode, desc, 
              weight, rate, makingAmt,net_rate,
              itemTotal,vat,sum_total,bill_type,date) 
            VALUES
             ('$custInfo','$invoiceNo','$barcode','$desc',
              '$weight','$rate','$makingAmt','$net_rate',
              '$itemTotal','$vat','$sum_total','$bill_type','$date')";
    $res = mysqli_query($sql,$con);

我是php的新手,所以我尝试了json_decode,但它在响应中返回一个空字符串。如何解析并将custInfo, invoiceNo, barcode, desc,weight, rate, makingAmt,net_rate,itemTotal,vat,sum_total,bill_type,date插入到我的表selected_items.

Thak you :))

3 个答案:

答案 0 :(得分:1)

试试这个(更新):

$arr = json_decode(stripslashes($_POST['jsonarray']), true);
$env = $arr['jsonarray'];

答案 1 :(得分:1)

$myArray = json_decode($data, true);


 $sql = "INSERT INTO selected_items 
             (custInfo, invoiceNo, barcode) 
            VALUES
             ('$myArray['custInfo'],$myArray['invoiceNo'],$myArray['barcode'])";
    $res = mysqli_query($sql,$con);

答案 2 :(得分:1)

编辑:添加了一些错误检查以帮助调试代码。

您可以尝试使用json_decode

将其直接解码为数组

然后你可以使用extract() 将变量导入数组的当前符号表,这会消除 foreach 循环。

<?php
    require "init.php";
    if($_POST){
        $data = $_POST;
        $array = json_decode($data, true);

        if($array===NULL){return print_r('json decode failed!');}
        if(!is_array($array)){return print_r('Well, the decoded data is corrupt, its supposed to be an array, '.gettype($array).' given!');}

        if(!isset($array['jsonarray'])){return print_r('no json data was ever received!');}
        extract($array['jsonarray']);

        $sql = "INSERT INTO selected_items 
         (custInfo, invoiceNo, barcode, desc, 
          weight, rate, makingAmt,net_rate,
          itemTotal,vat,sum_total,bill_type,date) 
        VALUES
         ('$custInfo','$invoiceNo','$barcode','$desc',
          '$weight','$rate','$makingAmt','$net_rate',
          '$itemTotal','$vat','$sum_total','$bill_type','$date')";
$res = mysqli_query($sql,$con);

    }else{
        echo "Nothing came from android code";
    }
?>