如何将多个选择值插入数据库
How insert multiple select values to database?
我正在尝试向数据库中插入多个select值。
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']) . '",)';
它只在数据库中插入单词"数组"。知道吗?
var selectArr = [];
$('#first option').each(function() {
selectArr.push($(this).val());
});
现在,您可以将selectArr传递到服务器,然后检查您得到的内容。
如果它正在插入一个数组,您应该通过for循环对其进行迭代,将所有值连接到一个字符串中:
<?php
$toInsert = '';
foreach($_POST['second'] as $p){
$toInsert = $toInsert . $p;
}
?>
$toInsert字符串是您应该写入数据库的字符串。
相关文章:
- 使用formData使用ajax将图像插入数据库
- 如何访问日期并将其插入数据库?节点.JS&MongoDB
- 使用Mysqli、PHP和AJAX的聊天系统正在将空白消息插入数据库
- AJAX PHP 连接将行插入数据库
- 使用 pur Html 和 Javascript 将文件插入数据库
- 将表单数据插入数据库后无法重定向到 html 页面
- 将整数变量插入数据库
- 单击按钮可多次动态添加选择框、文本框和日期,并将这些值插入数据库
- 动态选择列表AJAX和插入数据库表通过张贴形式
- 无法使用ajax序列化功能将图像插入数据库
- 使用ajax进行内联编辑(将值插入数据库)
- 将捕获的图像插入数据库问题(Cordova/SQlite/JavaScript)
- 如何在 PHP 中将动态表值插入数据库
- 将数据插入数据库express/mongodb应用程序时出错
- 通过API将视频信息插入数据库
- 我想单击一个
- 并获取 id 以通过 POST 将其插入数据库,可以吗?
- 如何将翻译后的文本插入数据库 php
- PHP-插入数据库而不刷新页面(OOP)
- MongoDB在将文档插入数据库之前为空集合
- 将本地存储值插入数据库