Home » Php » php – How can I insert data to MySQL from a checkbox form

php – How can I insert data to MySQL from a checkbox form

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to update my database with a 1 or 0 for checked or not checked.
So I have three columns called: csgoactive, faceitactive, rlactive

I’m currently stucked with the sql part…
So how can I insert a 1 now, if the box is checked and a 0 if the box is not checked?

Thats my current code:

<?php 
    $sqlread = "SELECT * FROM Users WHERE SteamID64='".$steamprofile['steamid']."'";
    $query = mysqli_query($db, $sqlread);
    $fetch = mysqli_fetch_assoc($query);
?>

<form method="post" action="">
    <div style="display: inline-block; text-align: left;">
        <?php
        if($fetch[csgoactive] == 1) {
            $csgocheck = "checked";
        } else {
            $csgocheck = "";
        }
        if($fetch[faceitactive] == 1) {
            $faceitcheck = "checked";
        } else {
            $faceitcheck = "";
        }
        if($fetch[rlactive] == 1) {
            $rlcheck = "checked";
        } else {
            $rlcheck = "";
        }
        echo '<label class="switch"><input type="checkbox" name="csgo" value="csgo"' . $csgocheck . '><span class="slider"></span></label> CS:GO <br>';
        echo '<label class="switch"><input type="checkbox" name="faceit" value="faceit"' . $faceitcheck . '><span class="slider"></span></label> FACEIT <br>';
        echo '<label class="switch"><input type="checkbox" name="rl" value="rl"' . $rlcheck . '><span class="slider"></span></label> Rocket League <br>';
        ?>
    </div>
    <br>
    <input type="submit" value="Save" class="btn btn-primary" name="submit">
</form>
How to&Answers: