Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would do something like this:</p> <pre><code>$fields = array( 'titulo', 'tipo_produto', 'quantidade_peso', 'unidade_de_venda', 'unidades_por_caixa', 'caixas_piso', 'pisos_palete', 'tipo_de_palete', 'unidades_palete', 'caixas_palete', 'uni_diametro', 'uni_largura', 'uni_profundidade', 'uni_altura', 'uni_peso_bruto_unidade', 'caixa_largura', 'caixa_profundidade', 'caixa_altura', 'altura_palete', 'volume_unidade', 'peso_caixa', 'peso_palete' ); $sql = 'INSERT INTO ficha_item ( %s ) VALUES ( %s )'; // make a list of field names: titulo, tipo_produto /*, etc. */ $fieldsClause = implode( ', ', $fields ); // make a list of named parameters: :titulo, :tipo_produto /*, etc. */ $valuesClause = implode( ', ', array_map( function( $value ) { return ':' . $value; }, $fields ) ); // or, with create_function $valuesClause = implode( ', ', array_map( create_function( '$value', 'return ":" . $value;' ), $fields ) ); $sql = sprintf( $sql, $fieldsClause, $valuesClause ); // $sql is now something similar to (formatted for display): // INSERT INTO ficha_item // ( titulo, tipo_produto /*, etc. */ ) // VALUES // ( :titulo, :tipo_produto /*, etc. */ ) $stmt = $db-&gt;prepare($sql); // if the keys in $_POST match with $fields, you can now simply pass $_POST here $stmt-&gt;execute( $_POST ); // or, as per Bill Karwin's sound suggestion, with the intersection of $_POST $stmt-&gt;execute( array_intersect_key( $_POST, array_flip( $fields ) ) ) </code></pre> <p>In other words, use named parameters, and have them be dynamically generated based on the <code>$fields</code> array. Although named parameters are not strictly necessary; they <em>do</em> help make things easier, because the order of elements passed to, for instance, <code>PDOStatement::execute()</code>doesn't matter now anymore.</p> <p><s>This assumes though, that the number of elements and their keys in <code>$_POST</code>, exactly match the number of fields and their names in <code>$sql</code>.</s></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload