#StackBounty: #php #mysql Dynamically created multi select box values not inserted correctly

Bounty: 50

<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.


Get this bounty!!!