How to fetch multiple values in checkbox and get data to store in mysql using php

Whatsapp

https://www.fundaofwebit.com/post/how-to-fetch-multiple-values-in-checkbox-and-store-in-mysql-using-php

How to fetch multiple values in checkbox and store in database in PHP MySQL 


In this post, we are going to learn how to fetch multiple values in checkbox and then these input checkbox values to insert into mydql database using php mysql.

First create a html form where we will how to fetch multiple data into multiple checkbox in php and then insert multiple checkbox values in mysql using php.

I have used Bootstrap v5 to design the user interface.

So, Lets get started to: (How to insert multiple checkbox data in mysql database using php and how to fetch multiple values in checkbox using php mysql)

Note: For better and detailed understanding. please watch out the Video above.


Step 1: Create a table brands into your Database (MySQL) as follows (To Fetch all products list):

CREATE TABLE `a_brands` (
    `id` int(11NOT NULL,
    `name` varchar(191NOT NULL,
);

Step 2: Create a index.php file and paste the below html form design code: (where the fetch data in multiple checkbox in php)

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Funda of Web IT</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">

                <?php  
                if(isset($_SESSION['status']))
                {
                    echo "<h4>".$_SESSION['status']."</h4>";
                    unset($_SESSION['status']);
                }
                ?>
                <div class="card mt-5">
                    <div class="card-header">
                        <h4>How to Get Multiple Values from DB in Checkbox in php</h4>
                    </div>
                    <div class="card-body">

                        <form action="code.php" method="POST">
                        <?php
                            $con = mysqli_connect("localhost","root","","phptutorials");

                            $brand_query = "SELECT * FROM a_brands";
                            $query_run = mysqli_query($con$brand_query);

                            if(mysqli_num_rows($query_run) > 0)
                            {
                                foreach($query_run as $brand)
                                {
                                    ?>
                                    <input type="checkbox" name="brandslist[]" value="<?= $brand['name']; ?>" /> <?= $brand['name']; ?> <br/>
                                    <?php
                                }
                            }
                            else
                            {
                                echo "No Record Found";
                            }
                        ?>
                            <div class="form-group mt-3">
                                <button name="save_multicheckbox" class="btn btn-primary">Save Multiple Checkbox</button>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Step 3: Create a table named demo into your Database (MySQL) as follows (To insert all products list you have checked in your checkbox):

CREATE TABLE `demo` (
    `id` int(11NOT NULL,
    `name` varchar(191NOT NULL,
);

Step 4: Create a code.php file and paste the below code to insert multiple data into multiple checkbox in mysql database in php.

<?php
session_start();
$con = mysqli_connect("localhost","root","","phptutorials");

if(isset($_POST['save_multicheckbox']))
{
    $brandlist = $_POST['brandslist'];
    foreach($brandlist as $branditems)
    {
        // echo $branditems."<br>";
        $query = "INSERT INTO demo (nameVALUES ('$branditems')";
        $query_run = mysqli_query($con$query);
    }

    if($query_run)
    {
        $_SESSION['status'] = "Inserted Successfully";
        header("Location: index.php");
    }
    else
    {
        $_SESSION['status'] = "Not Inserted";
        header("Location: index.php");
    }
}
?>



Thanks for reading...

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT