如何在mysql中插入多选数据

时间:2014-11-14 01:26:59

标签: php mysql multi-select

如何从mysql中的多选中插入数据

或使数组字符串像 - > ...

 $arr =array('a'=>'1','b'=>'3','c'=>'5');

out out like 1,3,5 将所有值插入一列中 $ sth = $ this-> db-> prepare(" UPDATE data SET category = $ arr where id = 1");

                <div class="control-group">
                    <label for="area" class="control-label">Rank</label>
                    <div class="controls">
                        <input type="number" name="rank" id="area" value="<?= $this->detali->rank ?>" />
                    </div>
                </div>





                <div class="control-group">
                    <label for="artist " class="control-label">Artist</label>
                    <?php
                    $art = explode(',', $this->detali->artist);
                    ?>
                    <div class="controls">
                        <select multiple="multiple" id="my-select" name="artist[]" class='multiselect'>
                            <?php 
                            foreach ($this->artist as $artist) { ?>
                                <option value="<?= $artist->artist_id ?>" <?php

                                            foreach ($art as $value) {

                                if (!(strcmp("$artist->artist_id", htmlentities($value, ENT_COMPAT, 'utf-8')))) {
                                    echo "SELECTED";
                                }                                                    

                                            }

                                ?>><?= $artist->artist_name ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>                    

如何从$_POST['artist'] (artist[])

获取数据
like  1,5,6

不是数组

2 个答案:

答案 0 :(得分:1)

如果我理解正确,你想要在数组中获取数据,如

array(3) {
    [0] => 'one',
    [1] => 'two',
    [2] => 'three'
}

并将其输出为

之类的字符串
one,two,three

如果是这种情况,您可以使用PHP的implode函数:

<?php
    $my_array = array('one', 'two', 'three);
    $my_string = implode(',', $array);
?>

implode的文档可在http://php.net/manual/en/function.implode.php

找到

答案 1 :(得分:0)

这应该可以解决问题:

$arr =array('a'=>'1','b'=>'3','c'=>'5');
echo implode(",", array_values($arr));