Doubt: How to insert multiple lines using $_GET and Mysqli from the same.php file to the same table

Asked

Viewed 35 times

-2

Speak cooler! I need a help.

I’m creating some screens with PHP 7.4 + Mysqli + Apache, running on an Ubuntu 18.04.

I have a file/script registro_serra.php, in it I capture variables to insert in the table corte_torete with the method $_GET a Form. As follows:

<form action="registro_serra.php" method="GET">
    <div class="form-group">
    <label>Comprimento do Torete 1</label>
    <select class="form-control text-center" id="comprimento_torete" name="comprimento_torete"> 
      <option value="11">11 Centímetros</option> 
      <option value="">...</option> 
      <option value="20">20 Centímetros</option>
    </select>

I’m able to capture these variables and insert them into B.D. As follows:

if(isset($_GET['btn-gravar-torete1'])):
// Gravando na tabela
$gravatt = mysqli_query ($connect, "INSERT INTO corte_torete (nro_lote...volume_torete) VALUES ('$lote3'...'$volume_tt')");
mysqli_close($connect);

In this same file, I need to capture the same values to insert into the same table. In another set identified as name 'torete2'.

    <div class="form-group">
    <label>Comprimento do Torete 2</label>
    <select class="form-control text-center" id="comprimento_torete_2" name="comprimento_torete_2"> 
      <option value="11">11 Centímetros</option> 
      <option value="">...</option> 
      <option value="20">20 Centímetros</option>
    </select>

Send it to B.D the same way

if(isset($_GET['btn-gravar-torete_2'])):
// Gravando na tabela
$gravatt_2 = mysqli_query ($connect, "INSERT INTO corte_torete (nro_lote...volume_torete) VALUES ('$lote3_2'...'$volume_tt_2')");
mysqli_close($connect);

Both insert cute, but only 1 at a time, I need to insert up to 9 times so, on the same page, for the same table. If I delete the record by Phpmyadmin, it allows a new insertion... I tried with $_POST, it’s still. Any suggestions?

I enabled to show the errors, and when it does not write, returns Error: Query was Empty

Thanks! It’s my first post here :)

1 answer

0

Let’s make a small change in your HTML to be better to control how many records there are to perform data insertion in the table.

  • In the name Torete Length select will be of best use, vector implementation to store your data, getting all the names as name="comprimento_torete[]".

With the parameter being passed as an array or array as you want to call it, you get even more to retrieve the data.


  • Form

    <form action="registro_serra.php" method="GET">
        <div class="form-group">
            <label>Comprimento do Torete 1</label>
            <select class="form-control text-center" id="comprimento_torete" name="comprimento_torete[]"> 
                <option value="11">11 Centímetros</option> 
                <option value="...">...</option> 
                <option value="20">20 Centímetros</option>
            </select>
        </div>
        <div class="form-group">
            <label>Comprimento do Torete 2</label>
            <select class="form-control text-center" id="comprimento_torete2" name="comprimento_torete[]"> 
                <option value="11">11 Centímetros</option> 
                <option value="...">...</option> 
                <option value="20">20 Centímetros</option>
            </select>
        </div>
        <div class="form-group">
            <label>Comprimento do Torete 3</label>
            <select class="form-control text-center" id="comprimento_torete3" name="comprimento_torete[]"> 
                <option value="11">11 Centímetros</option> 
                <option value="...">...</option> 
                <option value="20">20 Centímetros</option>
            </select>
        </div>
        <input type="submit" value="Enviar">
    </form>
    
  • registro_serra.php

    $c_toretes = $_GET['comprimento_torete']; // recupera array com os comprimentos
    $sql = ""; // Cria variável onde será implementado a inserção
    // Criando query se inserção 
    foreach($c_toretes as $key => $c_torete){
        $sql .= "('$c_torete')"; // valor da coluna para inserção
        $sql .= count($c_toretes) === $key + 1 ? "" : ","; // Define virgula para separar os parametros, caso seja o último item do array, não será implementado a virgula
    }
    // query se inserção
    mysqli_query ($connect, "INSERT INTO TABELA (COLUNA_COMPRIMENTO_TORETE) VALUES {$sql}");
    mysqli_close($connect);
    
    // Em teste realizado, a saída a query foi 
    "INSERT INTO TABELA (COLUNA_COMPRIMENTO_TORETE) VALUES ('11'),('...'),('20')"
    

Some consideration

As a query is being performed with the data recovered from a form, it is very risky to use the method GET for this, it is recommended the method POST, that becomes a little safer.

A second consideration would be the validation of the data before using it for anything, one of the most common for validation would be the filter_var(), as it in addition to validate the data it is possible to wipe the data too, using SANITIZE and VALIDADE.

  • Thanks for the help Pedro, in the meantime I managed to solve! : D One of the changes was to use $_POST instead of $_GET The other was to implement other "debug" functions as an example code if ($connect->multi_query($gravatt2) === TRUE) { echo "Recorded."; } Else { echo "Error: " . $gravatt2 . " " . $connect->error; } code So I discovered that I was trying to insert a duplicate value into a field defined as "Primary" and "auto increment", I removed these properties, so B.D allowed me to insert duplicate them...

  • @Jonathanwolffandrade, how nice that you managed to solve yourself, good studies!

Browser other questions tagged

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