How to insert multiple dropdown list/select option values in PHP MySQL

Whatsapp

https://www.fundaofwebit.com/post/how-to-insert-multiple-dropdown-list-select-option-values-in-php=mysql

How to insert multiple dropdown list/select option values in PHP MySQL


In this post, you will be lerarning about how to insert muliple drop down list values in mysql database each row using php, so, need to create HTML form with select option tags with submit button, as html form design is given below in step 2: for insert multiple select option values in php mysql.

I have used Bootstrap v5 to design the user interface.

So, Lets get started to: insert multiple select option in each row of mysql database

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

Step 1: Create a table named demo into your database (MySQL) as follows:

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

Step 2: Create a index.php file and paste the below html form design code:

<?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-10">
                <?php 
                    if(isset($_SESSION['status']))
                    {
                        ?>
                            <div class="alert alert-warning alert-dismissible fade show" role="alert">
                            <strong>Hey!</strong> <?php echo $_SESSION['status']; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                            </div>
                        <?php
                         unset($_SESSION['status']);
                    }
                ?>
                <div class="card mt-5">
                    <div class="card-header">
                        <h4>How to Insert Multiple Select Option (Dropdown List) Values into DB in php</h4>
                    </div>
                    <div class="card-body">
                          
                        <form action="code.php" method="POST">
                            <div class="form-group mb-3">
                                <label for="">Brands</label>
                                <select name="brandlist[]" multiple class="form-control">
                                    <option value="Red Mi">Red Mi</option>
                                    <option value="Samsung">Samsung</option>
                                    <option value="Nokia">Nokia</option>
                                    <option value="Vivo">Vivo</option>
                                    <option value="Asus">Asus</option>
                                    <option value="Lenovo">Lenovo</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <button type="submit" name="save_multi_select" class="btn btn-primary">Save Data</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 code.php file and paste the below code to insert multiple dropdown list or select option values  in mysql database in php.

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

if(isset($_POST['save_multi_select']))
{
    $brands = $_POST['brandlist'];

    foreach($brands as $item)
    {
        // echo $item."<br>";
        $query = "INSERT INTO demo (nameVALUES ('$item')";
        $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