<table>
<tr>
<td><select class="form-control selectpicker" data-live-search="true" name="reson[]" required="required">
<option>--Select--</option>
<option value="1">AAA</option>
<option value="2">BBB</option>
<option value="3">CCC</option>
<option value="4">DDD</option>
<option value="5">EEE</option>
</select>
</td>
<td>
<select class="form-control selectpicker" data-live-search="true" name="service[]" id="service" multiple="multiple">
<option>--Select--</option>
<option value="1">List 1</option>
<option value="2">List 2</option>
<option value="3">List 3</option>
<option value="4">List 4</option>
<option value="5">List 5</option>
<option value="6">List 6</option>
</select>
</td>
<td>
<input type="text" class="form-control" name="name[]" placeholder="Name" />
</td>
</tr>
<tr>
<td><select class="form-control selectpicker" data-live-search="true" name="reson[]" required="required">
<option>--Select--</option>
<option value="1">AAA</option>
<option value="2">BBB</option>
<option value="3">CCC</option>
<option value="4">DDD</option>
<option value="5">EEE</option>
</select>
</td>
<td>
<select class="form-control selectpicker" data-live-search="true" name="service[]" id="service" multiple="multiple">
<option>--Select--</option>
<option value="1">List 1</option>
<option value="2">List 2</option>
<option value="3">List 3</option>
<option value="4">List 4</option>
<option value="5">List 5</option>
<option value="6">List 6</option>
</select>
</td>
<td>
<input type="text" class="form-control" name="name[]" placeholder="Name" />
</td>
</tr>
<tr>
<td><select class="form-control selectpicker" data-live-search="true" name="reson[]" required="required">
<option>--Select--</option>
<option value="1">AAA</option>
<option value="2">BBB</option>
<option value="3">CCC</option>
<option value="4">DDD</option>
<option value="5">EEE</option>
</select>
</td>
<td>
<select class="form-control selectpicker" data-live-search="true" name="service[]" id="service" multiple="multiple">
<option>--Select--</option>
<option value="1">List 1</option>
<option value="2">List 2</option>
<option value="3">List 3</option>
<option value="4">List 4</option>
<option value="5">List 5</option>
<option value="6">List 6</option>
</select>
</td>
<td>
<input type="text" class="form-control" name="name[]" placeholder="Name" />
</td>
</tr>
</table>
此表行是动态生成的,其输入字段值作为数组传递,其中一个选择框是多选
这是我的PHP代码
<?php
extract($_POST);
foreach ($reson as $id => $value) {
$resona = ($reson[$id]);
$namep = ($name[$id]);
$rsid = $ob->insert_data('tbl_reson',array("reson" => $resona, "name" => $namep), true);
foreach ($service as $ii => $valu) {
$r_service = ($service[$ii]);
$ob->insert_data('tbl_service',array("reson_id" => $rsid, "service" => $r_service));
}
}
?>
假设这里有3行,我从第一行选择两个多选项,从第二行选择三个选项,从第三行选择四个选项。
当插入数据库时,所有行的选定选项都相同(多选中选择的所有选项都分组到一个数组中并保存到每个字段中)。
第一张表
------------------------------
id | resn | name
------------------------------
1 | 1 | Test
2 | 2 | aaa
3 | 3 | bbb
------------------------------
第二张表
--------------------------------
id | resnid | service
--------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
4 | 2 | 4
5 | 2 | 5
6 | 3 | 6
第二表当前列表
--------------------------------
id | resnid | service
--------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 1 | 4
5 | 1 | 5
6 | 1 | 6
7 | 2 | 1
8 | 2 | 2
9 | 2 | 3
10 | 2 | 4
11 | 2 | 5
12 | 2 | 6
等...
但我需要的是在一个表中插入reson[]
和name[]
,并根据第一个表的最后插入ID在另一个表中添加服务
请帮助实现这一目标。
答案 0 :(得分:1)
您应该为每个字段明确设置密钥。 reson
和name
的密钥应与service
的密钥匹配。以下是要使用的名称的示例。
reson[0]
name[0]
service[0][]
reson[1]
name[1]
service[1][]
reson[2]
name[2]
service[2][]
service
字段有一个额外的[]
,因为它会为每一行发送多个值。
由于您说您正在动态创建行,因此您可以轻松地使用循环计数器来创建这些名称。像这样:
<?php for($i=0; $i<3; $i++) { ?>
<select class="form-control selectpicker" data-live-search="true" name="reson[<?php echo $i; ?>]" required="required">
...
</select>
<select class="form-control selectpicker" data-live-search="true" name="service[<?php echo $i; ?>][]" id="service" multiple="multiple">
...
</select>
<input type="text" class="form-control" name="name[<?php echo $i; ?>]" placeholder="Name" />
<?php } ?>
然后你的PHP代码必须是这样的:
<?php
foreach ($reson as $id => $value) {
$reson = ($reson[$id]);
$namep = ($name[$id]);
$rsid = $ob->insert_data('tbl_reson',array("reson" => $reson, "name" => $namep), true);
foreach ($service[$id] as $ii => $valu) {
$r_service = $val;
$ob->insert_data('tbl_service',array("reson_id" => $rsid, "service" => $r_service));
}
}
?>
您说您使用以下Javascript代码创建新行:
var i=$('.div').length+1; $('#lobrows').append('
<select class="form-control selectpicker" data-live-search="true" multiple="multiple" name="service['+i+'][]" id="service'+i+'">
<option>--Select--</option>
<?php for($k=0; $k<=10;$k++){?><option value="Reason <?php echo $k;?>">Reason <?php echo $k;?></option>
<?php }?></select>
');
您应该确保同时在同一行添加name
和reson
。他们都应该为他们的名字使用相同的i
值。像这样:
var i=$('.div').length+1; $('#lobrows').append('
<select class="form-control selectpicker" data-live-search="true" name="reson['+i+']" required="required">
</select>
<select class="form-control selectpicker" data-live-search="true" multiple="multiple" name="service['+i+'][]" id="service'+i+'">
<option>--Select--</option>
<?php for($k=0; $k<=10;$k++){?><option value="Reason <?php echo $k;?>">Reason <?php echo $k;?></option>
<?php }?></select>
<input type="text" class="form-control" name="name['+i+']" placeholder="Name" />
');
答案 1 :(得分:0)
我认为你在foreach中的foreach有问题所以在第一张表中插入一条记录后,其他foreach将第二张表中的所有记录插入第一张只有第一个共振的信息,请尝试以下代码。
<?php
extract($_POST);
foreach ($reson as $id => $value) {
$resona = ($reson[$id]);
$namep = ($name[$id]);
$rsid = $ob->insert_data('tbl_reson',array("reson" => $resona, "name" => $namep), true);
$r_service = ($service[$id]);
$ob->insert_data('tbl_service',array("reson_id" => $rsid, "service" => $r_service));
}
?>
答案 2 :(得分:0)
<div style="font-size: 3em;">
<md-icon>highlight</md-icon>
Title
</div>
INSERT
; (无论“ob”是什么。)$id = ... SELECT LAST_INSERT_ID()
。$id
醇>
答案 3 :(得分:0)
试试这段代码:
extract($_POST);
$rsid = array();
foreach ($reson as $id => $value) {
if(is_array($value)){
foreach($value as $key => $val)
{
$resona = ($val);
$namep = ($name[$id]);
$rsid[] = $ob->insert_data('tbl_reson',array("reson" => $resona, "name" => $namep), true);
}
}
else
{
$resona = ($value);
$namep = ($name[$id]);
$rsid[] = $ob->insert_data('tbl_reson',array("reson" => $resona, "name" => $namep), true);
}
}
foreach ($service as $ii => $valu) {
if(is_array($valu)){
foreach($valu as $key => $val)
{
$r_service = ($val);
foreach($rsid as $reson_id)
$ob->insert_data('tbl_service',array("reson_id" => $reson_id, "service" => $r_service));
}
}
else
{
$r_service = ($valu);
foreach($rsid as $reson_id)
$ob->insert_data('tbl_service',array("reson_id" => $reson_id, "service" => $r_service));
}
}