Save array values to Mysql

Asked

Viewed 1,028 times

0

I am trying unsuccessfully to record some information in my database, I have a table where the user will insert lines according to their need, but when sending the information to the database 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 insertion 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";
    }
  • Remembering that the functions of type mysql_ have already been discontinued in PHP 5.5, and no longer work in PHP 7+ Why we should not use mysql type functions_

  • 1

    Hello @Knautiluz, thanks for the tip, this is a system I’m redoing and was testing if everything was working, thanks.

1 answer

2


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

No need to place the connection string with the bank inside the loop for, just declare it once before the for.

The connection line $queryExec you can put inside the loop for, because then you record each information in the loop.

The code would look 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";
    }
  • Hello @David Sam, thank you for making me see this monstrous wobble.

Browser other questions tagged

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