如何插入多选的值 [英] How to insert values of multiple select
本文介绍了如何插入多选的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个选择列表,这是代码:
I have a list multiple select, this is the code :
<form method="post" action="">
<select name=thematique[] multiple>
<option value="1"> MIE </option>
<option value="2"> Migration </option>
<option value="3"> Réunification familiale </option>
</select>
<button type="submit" class="button" name="categoriser">Catégoriser</button>
</form>
我的问题是,如果我选择MIE + Migration +Réunificationfamiliale,所以我的数据库中有3行具有相同的ID. 我想用一行将插入select的所有选项(0 1 2或3).
My problem is if I select MIE + Migration + Réunification familiale, so I have 3 lines in my data base for the same ID. I want to have one line which will insert all options of the select (0 1 2 or 3).
$id_struct=$_POST['id_struct'];
$thematique=$_POST['thematique']);
foreach($thematique as $item) {
$string= $item.' ';
echo $string;
$bdd->exec("INSERT INTO categorisation SET id_thematique=$string
WHERE id_struct=$id_struct");
}
PS:id_thematique是一个外键.
PS : id_thematique is a foreign key.
推荐答案
您可以将主题数组用作对象数组:
You can use array of you thematique as object array:
$thematiqueArray = array(0=>'MIE', 1=>'Migration', 2=>'Réunification familiale');
像这样设置您的html options元素:
set your html options element like this:
<option value=0>MIE</option>
<option value=1>Migration</option>
<option value=2>Réunification familiale</option>
并在您的POST上捕获类似的代码:
and on your POST catching similiar like this:
$thematique=$_POST['thematique']);
foreach($thematique as $key => $value) {
$item = $thematiqueArray[$key];
echo $item;
}
这篇关于如何插入多选的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文