Data insertion error while parsing Json array in PHP?

285
September 09, 2017, at 02:24 AM

Hi am getting the offset error issue in this code while parsing the data getting error on line

Notice : Undefined index: POB_QTY in C:path on line 30

Notice: Undefined index: POB_NAME in C:path on line 29

Notice: Uninitialized string offset: 1 in C:path on line 36

Notice: Uninitialized string offset: 1 in C:path on line 36

<?php
  include('config.php');
    // read json file
if($_SERVER['REQUEST_METHOD'] == "GET"){
    $filename = 'chemist.json';
    $json_data = file_get_contents($filename);   
    // $json_data=$_POST['QUESTION'];
    //convert json object to php associative array
    $data = json_decode($json_data,true);
    print_r($data);
if (is_array($data) || is_object($data))
{
    for($j=0;$j<sizeof($data);$j++){
        $jsonData[$j] = $data[$j]['DATA'];
        $jsonPob[$j] = $data[$j]['POB'];
        $name = $data[$j]['DATA']['CHEMIST_NAME'];
        $mobile = $data[$j]['DATA']['CHEMIST_MOBILE'];
        $products = $data[$j]['DATA']['STOCKIEST_NAME'];
        $prd = $data[$j]['DATA']['PRODUCT_CODE'];
        $createdby = $data[$j]['DATA']['CREATEDBY'];
        $createdon = $data[$j]['DATA']['CREATEDON'];

        for($i=0;$i<sizeof($data[$j]['POB']);$i++)
        {
            $pob[$i] = $data[$j]['POB'][$i]['POB_NAME'];
            $pobqty[$i] = $data[$j]['POB'][$i]['POB_QTY'];
            $pob=$pob[$i];
            $pobqty=$pobqty[$i];
            // echo $pob[$i]." <--> ".$pobqty[$i]."<br/>";
            $sql="INSERT INTO CHEMISTDEATILS(CHEMIST_NAME,CHEMIST_MOBILE,STOCKIEST_NAME,PRODUCT_CODE,POB_NAME,POB_QTY,CREATEDBY,CREATEDON)
        VALUES ('$name', '$mobile', '$stock', '$prd', '$pob[$i]', '$pobqty[$i]','$createdby', '$createdon');";
            $qur = mysql_query($sql);
        if($qur){
        $json = array("status" => 1, "msg" => "Done User added!");
        }else{
        $json = array("status" => 2, "msg" => "Already Submitted");
    }
        }
        // echo "<br/>-----------<br/>";

    }

}
}else{
    $json = array("status" => 0, "msg" => "Request method not accepted");
}

@mysql_close($conn);
/* Output header */
// header('Content-type: application/json');
echo json_encode($json);    
    //close connection
 ?>

Sample Json data

[{
    "DATA": {
        "PRODUCT_CODE": "H5414110104,H5414110105,",
        "CHEMIST_NAME": "Bhu",
        "CHEMIST_MOBILE": "8985180557",
        "STOCKIEST_NAME": "Cgh",
        "CREATEDON": "2017-09-15 22:58:01",
        "CREATEDBY": 10643
    },
    "POB": [{
        "POB_NAME": "TASTE GOOD"
    }, {
        "POB_QTY": "0"
    }, {
        "POB_NAME": "GLUCOWISE"
    }, {
        "POB_QTY": "0"
    }, {
        "POB_NAME": "ISTAPHASE"
    }, {
        "POB_QTY": "0"
    }, {
        "POB_NAME": "PRIME"
    }, {
        "POB_QTY": "0"
    }, {
        "POB_NAME": "JIOTAN"
    }, {
        "POB_QTY": "0"
    }, {
        "POB_NAME": "ROST"
    }, {
        "POB_QTY": "0"
    }, {
        "POB_NAME": "EXTEL"
    }, {
        "POB_QTY": "0"
    }]
}]
Answer 1

You have to get the DATA and POB values as follows as the $data is an object.

ex: $data->DATA & $data->POB

Rent Charter Buses Company
READ ALSO
How to check the value of a variable php

How to check the value of a variable php

I just started php I want to check the value of a variable If it was not a null, it would return the video to me The part I want to edit $ response ['type']

241
Free odds live feed api

Free odds live feed api

I am a website developerI want an API for odds live feed for free or on little cost

315
Using mutiple buttons inputs upload multiple images while saving names of them in database

Using mutiple buttons inputs upload multiple images while saving names of them in database

I am Francisco Costa and i am new to php html5 and js i am doing a project and i am learning on the flyRight now i have 1 form where i ask people data about their comapanies and the top products that they sale, the prices and pictures of it

212