How to save checkbox in database?

Asked

Viewed 98 times

2

My doubt is very simple, but I did not find exactly the command I want and I hope you can help me.

I have a vehicle budget page and I want to do a checkbox to select between three models of wheels and I want this to save in the database in the table orçamento as idroda.

I have the bank already created, and the table where I want to save the id of checkbox would be chosen orcamento.

So my goal would be first to save the checkbox in the database.

Just follow my code.

<form action="" method="post" name="checkbox"/>
<div id="ola">
    <label>
        <input type="checkbox" name="ba[]" value="opção 1"/>opção 1<br />
    </label>
    <label>
        <input type="checkbox" name="ba[]" value="opção 2"/>opção 2<br />
    </label>
    <label>
        <input type="checkbox" name="ba[]" value="opção 3"/>opção 3<br />
    </label>
    <input type="hidden" value="enviar" name="acao"/>
    <input type="submit" value="Enviar"/>
</div>
</form>

PHP

if(isset($_POST["ba"])) 
{ 
    echo "você escolheu:<br>"; 
}


if (isset ($_POST['acao']) && $_POST['acao'] == "enviar"){

    if(!empty($_POST['ba'])){
        $campo = $_POST['ba'];
        foreach($campo as $valor){
            echo $valor.'<br/>';
        }
    }
    else{
        echo "Nada marcado";
    }
}
  • Do you already have a database created? Are you used to working with SQL? If you have the database explain which table you want to enter the information.

  • 3

    Do you want me to save the 3 ids in a single record or each id in a different record? If you have the bank structure, ask the question

  • In place of echo do the Insert in the budget table.

  • Welcome to Stack Overflow, Bruno! Please invest a minute of your time by doing the tour, to learn more about how the site works. I also suggest that you respond to requests for information from other users if you want a good answer to your question (they don’t get to help you!).

No answers

Browser other questions tagged

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