Registration in foreach php

Asked

Viewed 109 times

0

all right? I’m trying to register several entries in the comic according to the number of installments requested, but I’m not getting. 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
  • 1

    have an error there, SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '2' for column 'dta_request' at Row 1' (length=105)

  • then the date format is correct because I have already been able to register like this in other tables :(

1 answer

2


The estate $this->Data seems to be a associative array. Although PHP does not distinguish between associative array and indexed array, its code does. Try to remove the foreach of the code, and shall 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 your comments, you used the foreach to write client data + data of each parcel into the database, creating separate lines in the database for each parcel. You can use the property nro_parcela in the foreach to save the data the way you want it:

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’ve helped.

  • The error before was solved, however as I think Voce noticed that inside my array in the debug I have 3 more two-dimensional array that would be the number of plots and each expiration date in relation to the number of plots, now I see this 'Line Error': # 169 # :: Array to string Conversion!

  • I was using foreach precisely to register all records type 5 plots, so 5 records!

  • Got it. One of the fields that defines the amount of plots is the nro_parcela, correct?

  • That’s right 'nro_parcel' => array (size=3) 0 => string '1' (length=1) 1 => string '2' (length=1) 2 => string '3' (length=1)

  • in this case is 3 plot, but, can be 5 6 7 up to 12.

  • Thank you very much!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.