php - Dynamically created multi select box values not inserted correctly

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

152

Answer

id | resn | name
724

Answer

1 | 1 | Test 2 | 2 | aaa 3 | 3 | bbb
50

Answer

|||
612

Answer

-- id | resnid | service
816

Answer

-- 1 | 1 | 1 2 | 1 | 2 3 | 2 | 3 4 | 2 | 4 5 | 2 | 5 6 | 3 | 6|||
378

Answer

-- id | resnid | service
960

Answer

-- 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[]
486

Answer

Solution:

You should explicitly set the key for each field. The key forreson andname should match the key forservice. 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][]

Theservice 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 data-live-search="true" name="reson[<?php echo $i; ?>]" required="required">
    ...
  </select>
  <select data-live-search="true" name="service[<?php echo $i; ?>][]" id="service" multiple="multiple">
    ...
  </select>
  <input type="text" 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));
    }
}
?>

Response to your comment below

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 thename andreson in the same row. They should all use the samei value for their names. Like this:

var i=$('.div').length+1; $('#lobrows').append('
  <select data-live-search="true" name="reson['+i+']" required="required">
  </select>
  <select 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" name="name['+i+']" placeholder="Name" />
');
329

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.

<?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));
 }
?>
105

Answer

Solution:

  1. Do the firstINSERT
  2. Perform$id = ... SELECT LAST_INSERT_ID(); (by whatever "ob" does.)
  3. Use$id in the secondINSERT.
516

Answer

Solution:

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));
        }


    }

People are also looking for solutions to the problem: php - How do I send data using a refresh function in javascript and retrieve another data using the same refresh function

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.