'How to insert data from multiple select dropdown values into database?

I have a multiple select dropdown with checkbox. But I am not sure how to insert it into database and the next time the page opens, it displays the current value from the database.

How can I insert values from my multiple select dropdown into database and display it as checked?

 <div class="form-group">
            <div align="left">
              <div class="formlist">Purchase Group :
                <select id="purchase_group" name="purchase_group" class="form-control" multiple="multiple">
                    <?php 
                        $query_pgr = "SELECT * FROM purchasing_group WHERE pgr_enable=1 ORDER BY pgr_name";
                        $rs_pgr = DB_Query($query_pgr);
                        
                        while ($row_pgr = DB_FetchRow($rs_pgr)) {
    
                            $pgr.='<option value='.$row_pgr["pgr_id"].'>' .$row_pgr["pgr_name"].' </option>';
                            
                            }   
                        
                        mysql_free_result($rs_pgr);
                        echo $pgr;
                    ?>
                     
                </select>
              </div>
            </div>
          </div>

this is the display of my multiple dropdown.

$('#btnSelected').click(function () {
                var selected = $("#purchase_group option:selected");
                var message = "";
                selected.each(function () {
                    message += $(this).text() + " " + $(this).val() + "\n";
                });
                alert(message);
            });
php


Solution 1:[1]

Every multiple dropdown plug-in hides the <select> with their own dropdown, what ever selection you do can be fetched in php like:

$selection = $_REQUEST['purchase_group'];

here $selection is an array you can use foreach() to get its values and insert it into database.

Note: To hold multiple values, the name of the <select> must be an array like:

<select id="purchase_group" name="purchase_group[]" class="form-control" multiple="multiple">

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mayank Pandeyz