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>

Javascript:

$('.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']) . '",)';

「配列」という単語のみをデータベースに挿入します。何か案が?

4

2 に答える 2

0
var selectArr = []; 

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

これで selectArr をサーバーに渡して、何が得られるかを確認できます。

于 2013-08-19T10:17:26.050 に答える
0

配列を挿入する場合は、for ループを介して反復処理し、すべての値を 1 つの文字列に結合する必要があります。

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

$toInsert 文字列は、DB に書き込む必要がある文字列です。

于 2013-08-19T10:20:03.117 に答える