Register in the mysql database an array

2

I would like to know if it is possible to insert multiple values inside a variable in html, pass via POST to php and register each one in a row in the database.

For example, enter multiple names through the name="name" field.

add everything, send pro php and insert one in each line. It would need a way that I can add more things and it does not mix. If I wanted to add a surname, age and sex, and at the time of registering, I would add a whole line and then go to another one.

<html>
<form action = "cadastro/cadastrando.php" method="POST">

  <tr>

    <td>Nome:
    <label for="nome"></label></td>
    <td height="33"><input type="text" name="nome"  size="30" required/></td>
    <td width="267">

        </tr>
    <tr>
        <td>Nome 2:
    <label for="nome"></label></td>
    <td height="33"><input type="text" name="nome"  size="30" required/></td>
    <td width="267">
        </td>
        </tr>
        <td>Nome:
    <label for="nome"></label></td>
    <td height="33"><input type="text" name="nome"  size="30" required/></td>
    <td width="267">
    </td>

     <br>
     <br>

    <center><button type="submit"  >Cadastrar <span class=""></span></button></center>

    <br>
    </form>
</html>
    
asked by anonymous 29.08.2017 / 02:44

1 answer

2

From the sim, you first need to use html so

<input type="text" name="nome[]"  size="30" required/>

Then you can use for or foreach to move through the array

for($i = 0; $i < count($_POST['nome']); $i++){
   $sql = $pdo->prepare("INSERT INTO tabela SET nome = ?");
   $sql->execute($_POST['nome'][$i]);
}
    
29.08.2017 / 03:01