Registration in foreach php

0

Alright? I am trying to register several records in the bd according to the number of parcels requested, but I am not able to. My code:

php:

 $Conn = parent::getConn();
    try {
        foreach ($this->Data as $pr):
            $Query = $Conn->prepare("INSERT INTO c022vdpr (dta_pedido, nro_pedido, cod_cliente, vlr_parcela, vct_parcela, nro_parcela, quitacao) VALUES (:dtaped, :nroped, :codcli, :vlrpar, :vctpar, :nropar, :quit)");
            $Query->execute(array(':dtaped' => $pr['dta_pedido'], ':codcli' => $pr['cod_cliente'], ':nroped' => $pr['nro_pedido'], ':vlrpar' => $pr['vlr_parcela'], ':vctpar' => $pr['vct_parcela'], ':nropar' => $pr['nro_parcela'], ':quit' => $pr['quitacao']
            ));
            $this->Result = true;
            $this->Error = ["<b>Atualizado!</b>", ACCEPT];
        endforeach;
    } catch (PDOException $e) {
        $this->Result = false;
        $this->Error = [$e->getMessage(), ERROR];
    }

error = Erro na Linha : # 168 # :: Illegal string offset

var_dump:

object(AdminProd)[2]
private 'Data' => 
array (size=7)
  'dta_pedido' => string '2017-07-03' (length=10)
  'nro_pedido' => int 477037
  'cod_cliente' => 
    array (size=3)
      0 => string '5' (length=1)
      1 => string '5' (length=1)
      2 => string '5' (length=1)
  'vlr_parcela' => float 132.5
  'vct_parcela' => 
    array (size=3)
      0 => string '2017-08-31' (length=10)
      1 => string '2017-09-30' (length=10)
      2 => string '2017-10-31' (length=10)
  'nro_parcela' => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
  'quitacao' => string 'N' (length=1)
   private 'CatId' => null
   private 'Error' => 
array (size=2)
  0 => string 'SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '2' for column 'dta_pedido' at row 1' (length=105)
  1 => string 'error' (length=5)
  private 'Result' => boolean false
    
asked by anonymous 03.07.2017 / 22:06

1 answer

2

The $this->Data quer property appears to be a associative array . Although PHP does not distinguish between associative array and indexed array, its code does. Try to remove foreach of code, and should have the expected result:

try {
    $Query = $Conn->prepare("INSERT INTO c022vdpr (dta_pedido, nro_pedido, cod_cliente, vlr_parcela, vct_parcela, nro_parcela, quitacao) VALUES (:dtaped, :nroped, :codcli, :vlrpar, :vctpar, :nropar, :quit)");
    $Query->execute(array(':dtaped' => $this->Data['dta_pedido'], ':codcli' => $this->Data['cod_cliente'], ':nroped' => $this->Data['nro_pedido'], ':vlrpar' => $this->Data['vlr_parcela'], ':vctpar' => $this->Data['vct_parcela'], ':nropar' => $this->Data['nro_parcela'], ':quit' => $this->Data['quitacao']
    ));
    $this->Result = true;
    $this->Error = ["<b>Atualizado!</b>", ACCEPT];
} catch (PDOException $e) {
    $this->Result = false;
    $this->Error = [$e->getMessage(), ERROR];
}

UPDATE

As you mentioned in the comments, you used foreach to record the customer data + data for each parcel, creating separate rows in the base for each parcel. You can use the nro_parcela property in foreach to save the data as you want:

try {
    foreach ($this->Data['nro_parcela'] as $k => $v):
        $Query = $Conn->prepare("INSERT INTO c022vdpr (dta_pedido, nro_pedido, cod_cliente, vlr_parcela, vct_parcela, nro_parcela, quitacao) VALUES (:dtaped, :nroped, :codcli, :vlrpar, :vctpar, :nropar, :quit)");
        $Query->execute(array(':dtaped' => $this->Data['dta_pedido'], ':codcli' => $this->Data['cod_cliente'][$k], ':nroped' => $this->Data['nro_pedido'], ':vlrpar' => $this->Data['vlr_parcela'], ':vctpar' => $this->Data['vct_parcela'][$k], ':nropar' => $v, ':quit' => $this->Data['quitacao']
        ));
        $this->Result = true;
        $this->Error = ["<b>Atualizado!</b>", ACCEPT];
    endforeach;
} catch (PDOException $e) {
    $this->Result = false;
    $this->Error = [$e->getMessage(), ERROR];
}

I hope I have helped.

    
03.07.2017 / 22:48