将重复数据插入mysql

时间:2016-03-05 20:26:04

标签: php mysql json

我想将一些数据添加到mysql表中。当我尝试创建行时,我使用此代码:

$sqlJetId = "INSERT INTO orders (jetId, amazonId, name, phoneNumber, addr1, addr2, city, state, zipcode, sku, quantity, carrier, method, trackingNumber, rejected, reasonRejected, cancel, returned, complete, error) VALUES ('$jetId', '', '$name', '$phoneNumber', '$addr1', '$addr2', '$city', '$state', '$zipcode', '$sku', '$quantity', '$carry', '$level', '', '', '', '', '', '', '')";
$resultJetId = $sqlConnection->query($sqlJetId);
if ($sqlConnection->query($sqlJetId) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sqlJetId . "<br>" . $sqlConnection->error;
}

没有发生错误,但这些值不会插入到sql表中。

此代码位于foreach循环中。这是完整的foreach循环:

foreach($url as $jetId)
{
   $skus = array();
   $ItemId = array();

   $jetAPI->getOrderDetail();
   if($detail = json_decode($detailResponse))      
   {
     if($orderDetail = $detail->order_detail);
     { 
         $carry = $orderDetail->request_shipping_carrier;
         $level = $orderDetail->request_service_level;
     }
     if($shipto = $detail->shipping_to)
     {
         if($shipto->recipient)
         {
             $name = $shipto->recipient->name;
             $phoneNumber = $shipto->recipient->phone_number;                        
         }
         if($shipto->address)
         {
             $addr1 = $shipto->address->address1;
             $addr2 = $shipto->address->address2;
             $city = $shipto->address->city;
             $state = $shipto->address->state;
             $zipcode = $shipto->address->zip_code;
         }        
     }
     if($itemsOrdered = $detail->order_items)
     {
         foreach($itemsOrdered as $itemOrdered)
         {
             $orderItemId = $itemOrdered->order_item_id;
             $sku = $itemOrdered->merchant_sku;
             $quantity = $itemOrdered->request_order_quantity;
             $cancel = $itemOrdered->request_order_cancel_qty;
             $skus[] = $sku;
             $quantityOrdered[] = $quantity;
         }
         $sku = implode(',' , $skus);
         $quantity = implode(',' , $quantityOrdered);
     }
     $jetId = substr($jetId, 30);
 }
 $sqlJetId = "INSERT INTO orders (jetId, amazonId, name, phoneNumber, addr1, addr2, city, state, zipcode, sku, quantity, carrier, method, trackingNumber, rejected, reasonRejected, cancel, returned, complete, error) VALUES ('$jetId', '', '$name', '$phoneNumber', '$addr1', '$addr2', '$city', '$state', '$zipcode', '$sku', '$quantity', '$carry', '$level', '', '', '', '', '', '', '')";
 $resultJetId = $sqlConnection->query($sqlJetId);
 if ($sqlConnection->query($sqlJetId) === TRUE) {
     echo "New record created successfully";
 } else {
     echo "Error: " . $sqlJetId . "<br>" . $sqlConnection->error;
 }
}

如果我做错了,请告诉我。我将不胜感激。我需要将数据插入到sql表中。

以下是我正在使用的一些测试数据:

stdClass Object
(
[buyer] => stdClass Object
    (
        [name] => 'Bart Simpson'
        [phone_number] => '212-212-4766'
    )

[customer_reference_order_id] => 002241836938
[fulfillment_node] => 29b7756343db41929281828b8fd46112
[has_shipments] => 
[hash_email] => 687931655832@customer.jet.com
[jet_request_directed_cancel] => 
[merchant_order_id] => 1c3397fae30747c381f8ed43a0355396
[order_detail] => stdClass Object
    (
        [request_shipping_carrier] => UPS
        [request_service_level] => Expedited
        [request_ship_by] => 2016-02-09T22:55:54.1834573Z
        [request_delivery_by] => 2016-02-10T22:55:54.1834573Z
    )

[order_items] => Array
    (
        [0] => stdClass Object
            (
                [order_item_id] => bc4f8a9a396f4ba2bace409b136c8282
                [merchant_sku] => Rubies_32295
                [request_order_quantity] => 2
                [request_order_cancel_qty] => 0
                [item_tax_code] => 
                [item_price] => stdClass Object
                    (
                        [item_tax] => 
                        [item_shipping_cost] => 0.66
                        [item_shipping_tax] => 
                        [base_price] => 10.99
                    )

                [product_title] => Star Wars: The Force Awakens Child\'s Stormtrooper Half Helmet
                [url] => "http://www.jet.com/api/merchant_skus/Rubies_32295"
            )

    )

[order_placed_date] => 2016-02-08T22:54:54.1834573Z
[order_totals] => stdClass Object
    (
        [item_price] => stdClass Object
            (
                [item_tax] => 
                [item_shipping_cost] => 1.32
                [item_shipping_tax] => 
                [base_price] => 21.98
            )

    )

[order_transmission_date] => 2016-02-08T22:55:54.1834573Z
[reference_order_id] => 878597791383
[shipping_to] => stdClass Object
    (
        [recipient] => stdClass Object
            (
                [name] => Maggie Simpson
                [phone_number] => 212-212-3939
            )

        [address] => stdClass Object
            (
                [address1] => 26 Birch street
                [address2] => 
                [city] => Old Cairo
                [state] => MS
                [zip_code] => 38829
            )

    )

[status] => ready
)

提前谢谢!

0 个答案:

没有答案