Reputation: 289
<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>
This table rows are generated dynamicaly, its input field values are passed as an array and one of the select box is multiselect
Here is my php code
<?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));
}
}
?>
Suppose here we have 3 rows and I select two multiple option from first row and three options from second row and four options from third row.
And when inserted into the DB, the selected options become same for all rows (All the options selected in multiselect are grouped into one array and saved into each field).
First table
------------------------------
id | resn | name
------------------------------
1 | 1 | Test
2 | 2 | aaa
3 | 3 | bbb
------------------------------
Second Table
--------------------------------
id | resnid | service
--------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
4 | 2 | 4
5 | 2 | 5
6 | 3 | 6
Second Table Current list
--------------------------------
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
etc...
But what I need is to insert reson[]
and name[]
in one table and service in another table based on last inserted id of the first table
Please Help to achieve this.
Upvotes: 1
Views: 718
Reputation: 6778
You should explicitly set the key for each field. The key for reson
and name
should match the key for service
. Here is an example for the names to use.
reson[0]
name[0]
service[0][]
reson[1]
name[1]
service[1][]
reson[2]
name[2]
service[2][]
The service
field has an additional []
because it sends multiple values for each row.
Since you said that you are creating the rows dynamically, you could easily use a loop counter to create these names. Like this:
<?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 } ?>
And then your PHP code would have to be like this:
<?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));
}
}
?>
You said that you are using the following Javascript code to create new rows:
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>
');
You should make sure that you are also adding the name
and reson
in the same row. They should all use the same i
value for their names. Like this:
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" />
');
Upvotes: 1
Reputation: 740
Try this code :
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));
}
}
Upvotes: 0
Reputation: 142356
INSERT
$id = ... SELECT LAST_INSERT_ID()
; (by whatever "ob" does.)$id
in the second INSERT
.Upvotes: 0
Reputation: 339
i think you have problem in foreach in foreach so after one record is inserted in first table other foreach is insert all record in second table with only first reson_id please try following code.
<?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));
}
?>
Upvotes: 0