Write array values in Mysql

0

I'm trying, unsuccessfully, to write some information in my database, I have a table where the user will insert rows according to his need, but when sending the information to the bank he is recording only the last information.

The table form is this:

        <form action="insertKit.php" method="post" target="_blank">
            <div class="table-responsive">
               <table id="products-table" class="table table-hover table-bordered">
                  <tbody>
                     <tr>
                        <th width="15%">Nº</th>
                        <th width="16%">Qtde.</th>
                        <th width="15%">Código</th>
                        <th width="32%">Descrição</th>
                        <th width="22%" class="actions">Ações</th>
                     </tr>
                     <tr>
                        <td><input type="text" name="numero[]" ></td>
                        <td><input type="text" name="quantidade[]"></td>
                        <td><input type="text" name="codigo[]"></td>
                        <td><input type="text" name="descricao[]"></td>
                        <td class="actions">
                           <button class="btn btn-large btn-danger" onclick="RemoveTableRow(this)" type="button">Remover</button>
                       </td>
                     </tr>
                  </tbody>
                  <tfoot>
                     <tr>
                        <td colspan="5" style="text-align: left;">
                           <button class="btn btn-large btn-success" onclick="AddTableRow(this)" type="button">Adicionar Linha</button>
                           <button class="btn btn-large btn-primary" type="submit">Gravar</button>
                        </td>
                     </tr>
                  </tfoot>
               </table>
            </div>
         </form>

The insert page is this:

for( $i=0; $i<count($_POST['numero']); $i++ ) {     

    // INSERINDO NO MYSQL
    mysql_select_db($database_conexao, $conexao);
    $query = "INSERT INTO kits  
                (numero,
                quantidade,
                codigo, 
                descricao   
                ) 
            VALUES 
                (
                '".$_POST['numero'][$i]."',
                '".$_POST['quantidade'][$i]."',             
                '".$_POST['codigo'][$i]."',
                '".$_POST['descricao'][$i]."' 
                )";

                echo $query;

}

    $queryExec = mysql_query($query, $conexao) or die('ERRO ao inserir registro no Banco');

    if ( $queryExec) {
        echo "GRAVADO COM SUCESSO";     
    } else {        
        echo "ERRO NA GRAVAÇÃO DO KIT";
    }
    
asked by anonymous 01.08.2017 / 23:57

1 answer

2

I think there is a position error in the script lines.

You do not need to put the connection string with the bank inside the for loop, just declare it once before for .

The $queryExec connection line you can place inside the for loop, because it writes each information in the loop.

The code looks like this:

mysql_select_db($database_conexao, $conexao);

for( $i=0; $i<count($_POST['numero']); $i++ ) {     

    // INSERINDO NO MYSQL
    $query = "INSERT INTO kits  
                (numero,
                quantidade,
                codigo, 
                descricao   
                ) 
            VALUES 
                (
                '".$_POST['numero'][$i]."',
                '".$_POST['quantidade'][$i]."',             
                '".$_POST['codigo'][$i]."',
                '".$_POST['descricao'][$i]."' 
                )";

                echo $query;

    $queryExec = mysql_query($query, $conexao) or die('ERRO ao inserir registro no Banco');
}

    if ( $queryExec) {
        echo "GRAVADO COM SUCESSO";     
    } else {        
        echo "ERRO NA GRAVAÇÃO DO KIT";
    }
    
02.08.2017 / 00:16