Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php解析数据并将值插入mysql数据库?_Php_Mysql_Json - Fatal编程技术网

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

使用php解析数据并将值插入mysql数据库?,php,mysql,json,Php,Mysql,Json,Php代码,用于检查json数据是否来自android代码 我是php新手,所以我尝试了json_解码,但它返回一个空字符串作为响应。如何解析并将custInfo、invoiceNo、条形码、desc、重量、费率、makingAmt、净价、itemTotal、vat、sum-total、bill-type、date插入我的表所选项目。 谢谢:)试试这个(更新): 编辑:添加了一些错误检查以帮助调试代码。 您可以尝试使用 然后您可以使用将变量从数组导入到当前符号表中,这样就消除了foreach循环

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

我是php新手,所以我尝试了json_解码,但它返回一个空字符串作为响应。如何解析并将
custInfo、invoiceNo、条形码、desc、重量、费率、makingAmt、净价、itemTotal、vat、sum-total、bill-type、date
插入我的表
所选项目。

谢谢:)

试试这个(更新):


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

您可以尝试使用

然后您可以使用将变量从数组导入到当前符号表中,这样就消除了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";
    }
?>

@NanaPartykar非常感谢:)但是我如何从数组中获取单个值。我是php新手,请帮助哦,你已经编写了循环$env数组的代码,你可以使用它,因为我仍然得到一个空响应。我尝试了你的代码。你确定发布的响应不是空的吗?如果$\u POST本身为null,那么您将得到空的responseLet us。当我直接打印$\u POST时,我会得到一个响应,但当我尝试使用json_decode时,响应为blanktest。json_decode的返回值,它会以适当的PHP类型返回json编码的值,如果无法解码json,或者编码数据深度超过递归限制,则返回null。使用此选项时,我得到一个响应$arr=json_decode(stripslashes($_POST['jsonarray']),true);类似这样的数组([0]=>数组([custInfo]=>Ujwal 9975022560[费率]=>24000[重量]=>21.00000[描述]=>GENTS ANGTHI 22k NO STONE[制作]=>200[总计]=>RS.156283.38[增值税]=>RS.3064.38[项目总计]=>51073[条码]=>BQSP78BB[净利率]=>24200[日期]=>2015-12-02[发票编号]=>2[票据类型]=>发票)现在我如何解析它
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);
$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);
<?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";
    }
?>