title: Taking a list of IDs from a table, add multi select form fields for each ID, and post the results of the form including the IDs to another table
body: <p>Currently I am constructing a form where I'd like to include the following work list by date functionality:</p>
  
  <ul>
  <li><p>User selects a date </p></li>
  <li><p>All IDs and Names of active users are drawn from an established MySQL table, presented in rows</p></li>
  <li><p>2 new fields for each ID/User are presented with multiple selections </p></li>
  <li><p>The ID and the 2 new multiple selection fields are passed to another table with columns ID, date, and 2 columns (AM Work and PM Work) to add the results of the multiple selection fields.</p></li>
  </ul>
  
  <p>I understand that one field holding results of the multiple selection fields will be held and that there will be multiple date and ID rows to store these values, that is not a problem for me. </p>
  
  <p>I've created the form table with the ID and name retrieved from the established table, and included the two fields where my multiple selections are the same in both, retrieved from another table of work types. </p>
  
  <p>I first tried multiple foreach loops to pass the ID and then each AM work type selected, and then each PM work type selected. I can do this for one record, however I'd like to be able to take multiple IDs and parse multiple AM and PM multi-select work types.</p>
  
  <pre><code>    //This is part of a loop that covers the AM work entry that I've tried already:
  
      // IF THE VALIDATION PASSES - SEND THE INFORMATION TO THE DB
      if ($valid) {
  
          // SET NEW VALUES FOR THE EXISTING RECORD
          $pdo = Database::connect();
          pdo-&gt;setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  
  
          //INSERT THE WORK DATA INTO THE DATABASE
          $capture_id = "";
          foreach($_POST["id"] as $key =&gt; $capture_id){
  
              //REARRANGE DATE FOR INSERT
              $work_date = date('Y-m-d', strtotime($work_date));
  
              //CAPTURE EACH VALUE FROM THE ARRAYS AS VARIABLES
              $id = $_POST["id"][$key];
  
              // FOR EACH PIECE OF AM WORK ADD TO WORK HISTORY TABLE
              $capture_am_work = "";
              foreach($_POST["$am_work"] as $key =&gt; $capture_am_work){ 
  
                  $am_work = $_POST["am_work"][$key];                         
  
                  //INSERT THE AM DATA INTO work_history_table
                  $sql = "INSERT INTO work_history_table (date, id, am_activity_id)
                  values(?,?,?,?)";
                  $q = $pdo-&gt;prepare($sql);
                  $q-&gt;execute(array($work_date, $id, $am_work, $pm_work));
  
              }
          }
      }
  </code></pre>
  
  <p>Form details are:</p>
  
  <pre><code>    &lt;form class="form-horizontal" action="work_scheduling.php" method="post" enctype="multipart/form-data"&gt;
  
      &lt;table class="table table-striped table-bordered"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                 &lt;th&gt;id&lt;/th&gt;
                 &lt;th&gt;name&lt;/th&gt;
                 &lt;th&gt;AM Work&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
              &lt;?php 
              $pdo = Database::connect();
              $sql = "SELECT * FROM user WHERE status = 'Active'";
  
              $sth = $pdo-&gt;prepare($sql);
              $sth-&gt;bindParam(':query',$query,PDO::PARAM_STR);
              $sth-&gt;execute();
              foreach ($sth-&gt;fetchAll(PDO::FETCH_ASSOC) as $row) {
                  echo '&lt;tr&gt;';
                      echo '&lt;td style="text-align: center; vertical-align: middle;"&gt;&lt;input name="id[]" type="text"  placeholder="id" value="';
                      echo $row['id'];
                      echo '" readonly&gt;&lt;/td&gt;';
                      echo '&lt;td style="text-align: center; vertical-align: middle;"&gt;&lt;input name="name[]" type="text"  placeholder="id" value="';
                      echo $row['name'];
                      echo '" readonly&gt;&lt;/td&gt;';
                      echo '&lt;td style="text-align: center; vertical-align: middle;"&gt;';
                      echo '&lt;select name="am_work[]" class="chosen-select" data-placeholder="Click To Select AM Work" multiple tabindex="10"&gt;';
                      echo '&lt;option value=""&gt;&lt;/option&gt;';
                      $am_work_query = mysqli_query($con, "SELECT id, diary_task_name FROM diary_task_category WHERE status='Active'");
                      while ($am_work_rows = $am_work_query-&gt;fetch_array(MYSQLI_ASSOC)) {
                          $am_work_value = $am_work_rows['id'];
                          echo '&lt;option value="';
                          echo $am_work_value;
                          echo '"&gt;';
                          echo $am_work_rows['id'],": ",$am_work_rows['diary_task_name'];
                          echo '&lt;/option&gt;';
                       }
                       echo '&lt;/select&gt;';
                       echo '&lt;/td&gt;';
                   echo '&lt;/tr&gt;';
                  }
              Database::disconnect();
              }
          ?&gt;
  
      &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;div class="form-actions"&gt;
          &lt;button type="submit" class="btn btn-success btn-sm"&gt;Update&lt;/button&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>******* ANSWER - CODE USED SUCCESSFULLY *******</p>
  
  <pre><code>    //table setup
  
      foreach ($sth-&gt;fetchAll(PDO::FETCH_ASSOC) as $row) {
          $id2 = $row['id'];
          $name = $row['name'];
  
          echo '&lt;tr&gt;';
              echo '&lt;td&gt;&lt;input name="id2[]" type="number"  placeholder="id2" value="';
              echo $id2;
              echo '"&gt;&lt;/td&gt;';
  
              echo '&lt;td&gt;&lt;input name="name[]" type="text"  placeholder="name" value="';
              echo $name;
              echo '"&gt;&lt;/td&gt;';
  
              echo '&lt;td&gt;';
              echo '&lt;select name="id2[';
              echo $id2;
              echo '][am_work][]" class="chosen-select" data-placeholder="Click To Select AM Work" multiple tabindex="10"&gt;';
              echo '&lt;option value=""&gt;&lt;/option&gt;';
              $am_work_query = mysqli_query($con, "SELECT id, diary_task_name FROM diary_task_category WHERE status='Active'");
              while ($am_work_rows = $am_work_query-&gt;fetch_array(MYSQLI_ASSOC)) {
                  $am_work_value = $am_work_rows['id'];
                  echo '&lt;option value="';
                  echo $am_work_value;
                  echo '"&gt;';
                  echo $am_work_rows['id'],": ",$am_work_rows['diary_task_name'];
                  echo '&lt;/option&gt;';
              }
              echo '&lt;/select&gt;';
              echo '&lt;/td&gt;';
              echo '&lt;/tr&gt;';
      }
  
  
  
      //loop
  
      foreach($_POST["id2"] as $record_id =&gt; $value){
          $val1 = $record_id;
  
          foreach($value["am_work"] as $key =&gt; $am_work_value){
              $val2 = $value["am_work"][$key];
  
             //INSERT THE AM DATA INTO work_history_table
             $sql = "INSERT INTO work_history_table (date, horse_id, am_activity_id)
             values(?,?,?)";
             $q = $pdo-&gt;prepare($sql);
             $q-&gt;execute(array($work_date, $val1, $val2));
          }
  
      }   
  </code></pre>
  
  <p>ultimately the expected results would be entries into a table that iterates through each ID, and then for each id, iterate through each multi-selected items.</p>
  
  <p>Please be gentle - I'm completely new to all of this!</p>