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 ");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里