Good morning I have a form where the input is generated automatically with a for depending on an assigned number. In short, a form of 4 fields is generated n times to be saved information for the same id.
The problem is that the inputs are generated with the same "name" and that means that when you insert them, only one row is saved.
The question is how to put them into an array and change the name to the "name" of the input so that the n numbers of inputs are saved in a single query
<?php
$cantidadI=3;
?>
<div id="contenedor">
<form id="datos" action="inserta.php" method="post">
<div id="tabla">
<table>
<?php for ($i=0; $i<=$cantidad; $i++) {
?>
<tr>
<td colspan="4" align="center">
Nombre:<input id="nombre" type="text" name="nombre[]" />
</td>
</tr>
<tr>
<td colspan="4" align="center">
No Fase:<input id="nofase" type="text" name="nofase[]" />
</td>
</tr>
<tr>
<td colspan="4" align="center">
Dias Duracion:<input id="diasdu" type="text" name="diasdu[]" />
</td>
</tr>
<tr>
<td colspan="4" align="center">
Precio:<input id="presio" type="text" name="presio[]" />
</td>
</tr>
<?php } ?>
</table><br>
</div>
<input id="guarda" type="submit" value="Guardar" />
<a href="#"><input id="cance" type="button" value="Cancelar" /></a>
</form>
</div>
INSERT:
foreach ($_POST as $key => $values) {
$query = "INSERT INTO detalles(nombre, no_fase, dias_duracion, precio) VALUES ('".$values['nombre']."','".$values['nofase']."','".$values['diasdu']."','".$values['presio']."')";
mysql_query($query, $conexion);
}
When doing the print_r ($ _ POST) I realize that it only takes
Array ( [nombre] => Array ( [0] => miguel [1] => miguel [2] => miguel ) [nofase] => Array ( [0] => 7 [1] => 7 [2] => 7 ) [diasdu] => Array ( [0] => 45 [1] => 45 [2] => 45 ) [presio] => Array ( [0] => 1236 [1] => 1236 [2] => 1236 ) )