简体   繁体   中英

Select from database in a multiselect and then insert as an array

I used this code to select classes for students from the database in a dropdown. It appeared correctly. Then I when I realized that I actually want to select more than one class for a student, I added multiple. It showed as a multiple but still inserts only one value in database table.

<?php
require_once('../config.php');
$class_result = $conn->query('select * from class');
?>
        <label for="exampleFormControlTextarea1">Class</label>
        <select class="form-control" name="class[]" id="class" multiple>
            <option value=""></option>
            <?php
            if ($plot_type_result->num_rows > 0) {
                // output data of each row
                while ($row = $plot_type_result->fetch_assoc()) {
            ?>
            <option value="<?php echo $row["class"]; ?>">
                <?php echo $row["class"]; ?>
            </option>
            <?php
                }
            }
            ?>
        </select>

Now I want this select class to be a multi select. If I add multiple it becomes a multiple but doesn't really inserts the multiple values I select. Only selects the last selected from the database and inserts it as a single one. How can I achieve multi select and actually pass it as an array to the database with more than one value?

$classDatas= implode(", ", $datas);

You can try to save the array in a single column by converting it to text with implote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM