Php 如何将多个select值插入数据库?

Php 如何将多个select值插入数据库?,php,javascript,html,Php,Javascript,Html,我正在尝试向数据库插入多个select值 HTML PHP:(这只是PHP代码的一小部分) 它只在数据库中插入单词“Array”。有什么想法吗 var selectArr = []; $('#first option').each(function() { selectArr.push($(this).val()); }); 现在,您可以将selectArr传递给服务器,然后检查您得到的结果。如果它正在插入数组,您应该通过for循环对其进行迭代,以将所有值合并到一个字符串中: &l

我正在尝试向数据库插入多个select值

HTML

PHP:(这只是PHP代码的一小部分)

它只在数据库中插入单词“Array”。有什么想法吗

var selectArr = []; 

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

现在,您可以将selectArr传递给服务器,然后检查您得到的结果。

如果它正在插入数组,您应该通过for循环对其进行迭代,以将所有值合并到一个字符串中:

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


$toInsert字符串是应该写入数据库的字符串。

这是因为代码$this->SanitizeForSQL($formvars['second'])返回一个数组,不能以这种方式插入。查找批插入查询。
$formvars['second'] = $this->Sanitize($_POST['second']);
$insert_query = 'insert into 'comments'(second) values ("' . $this->SanitizeForSQL($formvars['second']) . '",)';
var selectArr = []; 

$('#first option').each(function() {
    selectArr.push($(this).val());
});
<?php
    $toInsert = '';
    foreach($_POST['second'] as $p){
        $toInsert = $toInsert . $p;
    }
?>