user2040602
user2040602

Reputation:

PHP Loop JSon and Insert the Json Data into Database

{"details":
[{"Ticket_Date":"2014-02-20 11:32:05","Ticket_No":1950,"Amount":1.3,"In_Out":"IN","Vehicle_ID":27,"From_LocationID":3,"PriceType_ID":0,"Trip_ID":"6d744df4dfc017b1-103879384421","To_LocationID":1,"Inspector_Print ":0,"Driver_ID":16},
{"Ticket_Date":"2014-02-20 11:56:22","Ticket_No":1951,"Amount":1.3,"In_Out":"IN","Vehicle_ID":27,"From_LocationID":3,"PriceType_ID":0,"Trip_ID":"6d744df4dfc017b1-505617563631","To_LocationID":1,"Inspector_Print ":1,"Driver_ID":16}
]}

This is the Json Pass to PHP, and i m trying to use PHP insert those data in my Database

<?php
$data = file_get_contents("php://input");
//echo $data;
//$obj = var_dump(json_decode($data));

$json = json_decode($data);


mysql_connect("localhost", "root", "123456") or die("Could not connect");
mysql_select_db("db_shuttlebus") or die("Could not select database");

if (is_array($json)) {
    $d = array();
    foreach($json as $obj) {
        $Ticket_No = $obj->{'Ticket_No'};
        $Ticket_Date = $obj->{'Ticket_Date'};
        $Amount = $obj->{'Amount'};
        $In_Out = $obj->{'In_Out'};
        $Vehicle_ID = $obj->{'Vehicle_ID'};     
        $From_LocationID = $obj->{'From_LocationID'};
        $PriceType_ID = $obj->{'PriceType_ID'};
        $Trip_ID = $obj->{'Trip_ID'};
        $To_LocationID = $obj->{'To_LocationID'};
        $Inspector_Print = $obj->{'Inspector_Print'};
        $Driver_ID = $obj->{'Driver_ID'};
        $Updated_Time = date("Y-m-d H:i:s");
        $Route_ID = $obj->{'Route_ID'};
        //echo $_id;
        $query = "INSERT INTO tbl_ticket (Ticket_No,Ticket_Date,Amount,In_Out,Vehicle_ID,From_LocationID,PriceType_ID,Trip_ID,To_LocationID,Inspector_Print,Driver_ID,Updated_Time,Route_ID)VALUES('".$Ticket_No."','".$Ticket_Date."','".$Amount."','".$In_Out."','".$Vehicle_ID."','".$From_LocationID."','".$PriceType_ID."','".$Trip_ID."','".$To_LocationID."','".$Inspector_Print."','".$Driver_ID."','".$Updated_Time."','".$Route_ID."')";

        $rs = mysql_query($query) or die ("Error in query: $query " . mysql_error());
            if (!$rs) { 
                $d[] = array('Ticket_No' => $Ticket_No ,'Updated_Time' => $Updated_Time);
            }
    }
    $pass_json = json_encode($d);
    echo $pass_json;

}
?>

But, i can't get any Data in $json as $obj , Why?

Upvotes: 0

Views: 118

Answers (2)

jterry
jterry

Reputation: 6269

is_array will return FALSE for $json, because even if the JSON is valid (which it looks like it is), by default json_decode returns an object, not an array. This line:

if (is_array($json)) {

...will always return FALSE and not execute. To convert your JSON to an associative array, pass the second parameter to the function:

$json = json_decode($data, true);

In your example, just change is_array to is_object, if you want to leave the rest of your code as-is. A much better way to check if your JSON was valid, however:

if(json_last_error() === JSON_ERROR_NONE) {

Then you can deal with the problem of the details parameter of your decoded object:

foreach($json->details as $obj) {

Upvotes: 0

Andrew
Andrew

Reputation: 1776

You should replace your foreach with

foreach($json->details as $obj)

Upvotes: 2

Related Questions