2013-08-19 4 views
0

데이터베이스에 여러 개의 선택 값을 삽입하려고합니다.데이터베이스에 여러 개의 선택 값을 삽입하는 방법은 무엇입니까?

HTML

<select style="width:175px" id="first" multiple="true"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
    <option value="4">4</option> 
    <option value="5">5</option>   
</select> 
</div> 

<div class="mid"> 
<button type="button" class='add'> > </button> 
<button type="button" class='remove'> < </button> 
<button type="button" class='add-all'> >>> </button> 
<button type="button" class='remove-all'> <<< </button> 
</div> 

<div class="end"> 
<select style="width:175px" id="second" name="second[]" multiple="true"> 
</select> 
</div> 

<div style="clear:both;"></div> 

자바 :

$('.add').click(function(){ 
$('#first option:selected').appendTo('#second'); 
}); 
$('.remove').click(function(){ 
$('#second option:selected').appendTo('#first'); 
}); 
$('.add-all').click(function(){ 
$('#first option').appendTo('#second'); 
}); 
$('.remove-all').click(function(){ 
$('#second option').appendTo('#first'); 
}); 

PHP (PHP 코드의 그것의 단지 작은 부분)

$formvars['second'] = $this->Sanitize($_POST['second']); 
$insert_query = 'insert into 'comments'(second) values ("' . $this->SanitizeForSQL($formvars['second']) . '",)'; 

그 라이트 박스 만 단어 "배열"을 삽입 데이터 베이스. 어떤 생각?

+0

코드 $ this-> SanitizeForSQL ($ formvars [ 'second'])이 배열을 반환하고이 방법으로 삽입 할 수 없기 때문입니다. 일괄 삽입 쿼리를 찾습니다. –

답변

0
var selectArr = []; 

$('#first option').each(function() { 
    selectArr.push($(this).val()); 
}); 

이제 selectArr을 서버에 전달한 다음 얻은 것을 확인할 수 있습니다. 이 배열을 삽입이라면

0

, 당신은 루프가 하나의 문자열의 모든 값에 가입하기를 통해 그것을 반복해야합니다

<?php 
    $toInsert = ''; 
    foreach($_POST['second'] as $p){ 
     $toInsert = $toInsert . $p; 
    } 
?> 

$ toInsert 문자열을 사용하면 DB로 작성해야 하나입니다.

관련 문제