Register an array in the mysql database

Asked

Viewed 346 times

2

I wonder if it is possible to insert several values within 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".

Put everything together, send php and insert one into each line. It would take a way for me to add more things and it doesn’t mix. If I wanted to add last name, age and gender, and when registering to register an entire line to then go to another.

<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>

1 answer

2


Da yes, first Voce needs to use html like this

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

Then Voce can use one for or foreach to browse the array

for($i = 0; $i < count($_POST['nome']); $i++){
   $sql = $pdo->prepare("INSERT INTO tabela SET nome = ?");
   $sql->execute($_POST['nome'][$i]);
}
  • Cara didn’t even know this existed, out of curiosity how is identified the beginning and end of a name in the input?

Browser other questions tagged

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