Registering form data with inputs of equal PHP names

Asked

Viewed 240 times

0

I find myself at:

  • implement a form
  • has several inputs with the same name
  • I need to enter the data into the database

I intend at the moment:

  • create a record for each user of the platform
  • the form has several fields linking to the various entries of the database

    <div class="form nome_ch">
        <label for="nome_ch">Nome</label>
        <input type="text" name="nome_ch[]" id="nome_ch">
    </div>
    <div class="form cargo_ch">
        <label for="cargo_ch">Cargo</label>
        <input type="text" name="cargo_ch[]" id="cargo_ch">
    </div>
    <div class="form telefone_ch">
        <label for="telefone_ch">Telefone</label>
        <input type="text" name="telefone_ch[]" id="telefone_ch">
    </div>
    <div class="form email_ch">
        <label for="email_ch">E-mail</label>
        <input type="text" name="email_ch[]" id="email_ch">
    </div>
    <div class="form nascimento_ch">
        <label for="nascimento_ch">Nascimento</label>
        <input type="text" name="nascimento_ch[]" id="nascimento_ch">
    </div>
    

I don’t know how I do:

  • enter in the database

There are several form parameters that connect the variables or parameters with identical names

  • In php you take the $_POST['name_ch'] data, it will be an array as sent in the html name="name_ch[]". It is OK to have several inputs with the same name if they have indicated as array in html by symbol [].

  • yes, but my doubt is that there are 5 fields and not only 1

  • So that’s exactly it, if you send 2 inputs with name="name_ch[]", same names, you’ll get the 2 inputs in php by taking them for $_POST['name_ch'], and you’ll get an array with the 2 elements.

1 answer

0


Hello, we can do it this way:

ex:

<div class="form nome_ch">
    <label for="nome_ch">Nome</label>
    <input type="text" name="data[nome_ch]" id="nome_ch">
</div>

...

<div class="form nascimento_ch">
    <label for="nascimento_ch">Nascimento</label>
    <input type="text" name="data[nascimento_ch]" id="nascimento_ch">
</div>

Php control

<?php 

print_r($_POST['data']);

Isso fara com quer passe as informações pelo único vetor.


Logo apos crie a condição de banco

$servidornome = "localhost";
$usuario = "username";
$senha = "password";

$conn = new mysqli($servidornome, $usuario, $senha);


if ($conn->connect_error) {
    die("Conexão falhou: " . $conn->connect_error);
} 

echo "conctado sucesso";

$sql = "INSERT INTO sua tabela (nome_ch ,... , nascimento_ch)
VALUES ('{$_POST['data']['nome_ch']}', ... , '{$_POST['data']['nascimento_ch']}')";

if ($conn->query($sql) === TRUE) {
    echo "inserido com sucesso";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

Browser other questions tagged

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