PHP 8 Checkbox Set to Check Based on Database Value Example


Share:
 | Published By: Admin | Published On: May 26, 2022 | Category: PHP 8


Hello Friends,

In this article we will cover on how to implement PHP 8 Checkbox Set to Check Based on Database Value Example. I explained simply step by step Get checked Checkboxes value with PHP 8 MySQL. it's simple example of PHP Checkbox Checked from Database Example. We will use Insert Checkbox Value in Database Using PHP 8.

I will give you simple example of How To Save Checkbox Value In Database Using PHP 8.

So, let's see bellow solution:

index.php
<!doctype html>
<html lang="en">
<head>
    <!-- Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    <title>PHP 8 Checkbox Set to Check Based on Database Value Example - Mywebtuts.com</title>
</head>
<body>
    <?php 

        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass = '';
        $db = 'shope';

        $conn = mysqli_connect($dbhost, $dbuser, $dbpass , $db) or die($conn);  

        if(isset($_POST['submit'])){

            if(!empty($_POST['lang'])) {

                $lang = implode(",",$_POST['lang']);

                // Insert and Update record
                $checkEntries = mysqli_query($conn,"SELECT * FROM languages");
              
                if(mysqli_num_rows($checkEntries) == 0){
                    mysqli_query($conn,"INSERT INTO languages(language) VALUES('".$lang."')");
                }else{
                    mysqli_query($conn,"UPDATE languages SET language='".$lang."' ");
                }
            }
        }        

    ?>

    <div class="container mt-5">
        <div class="row">
            <div class="col-md-12">
                <div class="card w-75 m-auto">
                    <div class="card-header bg-info text-white text-center">
                        <h4>PHP 8 Checkbox Set to Check Based on Database Value Example - Mywebtuts.com</h4>
                    </div>
                    <div class="card-body">
                        <form method="post" action="">
                            <?php

                                $checked_arr = array();

                                // Fetch checked values
                                $fetchLang = mysqli_query($conn,"SELECT * FROM languages");
                                if(mysqli_num_rows($fetchLang) > 0){
                                  $result = mysqli_fetch_assoc($fetchLang);
                                  $checked_arr = explode(",",$result['language']);
                                }

                                // Create checkboxes
                                $languages_arr = array("PHP","JavaScript","jQuery","AngularJS");
                                
                                foreach($languages_arr as $language){

                                    $checked = "";
                                    if(in_array($language,$checked_arr)){
                                        $checked = "checked";
                                    }

                                    echo '<div class="form-check">
                                            <input type="checkbox" name="lang[]" class="form-check-input" style="width: 20px;height: 20px;left: 10px;top: -1px;" value="'.$language.'" '.$checked.' > '.$language.'
                                          </div><br> ';
                                }
                            ?>
                            <hr>
                            <input type="submit" value="Submit" name="submit" class="btn btn-primary">
                        </form>  
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
Output:

It will help you...
Share: