insert multiple fixes with php

1

How can I insert multiple fixes in mysql with php.

html

<form action="">
  <input type="text" name="producto[]">
  <input type="text" name="precio[]">
  <input type="text" name="cantidad[]">
  <input type="submit">
</form>

php

$arrayproducto=$_POST['producto'];
$arraycantidad=$_POST['cantidad'];

$arrayprecio=$_POST['precio'];

$combinar=array_combine($arrayproducto, $arraycantidad);

foreach($combinar as $producto=>$cantidad)
{
 // Consulta para insertar los arreglos
}  

Usually I inserted two arrays that way, but now that I have another array ( $ arrayprice ), how can I insert the third one?

    
asked by Raphael 20.12.2016 в 04:04
source

1 answer

1

If what you have in those $_POST use the same key:

$arrayproducto=$_POST['producto'];
$arraycantidad=$_POST['cantidad'];
$arrayprecio=$_POST['precio'];
foreach($arrayproducto as $llave=>$producto){
  $array_final[$producto] = [
    'cantidad'=>$arraycantidad[$llave], 
    'precio'=>$arrayprecio[$llave] 
  ];
}

print_r($array_final);

To build your queries SQL would be something like this:

foreach($array_final as $producto=>$detalle){
  $sql="INSERT INTO tbl_detalle_venta (prod_id, cantidad, precio) VALUES ('$producto','$detalle[cantidad]','$detalle[precio]')";
}

If you do not use the same key we would need an example of the content of those $_POST , I suggest try this code.

    
answered by 20.12.2016 / 09:12
source