php - Dynamically created multi select box values not inserted correctly
<table>
<tr>
<td><select 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 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" name="name[]" placeholder="Name" />
</td>
</tr>
<tr>
<td><select 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 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" name="name[]" placeholder="Name" />
</td>
</tr>
<tr>
<td><select 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 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" 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
Second Table
{-code-4}
Second Table Current list
{-code-5}
etc...
But what I need is to insert{-code-6}
and{-code-7}
in one table and service in another table based on last inserted id of the first table
Please Help to achieve this.
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Solution:
You should explicitly set the key for each field. The key for
reson
andname
should match the key forservice
. Here is an example for the names to use.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:
And then your PHP code would have to be like this:
Response to your comment below
You said that you are using the following Javascript code to create new rows:
You should make sure that you are also adding the
name
andreson
in the same row. They should all use the samei
value for their names. Like this:Answer
Solution:
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.
Answer
Solution:
INSERT
$id = ... SELECT LAST_INSERT_ID()
; (by whatever "ob" does.)$id
in the secondINSERT
.Answer
Solution:
Try this code :