title: Insert MySQL table column array into new MySQL table
body: <p>I have a php form that gets dumped into a MySQL table. I create an array that is populated from a Product table using php. </p>
  
  <p>Queries the table to get the values for the array - works fine.</p>
  
  <pre><code> &lt;?php 
   function getProducts ($id)
   {
      $sql =("SELECT * FROM products where products.CAT_ID = $id AND DISABLED ='NO'");
      $result = mysql_query($sql);
      if ($result &gt; 0)
      {
      $products = array();
      $x=0;
      while($row = mysql_fetch_array($result))
      {
          $products[$x]= $row;
          $x += 1;
      }
      return $products;
      }
      else return false;  
  }
  function newProd(){
  global $products;
  }
  ?&gt;
  </code></pre>
  
  <p>Then I use the above array to create labels in a javascript accordion. Next to the labels -are select boxes. (Removed unnecessary page elements.. ) Html/php form below that displays the above array.   </p>
  
  <pre><code>&lt;?php include('includes/query.php');?&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>
  
      
          Inventory
      </p>
  
  <pre><code>&lt;body onload="toggleField()"&gt;
  
          &lt;div id="container" class="container_16"&gt;
          &lt;form name="form1" id="form1" action="insert.php" method="post" enctype="multipart/form-data" style="height: inherit"&gt;
              &lt;div id="Info" class="grid_16"&gt; 
                  &lt;fieldset&gt;
                      &lt;label id="Label1"&gt;
                          &lt;span class="Labels"&gt;Store Information&lt;/span&gt; &lt;br&gt;
                      &lt;/label&gt;
                      &lt;div&gt;
                      &lt;label for="name"&gt; 
                          &lt;span class="Labels"&gt; Name :&lt;/span&gt; &lt;/label&gt; 
              &lt;input id="name" name="name" type="text"&gt;  
                      &lt;/div&gt;  
            &lt;/fieldset&gt;
              &lt;/div&gt;
              &lt;hr id="SpacePadding"&gt;
              &lt;hr class="SpacePadding"&gt;
              &lt;div class="clear"&gt;&lt;/div&gt;
                  &lt;!-- Start of the Accordion Container--&gt;
                  &lt;!-- Start array, make sure Category Array isn't false --&gt;
                  &lt;?php if($categoryArray !=false):?&gt;
                      &lt;!-- create the variables for modulus --&gt;
                      &lt;!-- Start the foreach loop for the category Array --&gt;
                      &lt;?php $cataCounter = 0; ?&gt;
                      &lt;?php $closeSection = false; ?&gt;
                      &lt;?php foreach($categoryArray as $row):?&gt;
                          &lt;?php if($cataCounter % 2 == 0): ?&gt;
                              &lt;div class="push_1 grid_8"&gt;
                              &lt;?php $closeSection = false; ?&gt;
                          &lt;?php else: ?&gt;
                              &lt;div class="grid_8"&gt;
                              &lt;?php $closeSection = true; ?&gt;
                          &lt;?php endif; ?&gt;&lt;!-- insert modulus--&gt;
                                  &lt;div class="AccordionTitle"&gt;&lt;?php echo $row['CATEGORY_NAME'];?&gt;&lt;/div&gt;
                                  &lt;div class="AccordionContent"&gt;
                                      &lt;table&gt;
                                          &lt;?php foreach(getProducts($row['CAT_ID']) as $row):?&gt;
                                          &lt;tr&gt;
                                              &lt;th class="boldLabels"&gt;&lt;?php echo $row['PRODUCT_NAME'];?&gt;
                                              &lt;span class="label"&gt;&lt;/span&gt;&lt;/th&gt;
                                              &lt;th&gt;&lt;span class="Labels"&gt;Count:&lt;/span&gt;&lt;/th&gt;
                                              &lt;td&gt;
                                                  &lt;select name="selCount[]" onChange="toggleField(this.value);" class="Listbox"&gt;
                                                      &lt;option value="Select"&gt;Select One&lt;/option&gt;
                                                      &lt;option value="0"&gt;0&lt;/option&gt;
                                                      &lt;option value="1"&gt;1&lt;/option&gt;
                                                      &lt;option value="2"&gt;2&lt;/option&gt;
                                                      &lt;option value="3"&gt;3&lt;/option&gt;
                                                      &lt;option value="4"&gt;4&lt;/option&gt;
                                                      &lt;option value="5"&gt;5&lt;/option&gt;
                                                      &lt;option value="6"&gt;6&lt;/option&gt;
                                                      &lt;option value="7"&gt;7&lt;/option&gt;
                                                      &lt;option value="8"&gt;8&lt;/option&gt;
                                                      &lt;option value="9"&gt;9&lt;/option&gt;
                                                      &lt;option value="10"&gt;10&lt;/option&gt;
                                                      &lt;option value="Other"&gt;Other(Specify)&lt;/option&gt;
                                                  &lt;/select&gt;
                                                  &lt;input type="text" name="otherCount[]" class ="Listbox" style="display: none;"&gt;
                                              &lt;/td&gt;
                                              &lt;td&gt;&lt;span class="Labels"&gt;Need:&lt;/span&gt;&lt;/td&gt;  
                                              &lt;td&gt;
                                                  &lt;select name="selNeed[]" onChange="toggleField2(this.value);" class="Listbox"&gt;
                                                      &lt;option value="Select"&gt;Select One&lt;/option&gt;
                                                      &lt;option value="0"&gt;0&lt;/option&gt;
                                                      &lt;option value="1"&gt;1&lt;/option&gt;
                                                      &lt;option value="2"&gt;2&lt;/option&gt;
                                                      &lt;option value="3"&gt;3&lt;/option&gt;
                                                      &lt;option value="4"&gt;4&lt;/option&gt;
                                                      &lt;option value="5"&gt;5&lt;/option&gt;
                                                      &lt;option value="6"&gt;6&lt;/option&gt;
                                                      &lt;option value="7"&gt;7&lt;/option&gt;
                                                      &lt;option value="8"&gt;8&lt;/option&gt;
                                                      &lt;option value="9"&gt;9&lt;/option&gt;
                                                      &lt;option value="10"&gt;10&lt;/option&gt;
                                                      &lt;option value="Other"&gt;Other(Specify)&lt;/option&gt;
                                                  &lt;/select&gt;
                                                  &lt;input type="text" name="otherNeed[]" class ="Listbox" style="display: none;"&gt;
                                              &lt;/td&gt;
                                          &lt;/tr&gt;
                                          &lt;?php endforeach;?&gt;
                                      &lt;/table&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;?php if($closeSection): ?&gt;
                                  &lt;div class="clear"&gt;&lt;/div&gt;
                              &lt;?php endif; ?&gt;
                          &lt;?php $cataCounter += 1; ?&gt;
                      &lt;?php endforeach;?&gt;
              &lt;?php endif;?&gt;
  
          &lt;input name="Submit" type="submit" value="submit"&gt;
          &lt;/div&gt;
          &lt;/form&gt;
          &lt;br&gt;
      &lt;/div&gt;
          &lt;footer&gt;
          &lt;div align="center"&gt;
              &lt;ul&gt;
                  &lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="#"&gt;Daily Inventory Counts&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="#"&gt;Contact System Admin&lt;/a&gt;&lt;/li&gt;   
              &lt;/ul&gt;
          &lt;/div&gt;
      &lt;/footer&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>When submitted, I want the Product_Name to go inside a new table with the correlating select option.
  (i took out the database connection info out.)</p>
  
  <pre><code>    &lt;?php
  //Assign array
  $SelCount = $_POST['selCount'];
  $SelNeed = $_POST['selNeed'];
  $otherNeed = $_POST['otherNeed'];
  $otherCount = $_POST['otherCount'];
  
  $limit = count($SelCount);
  
  for($i=0;$i&lt;$limit;$i++) {
      $SelCount[$i] = mysql_real_escape_string($SelCount[$i]);
      $SelNeed[$i] = mysql_real_escape_string($SelNeed[$i]);
      $otherNeed[$i] = mysql_real_escape_string($otherNeed[$i]);
      $otherCount[$i] = mysql_real_escape_string($otherCount[$i]);    
  
  if  (($SelCount[$i]) !="Select" ) {
  $sql = ("INSERT INTO inventory ( STORE_ID, REQUESTOR, ITEM_COUNT, ITEM_NEED, NEED_COUNT, OTHER_COUNT) 
  VALUES ('$_POST[store]', '$_POST[name]', '".$SelCount[$i]."', '".$SelNeed[$i]."', '".$otherNeed[$i]."', '".$otherCount[$i]."')");
  
      if(mysql_query($sql ,$db)) 
          echo "$i successfully inserted.&lt;br/&gt;";
          else 
          echo "$i encountered an error. &lt;br/&gt;";
  
      } 
  }
  
  ?&gt;
  </code></pre>