INSERT php function in mysql database(checkbox)

Asked

Viewed 70 times

0

I’m having trouble making several INSERTS.

Problem: I am developing system that registers tag employee on a table but I’m not able to do everything at once.

Example: I would like to register a name of a person, your company and tag name in a table on mysql example:

(Mateus,company,php)

(enterprise, enterprise, enterprise, java)

(Lucas,company,php)

(Ucas, enterprise, java)

how would INSERT be in this situation

<form name="form" method="post" action="form.php">
<p>
<label for="nome">Nome:</label>
<input type="text" id="nome" />
</p>
<p>
<label for="empresa">empresa:</label>
<input type="text" id="empresa" />
</p>
<p>
<label>php</label>
<input type="checkbox" name="genero[]" value="php"/>
</p>
<p>
<label>java</label>
<input type="checkbox" name="genero[]" value="java"/>
</p>
<p>
<label>mysql</label>
<input type="checkbox" name="genero[]" value="mysql"/>
</p>
<p>
<label>Terror</label>
<input type="checkbox" name="genero[]" value="terror"/>
</p>
<input type="submit" name="submit" value="Enviar"/>
</form>

php form.

foreach ($_POST['genero'] as $value) {

}

1 answer

1


First that is missing the name in your Name and Business inputs, you should fix this. Then you can create a simple inserts and execution of querys:

$nome = $_POST['nome'];
$empresa = $_POST['empresa'];
foreach ($_POST['genero'] as $value) {
    $query = 'INSERT INTO tabela(nome, empresa, tag) VALUES({$nome}, {$empresa}, {$value})';
    //ou
    //$query = 'INSERT INTO tabela(nome, empresa, tag) VALUES('.$nome.', '.$empresa.', '.$value.')';
    // executa a query e vai para o próximo registro...
}

Browser other questions tagged

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