Load extra data by the id received by the array

0

I have a page that receives the ID (s) of the product (s) that are already saved in the 'products' table and being added via $_POST to an array that receives this data (product_name, product_id , commodity value, quant_product)

In the 'products' table, this data is already saved, such as product_id, product_name, etc., and also other data such as ipi, icms, cofins.

I am currently receiving via $_POST only some fields like the ID (s) of the Product (s) added, I also need to consult the REMAINING of the data LINKED to these ID (s), which already are saved in the 'products' table (eg ipi, icms, cofins).

SUMMARY: Also take the other fields of the products table linked to each ID added via $_POST in the form of arrays (eg: product_id [])

//POST em array que recebo

$valor_unitario = $_POST['valorunitario'];
$nome-produto= $_POST['nome_produto'];
$quant_produto = $_POST['quant_produto'];
$subtotal = $_POST['subtotal'];
$id_produto = $_POST['id_produto'];


//AQUI INSERE OS ARRAYS NO BANCO DE DADOS, RECEBE OS PRODUTOS ADICIONADOS VIA POST (AQUI QUERO SALVAR O RESTANTE DOS CAMPOS DO PRODUTO ATRAVÉS DO ID DELES.
$i=0;
$sql= "INSERT INTO 'log_nfe_produtos' ('NOME_PRODUTO', 'VALOR_UNITARIO', 'QUANT_PRODUTO', 'SUBTOTAL', 'ID_EMPRESA','ID_NF') VALUES ";

foreach($nome_produto as $p){

$sql=$sql."('$p','$valor_unitario[$i]','$quant_produto[$i]','$subtotal[$i]','1','$cod_pedido[$i]'),";

$i++;

}

$sqlFinal = substr($sql, 0, strlen($sql)-1);

$sqlFinal2 = $conn->prepare("$sqlFinal");

$sqlFinal2->execute();
    
asked by anonymous 06.07.2017 / 00:02

1 answer

1

So if you want to redeem the values of the product that was sent per post before doing this insert you have to do a select before in your database by the product id;

$dbi     = Conexao::singleton();
$query   = mysql_query("SELECT IPI,ICMS,CONFINS FROM PRODUTOS WHERE ID_PRODUTO =".$id_produto,$dbi);
$valores = mysql_fetch_array($query,MYSQL_ASSOC);

Now to use the values collected in the bank with the id of the product simply do so:     $ values ['IPI'];     $ values ['ICMS'];     $ values ['CONFINS'];

Example does not use the mysqli class but can follow this documentation on how to work with this class.

link

I hope it helps understanding.

Changing as requested in comment:

$dbi            = Conexao::singleton();
$listaProdutos  = Array(0,1,32,58,10);
for($i=0;$i<count($listaProdutos);$i++){
   $query   = mysql_query("SELECT IPI,ICMS,CONFINS FROM PRODUTOS WHERE 
   ID_PRODUTO =".$listaProdutos[$i],$dbi);
   $valores = mysql_fetch_array($query,MYSQL_ASSOC);
   // Aqui pode entrar seu insert para notas fiscais utilizando os valores 
   //coletados da tabela produtos e do POST
}
  

Expanding I used a FOR loop to read an array with product ids for each > loop increment. Let's read a new position of the array and referring to the database the values referring to the product that are IPI, ICMS and COFINS.

Array is nothing more than a list:

  

$ listProducts = Array (0,1,32,58,10);

     

$ listProducts [0] = 0;   $ listProducts [1] = 1;   $ listProducts [2] = 32;   $ listProducts [3] = 58;   $ listProducts [4] = 10;

There is another solution to what I think you want to do. If you want to insert the IPI, ICMS and COFINS in the invoice table could make this select within the insert itself is more laborious and time-consuming but nevertheless consumes much less resource of the machine.

You can just do this:

 INSERT INTO log_nfe_produtos ('NOME_PRODUTO', 'VALOR_UNITARIO', 'QUANT_PRODUTO', 'SUBTOTAL', 'ID_EMPRESA','ID_NF','IPI','ICMS','COFINS')
 SELECT '$valor_unitario[$i]','$quant_produto[$i]','$subtotal[$i]','1','$cod_pedido[$i]', IPI,ICMS,COFINS FROM PRODUTOS WHERE ID_PRODUTO =".$id_produto;
    
06.07.2017 / 04:49