簡體   English   中英

使用php解析數據並將值插入mysql數據庫?

[英]Parsing data and inserting values into mysql database using php?

php代碼檢查json數據是否來自android代碼。

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

作為響應,我正在獲取json數據,如下所示:

([jsonarray] => [{“ custInfo”:“ Ujwal 9975022560”,“ rate”:“ 24000”,“ weight”:“ 21.00000”,“ desc”:“ GENTS ANGTHI 22k NO STONE”,“ makingAmt”:“ 200“,” sum_total“:” RS.156283.38“,” vat“:” RS.3064.38“,” itemTotal“:” 51073“,”條形碼“:” BQSP78BB“,” net_rate“:” 24200“,” date“ ::“ 2015-12-02”,“發票編號”:“ 1”,“ bill_type”:“發票”},{“ custInfo”:“ Ujwal 9975022560”,“ rate”:“ 24000”,“ weight”:“ 21.00000 “,” desc“:” GENTS ANGTHI 22k NO STONE“,” makingAmt“:” 200“,” sum_total“:” RS.156283.38“,” vat“:” RS.3064.38“,” itemTotal“:” 51073“, “條形碼”:“ BQSP78BB”,“ net_rate”:“ 24200”,“日期”:“ 2015-12-02”,“發票編號”:“ 1”,“帳單類型”:“發票”},{“ custInfo”: “ Ujwal 9975022560”,“ rate”:“ 24000”,“ weight”:“ 21.00000”,“ desc”:“ GENTS ANGTHI 22k NO STONE”,“ makingAmt”:“ 200”,“ sum_total”:“ RS.156283.38” ,“ vat”:“ RS.3064.38”,“ itemTotal”:“ 51073”,“條形碼”:“ BQSP78BB”,“ net_rate”:“ 24200”,“ date”:“ 2015-12-02”,“ invoiceNo” :“ 1”,“ bill_type”:“發票”}])

我想做的就是解析此響應並將數據插入到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.

謝謝你:)

試試這個(更新):

$arr = json_decode(stripslashes($_POST['jsonarray']), true);
$env = $arr['jsonarray'];
$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);

編輯:添加了一些錯誤檢查,以幫助調試代碼。

您可以嘗試使用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";
    }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM