vendredi 24 novembre 2017

Failing to send checkbox value to MySQL database?

I'm trying to store a Boolean value in my database based off a checkbox(Watched) in a form. If the checkbox isn't checked on form submit, it should send a 0; Otherwise a 1.

However, it sends a value of 0 no matter if it's checked or not. The Title Field and Genre field send perfectly, however the Watched conditional is not working correctly. I'm stumped and having a hard time wrapping my head around this, any thoughts?

<?php 

    require 'config/config.php';
    require 'config/db.php';

    // Check for Submit
    if(isset($_POST['submit'])) {
        // Get Form Data
        $title = mysqli_real_escape_string($conn, $_POST['title']);
        $genre = mysqli_real_escape_string($conn, $_POST['genre']);
        if (isset($_POST['watched']) && ($_POST['watched'] == "value")) {
            $watched = 1;
        } else {
            $watched = 0;
        }

        $query = "INSERT INTO movies(name, genre, watched) VALUES('$title', '$genre', '$watched')";

        if (mysqli_query($conn, $query)) {
            header('Location: '.ROOT_URL.'');
        } else {
            echo "ERROR: " . mysqli_error($conn);
        }
    }

?>

                <form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">

                    <div class="md-form">
                        <input type="text" id="form1" class="form-control" name="title">
                        <label for="form1" class="">Title</label>
                    </div>

                    <div class="md-form">
                        <input type="text" id="form1" class="form-control" name="genre">
                        <label for="form1" class="">Genre</label>
                    </div>

                    <div class="form-group">
                        <input type="checkbox" id="checkbox1" name="watched">
                        <label for="checkbox1">Have you watched it already?</label>
                    </div>

                    <input type="submit" name="submit" value="submit" class="btn btn-primary">

                </form>




Aucun commentaire:

Enregistrer un commentaire