无法将返回的API请求插入数据库 - php

时间:2017-05-21 06:21:39

标签: php mysql xml multidimensional-array foreach

我目前得到的错误:

  

未捕获的异常' PDOException'消息' SQLSTATE [23000]:   完整性约束违规:1062重复条目   ' ac993f75-035b-433C-82e0-7b7a2d40802c'对于密钥' account_id''

我试图插入到我的表中的xml数组如下,

SimpleXMLElement Object
(
    [PaymentID] => ddbbf5df-16d7-4f07-be3b-50102d015473
    [Date] => 2016-11-30T00:00:00
    [BankAmount] => 14.38
    [Amount] => 14.38
    [Reference] => manual
    [CurrencyRate] => 1.000000
    [PaymentType] => ACCRECPAYMENT
    [Status] => AUTHORISED
    [UpdatedDateUTC] => 2016-11-30T20:50:25.73
    [Account] => SimpleXMLElement Object
        (
            [AccountID] => ac993f75-035b-433c-82e0-7b7a2d40802c
            [Code] => 090
        )

    [Invoice] => SimpleXMLElement Object
        (
            [Contact] => SimpleXMLElement Object
                (
                    [ContactID] => 077866c9-462a-4713-9cc1-e75e3b4e29df
                    [ContactNumber] => SH-Customer-4236919494
                    [Name] => Joe Bloggs - 1
                )

            [CurrencyCode] => NZD
            [Type] => ACCREC
            [InvoiceID] => 2d06200d-69e7-488d-babb-db2664c09225
            [InvoiceNumber] => INV Order #1010
        )

)
SimpleXMLElement Object
(
    [PaymentID] => 1ea3b3ec-65a9-4fb8-8646-5376a6d2a6a3
    [Date] => 2016-11-30T00:00:00
    [BankAmount] => 14.38
    [Amount] => 14.38
    [Reference] => manual
    [CurrencyRate] => 1.000000
    [PaymentType] => ACCRECPAYMENT
    [Status] => AUTHORISED
    [UpdatedDateUTC] => 2016-11-30T20:50:28.007
    [Account] => SimpleXMLElement Object
        (
            [AccountID] => ac993f75-035b-433c-82e0-7b7a2d40802c
            [Code] => 090
        )

    [Invoice] => SimpleXMLElement Object
        (
            [Contact] => SimpleXMLElement Object
                (
                    [ContactID] => e00f1e33-84b8-4c52-9428-f4c606069740
                    [Name] => Shopify Customer - UnleashedCET
                )

            [CurrencyCode] => NZD
            [Type] => ACCREC
            [InvoiceID] => cf5c20f1-cf82-4c74-9cce-7df0b7689ed4
            [InvoiceNumber] => INV Order #1011
        )

)

我正在使用的功能如下:

function dataPayments ($array)
{
    $PDO = new PDO_connection();
    $pdo = $PDO->db_connection('test_db');
    $sql = $pdo->prepare('INSERT INTO db_xero_data_payments (
                      integration_id,
                      payment_id,
                      date,
                      bank_amount,
                      reference,
                      curreny_rate,
                      payment_type,
                      status,
                      updated_date,
                      account_id,
                      account_code,
                      contact_id,
                      contact_name,
                      currency_code,
                      type,
                      invoice_id,
                      invoice_number
                      )
                      VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');

    foreach($array->Payments->Payment as $data)
    {
            $integration_id = "guid";
            $payment_id = $data->PaymentID;
            $date = $data->Date;
            $bank_amount = $data->BankAmount;
            $reference = $data->Reference;
            $curreny_rate = $data->CurrencyRate;
            $payment_type = $data->PaymentType;
            $status = $data->Status;
            $updated_date = $data->UpdatedDateUTC;
            $account_id = $data->Account->AccountID;
            $account_code = $data->Account->Code;
            $contact_id = $data->Invoice->Contact->ContactID;
            $contact_name = $data->Invoice->Contact->Name;
            $currency_code = $data->Invoice->CurrencyCode;
            $type = $data->Invoice->Type;
            $invoice_id = $data->Invoice->InvoiceID;
            $invoice_number = $data->Invoice->InvoiceNumber;

            $sql->execute([
                $integration_id,
                $payment_id,
                $date,
                $bank_amount,
                $reference,
                $curreny_rate,
                $payment_type,
                $status,
                $updated_date,
                $account_id,
                $account_code,
                $contact_id,
                $contact_name,
                $currency_code,
                $type,
                $invoice_id,
                $invoice_number
            ]);
    }

我不确定正确的循环结构是为了循环数据字段并从一个数组移动到下一个数组,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

请检查您的表格结构。

用于密钥' account_id''。可以将其设置为主键或唯一键。所以它违反了约束条件。我们您尝试使用相同的account_id加载记录。