Updating with multiple select <option selected="selected">

时间:2017-12-18 05:51:45

标签: php html mysql arrays multiple-select

I have a form where a user can update several data inside the database and one of the fields is using a multiple select. I'm stuck with the selected part, where the system will display back the keyword values from the database in the option. The updating process works fine, the problem is only to display the existing values from the database inside the multiple select options.

<form action="" method="post" enctype="multipart/form-data">
    <?php
    if(isset($_GET['edit'])) {
        $id = $_GET['edit'];
        $query = "SELECT * FROM record WHERE id = $id";
        $select_id = mysqli_query($con,$query);
        while($row = mysqli_fetch_assoc($select_id)){
            $keywords = explode (', ', $row['record_keywords']) ;
            ?>
            <label class="control-label">Keywords</label>
            <select multiple name="keywords[]" type="next" class="selectpicker" data-style="btn btn-default btn-block">
                <?php
                $query = "SELECT * FROM key";
                $select_categories = mysqli_query($con,$query);
                while($row = mysqli_fetch_assoc($select_categories)) {
                    $kk = $row['key'] == $keywords;
                    echo '<option '.($kk ? 'selected="selected"' : '').'>' . $row['key'] . '</option>';
                } ?>
            </select>

            <button class="btn btn-warning btn-fill btn-wd btn-finish pull-right" name="update" type="submit">Update</button>
            <?php
        }//end of while
    }//end of if
    ?>
</form>

0 个答案:

没有答案