2014-11-14 48 views
2

從多選擇如何插入數據在MySQL從多選擇在MySQL中如何插入數據

或作出這樣的字符串數組 - > ...

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

出來放像 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 

不是陣列

回答

0
被發現

這應該是訣竅:

$arr =array('a'=>'1','b'=>'3','c'=>'5'); 
echo implode(",", array_values($arr)); 
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