I only get a checkbox in my database [closed]

-1

I'm making a form with PHP and HTML where when tagging several checkbox should I get them and insert into my database.

But I can only receive one, for example, if I tick 2% with% I will only receive the last chekbox selected, what should I do to get all those selected?

My checkbox :

<form method="POST" action="cadastra.php">
  <input type="checkbox" name="situacao" value="bom" />bom
  <input type="checkbox" name="situacao" value="regular" />Regular
  <input type="checkbox" name="situacao" value="inrregular" />Inrregular
  <input type="submit" value="Cadastrar" />
</form>

My form.php :

$situacao = $_POST['situacao'];
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "form";

$conn = mysqli_connect($servidor, $usuario, $senha, $dbname); // faz a conexao

//INSERT INTO FORMULARIO SITUACAO = 
//ESCOLHE A TABELA SITUAÇAO  DA TABELA FORMULARIO
//VALUES('$SITUACAO') = RECEBE OS VALORES QUE TÊM  O NAME SITUAÇAO

$result_formulario = "INSERT INTO formulario(situacao) VALUES ('$situacao')"; 
$resultado_formulario = mysqli_query($conn, $result_formulario);

    
asked by anonymous 16.04.2018 / 15:15

1 answer

2

To register all the fields marked, you must leave the inputs as arrays , and then go through the variable to insert each one.

Follow the modifications made to your code:

HTML

  <input type="checkbox" name="situacao[]" value="bom" />bom
  <input type="checkbox" name="situacao[]" value="regular" />Regular
  <input type="checkbox" name="situacao[]" value="inrregular" />Inrregular

PHP

foreach($situacao as $s) {
    $result_formulario = "INSERT INTO formulario(situacao) VALUES ('$s')";
    $resultado_formulario = mysqli_query($conn, $result_formulario);
}
    
16.04.2018 / 16:02