How can I save checkbox in the database with php?

Asked

Viewed 4,981 times

1

I have several checkbox components on the site, each with the value of a movie name, how can I save in the bank?

HTML of one of them:

 <input type="checkbox" class="checkbox" name="filme">

Do you have to have value or name different from each other? Movie checkbox type Origin and other Insurgent movie?

is for the user to save and later see the movies he has already watched, so it has to be marked

  • What is the purpose of the user to mark a particular movie? Location?

  • it’s like saving the movies he’s already seen, forgot to put that kkkk

  • Why not do a foreach() to add the fields in the database? then just do an isset() to check if there is in the database, if you have, mark as checked...

  • I don’t even know what that kkk can give me an example?

1 answer

1

You need to put something like filme[] in the name of checkboxes, and then do a foreach, as said by @Andrébaill.

Something like that:

<input type="checkbox" name="filme[]" value=filme_1>
<input type="checkbox" name="filme[]" value=filme_2>
<input type="checkbox" name="filme[]" value=filme_3>
<input type="checkbox" name="filme[]" value=filme_4>

And then in your php code:

<?php

if(isset($_POST['filme'])){
    $listaCheckbox = $_POST['filme'];

    foreach ($listaCheckbox as $filme) {
        echo $filme;
        //aqui você salva no seu banco
    }
}

?>

Remembering that before using a value coming from a POST, you must first validate it for security reasons, especially if you are going to save this data in the database. Take a look at the method filter_input.

Browser other questions tagged

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