Load extra data by the id received by the array

Asked

Viewed 70 times

0

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

In the 'products' table these data are already saved, such as id_product, product name, etc, and also other data such as ipi, icms, Cofins.

I’m getting via $_POST just some fields like the ID(s) of the Product(s) added(s), I also need to consult the REST of the data LINKED to this(s) ID(s), which are already saved(s) in the table 'products' (ex: ipi,icms,Cofins).

SUMMARIZING: Also pick the other fields of the products table linked to each ID added via $_POST arrays (e.g.: id_product[] )

//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();
  • Okay, I will comment: your question does not make much sense. It has like [Edit] and try to be clearer in your text?

  • Okay, I changed the question, check if you have conditions to solve.

1 answer

1

So if you want to redeem the values of the product that was sent by post before performing this Index you have to select before in your database by 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 database with the product id just do so: $values['IPI']; $values['ICMS']; $values['CONFINS''];

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

https://secure.php.net/manual/en/mysqli.prepare.php

I hope it helps the 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
}

I used a FOR loop to read an array with product id at each loop increment. Let’s read a new position of the array and consult in the >database the values related 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’s another solution to what I think you want to do. If you want to insert the IPI , ICMS and COFINS in the table of fiscal notes could make this select within the Insert itself is more laborious and time consuming but nevertheless consumes much less machine resource.

Can you just do that :

 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;
  • This example you reported, works if the array has more than 1 product (2,3,4,5 products,etc) ?

  • What do you mean? Specify better than put the answer there! This example takes the value of a product by its id in the database to load IPI, CONFINS and ICMS. But if it’s multiple brother products, you can assemble it into FOR loop .

  • Yes, since it is array and receives ID(s) (o(s) means that it could or could not be multiple ID(s)), I thought you would understand that it would be multiple products... THE FUNCTION I’ve ALREADY POSTED TAKES THE PRODUCTS SENT BY THE ARRAY AND SAVED ALREADY (but I would like it to load the remaining data of each product before making this entry)

  • "There’s another solution to what I think you want to do. If you want to insert the IPI , ICMS and COFINS in the table of fiscal notes could make this select within the Insert itself is more laborious and time consuming but nevertheless consumes much less machine resource." ; That’s what I’d like to do!

Browser other questions tagged

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