Insert 'N' times of records with the same Input

4

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 ) )
    
asked by laura 15.06.2016 в 16:56
source

2 answers

2

One option is to create a multidimensional array with the $i that you use in the for:

        <?php for ($i=0; $i<=3 ; $i++) { ?>
            <tr>
                <td colspan="4" align="center">
                    Nombre:<input id="nombre" type="text" name="usuario[<?= $i ?>][nombre]" />
                </td>
            </tr>
            <tr>
                <td colspan="4" align="center">
                    No Fase:<input id="nofase" type="text" name="usuario[<?= $i ?>][nofase]" />
                </td>
            </tr>
            <tr>
                <td colspan="4" align="center">
                    Dias Duracion:<input id="diasdu" type="text" name="usuario[<?= $i ?>][diasdu]" />
                </td>
            </tr>
            <tr>
                <td colspan="4" align="center">
                    Precio:<input id="presio" type="text" name="usuario[<?= $i ?>][presio]" />
                </td>
            </tr>
        <?php } ?>

You should modify your INSERT form in the database a bit too:

foreach ($_POST['usuario'] 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);
}

By the way, I recommend that you DO NOT use mysql_query, use the functions of mysqli.

    
answered by 15.06.2016 / 17:30
source
0

To be able to carry out what you request, it is only necessary that you add the brackets to the inputs and with this the sending of the info will be in array, that is:

<input type="text" name="nombre[]" value="asdf" />
<input type="text" name="nombre[]" value="jkl" /> 
    
answered by 15.06.2016 в 17:10