I received XML data from Amazon API request and successfully stored it into an array.
The array looks like this:
array(2)
{
["ListOrdersResult"]=> array(2)
{
["Orders"]=> array(1)
{
["Order"]=> array(94)
{
[0]=> array(25)
{
["LatestShipDate"]=> string(20) "2018-05-09T06:59:59Z"
["OrderType"]=> string(13) "StandardOrder"
["PurchaseDate"]=> string(20) "2018-05-09T06:09:01Z"
["AmazonOrderId"]=> string(19) "111-1111111-111111"
["BuyerEmail"]=> string(38) "somecode@marketplace.amazon.com"
["IsReplacementOrder"]=> string(5) "false"
and so on....
For now I try to store 2 variables from this 94 Order arrays in my SQL database, without luck.
What I've tried so far:
$orderdata = array();
foreach($orders as $results) {
foreach($results as $amaorders) {
foreach($amaorders as $numbers){
foreach($numbers as $data => $finaldata){
$orderid = $finaldata['AmazonOrderId'];
$orderstatus = $finaldata['OrderStatus'];
$orderdata[] = "('$orderid','$orderstatus')";
}
}
}
}
$link = mysqli_connect("localhost", "sendjapa_amazon", "hayashir", "sendjapa_amazon");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$imploded = implode(',', $orderdata);
$sql= mysqli_query($link, "INSERT into amazon_orders (AmazonOrderId, OrderStatus) VALUES '.$imploded.'' ");
if($sql === false){
die(mysqli_error($link));
}
The following error message is shown:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.('111-11111 and so on.
When I var_dump($orderdata) it looks like it correctly stores the data into $orderdata:
array(92)
{
[0]=> string(33) "('111-111111-111111','Shipped')"
[1]=> string(35) "('222-222222-222222','Unshipped')"
[2]=> string(33) "('333-3333333-3333333','Shipped')"
[3]=> string(33) "('444-4444444-4444444','Shipped')"
and so on...
When I var_dump($imploded) it looks like this:
string(3163) "('111-111111-111111','Shipped'),('222-222222-222222','Unshipped'),('333-333333-333333','Shipped'),('444-44444-444444','Shipped'), and so on...
Usually I have the database connect code in a separate file, but I've included it here so you can see the complete code.
Thank you