how to receive dynamic data from a form and write them to the BD

Asked

Viewed 20 times

0

my difficulty happens because I created a dynamic grid where I have sequential lines for product inclusion, so I don’t have the names of the traditionally fixed input.

In this case I’m assigning sequential numbering according to which I insert the lines.

Ex:

I WILL FOCUS ONLY ON THE PRODUCT FIELD, AS OTHERS WILL FOLLOW THE SAME CONFIGURATION

and when I get these values I can’t read the data ...

$COMPRAS = NEW Compras();


$total              = $_GET['hidden_total'] ; // recebo o valor total da compra
$linha              = $_GET['hidden_linha'] ; // recebo o numero de linhas de produtos inseridos

for ( $i = 1 ;  $i <= $linha ;  $i ++ ) { 

  $produto.$i       = strtoupper($_POST['produto'.$i]) ;
  $qtd.$i           = $_POST['qtd'.$i] ;
  $valor.$i         = $_POST['valor'.$i] ;


  $COMPRAS->produto.$i($produto.$i);
  $COMPRAS->qtd.$i($qtd.$i);
  $COMPRAS->valor.$i($valor.$i);

}

and the class receives the data in this way ...

public function produto($produto)       {   $this->produto = $produto; }

public function qtd($qtd)               {   $this->qtd = $qtd; }

public function valor($valor)           {   $this->valor = $valor; }



### INSERT DE PRODUTOS ### x_produtos_cadastro -> x_produtos_cadastro_ok 

public function insert(){

    $sql  = "INSERT INTO $this->table ( produto,qtd,valor )
    VALUES  ( :produto,:qtd,:valor )";


    $z = DB::prepare($sql);

    //$z->bindParam(':usuario'          , $this->usuario);

    $z->bindParam(':produto'        , $this->produto);
    $z->bindParam(':qtd'            , $this->qtd);
    $z->bindParam(':valor'          , $this->valor);

    return $z->execute(); 

}
  • Why don’t you just add [] in the field name and handles the values with the array what to receive?

  • because in the implementation I had to do several controls and even get here I used counters.

  • within the grid I calculate everything, order the products, other functionalities, etc.... Anyway, I had to use accountants and now I have to finish and save everything in BD

No answers

Browser other questions tagged

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