Jump to content

Update mysql from array


radagast
Go to solution Solved by objnoob,

Recommended Posts

Hello

 

I am really having a brain freeze an am hoping that someone can help.

 

I will be receiving the below array from our courier every 3 hours and need to update our mysql database.

 

Array Snippet :

 

{"tracking":[{"waybill":"TCG3267158","booking_no":"94887"}]}{"tracking":[{"waybill":"TCG3240376","booking_no":"94870"}]}{"tracking":[{"waybill":"TCG3267284","booking_no":"94664"}]}{"tracking":[{"waybill":"TCG3343222","booking_no":"94892"}]}{"tracking":[{"waybill":"TCG3235404","booking_no":"93326"}]}{"tracking":[{"waybill":"TCG2976343","booking_no":"93547"}]}{"tracking":[{"waybill":"TCG3246653","booking_no":"93357"}]}{"tracking":[{"waybill":"TCG3199956","booking_no":"93517"}]}{"tracking":[{"waybill":"TCG3216882","booking_no":"93449"}]}{"tracking":[{"waybill":"TCG3170110","booking_no":"93879"}]}{"tracking":[{"waybill":"TCG3247064","booking_no":"93807"}]}{"tracking":[{"waybill":"TCG3252060","booking_no":"93439"}]}

 

 

I am unsure on how to retrieve the data from the array to insert into our database as per the below.

 

 

$sql = "UPDATE waybill SET waybill = '$waybill' WHERE booking_no = '$booking_no' AND waybill IS NULL";
$update=mysql_query($sql) or die(mysql_error());
 

 

All and any help will be greatly appreciated.

 

 

Link to comment
Share on other sites

That's not a PHP array.  That looks like JSON, and it also appears to be more than 1 object which is invalid if evaluated as a single entity.

Ensure that the tracking objects be contained in a single javascript array/object...

 

[ 

       {"tracking":[{"waybill":"TCG3267158","booking_no":"94887"}]}

   ,   {"tracking":[{"waybill":"TCG3240376","booking_no":"94870"}]}

]

 

Use PHP's buildin json_decode() function to convert the JSON string to a PHP array.

Now as a PHP array, you can loop through the tracking elements....

$javascript_array = '[ {"tracking":[{"waybill":"TCG3267158","booking_no":"94887"}]},{"tracking":[{"waybill":"TCG3240376","booking_no":"94870"}]}]';

$tracking_list = json_decode($javascript_array);
foreach($tracking_list as $track) {

    # SANITIZE TRACKING DATA HERE

     $sql = "UPDATE waybill SET waybill = '{$track['tracking']['waybill']}' WHERE booking_no = '{$track['tracking']['booking_no']}' AND waybill IS NULL";
     $update=mysql_query($sql) or die(mysql_error());
}

Be sure to sanitize the values going into the database before using them in your UPDATE statement.

Link to comment
Share on other sites

Good Day objnoob

 

Thank you so much for your responds.

 

Yes you are right they have now confirm that it is in fact JSON and I have asked them to make it a single javascript array/object as you suggested and they seem to be more confused than me.

 

They sent me the below code.

 

$arr = array("waybill" => $WAYBILL, "booking_no" => $REFERENCE); 

 
 
 $data = array(
'tracking' => array()
);
$data['tracking'][ ] = $arr; 
 
 
header('Content-Type: application/json; charset=utf8');

echo json_encode($data); 

 

 

Your help once again will be greatly appreciated.

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.