douhun8647 2018-05-12 06:33 采纳率: 0%
浏览 26
已采纳

将PHP数组数据(从XML)插入SQL无效

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

  • 写回答

1条回答 默认 最新

  • duangengruan2144 2018-05-12 07:12
    关注

    When you build up your final SQL, you have extra quotes and .'s. So the code in the line should be...

    $sql= mysqli_query($link, "INSERT into amazon_orders (AmazonOrderId, OrderStatus) 
                    VALUES $imploded ");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度