Insert mysql database

Asked

Viewed 40 times

-1

Hello!

The first php code generates several inputs. Where the user informs how many inputs he wants in the $value variable.

for ($i=1; $i <=$valor ; $i++) { 

                echo "
                <label>Chamado $i:</label><br>
                <input type='number' name='valor %i' placeholder='Chamado'>
                <br><br>
                ";

              } 

so I would like to know how to do the Insert based on the amount the user entered?

2 answers

0

Probably it will fill the variable at another time, this time can be a form sent via post or via get, can be hard-code as I did below, you can test the functioning on the site : http://phptester.net/

<?php 


// $valor = $_POST['valor']; 

// $valor = $_GET['valor']; 

$valor = 10; 

for ($i=1; $i <=$valor ; $i++) { 
            echo "
            <label>Chamado $i:</label><br>
            <input type='number' name='valor %i' placeholder='Chamado'>
            <br><br>
            ";

          } 

0


If you will receive the variable $valor front-end or similar where the user will enter the value and give a Ubmit, you can do the following:

$valor = sizeof($_POST['valor']); // ou $_GET ( caso tenha que percorrer a quantidade de elementos).

ou

$valor = $_POST['valor']; // ou $_GET (caso só tenha que receber o valor digitado pelo usuário em um input)

for ($i = 0; $i < $valor; $i) {
   $valor1 = $_POST['nome'][$i];
   $valor2 = $_POST['telefone'][$i];

   $query = "INSERT INTO database VALUES ('$valor1' , '$valor2')";
}

I made an example for you creating some variables, because I do not know what you are sending to your script / bank, so in case you would put yours there instead. In short you check the amount of the value by taking the sizeof of the variable if it has to go through the existing quantity, or only you receive it as in the second parameter via $_POST according to what the user type.

Browser other questions tagged

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