title: PHP Form with While Loop Only Sending Last $_POST Checkbox
body: <p>I have a PHP form that pulls data from the database, prints it out in rows, and then allows the user to check which items they need. On save, only the very last option is being sent through to $_POST. Here's my code: </p>
  
  <pre><code>global $dbc; 
  
  $afQuery = "SELECT fid, facility_name, city, state, country, phone from facilities 
              WHERE uid = '123'
              AND fid != '456'
              GROUP BY fid;";
          $afResult = $dbc-&gt;query($afQuery,__FILE__,__LINE__);
          $i = 0;
          while ($affiliated = $dbc-&gt;fetch($afResult)) {      
              ++$i;
              echo '&lt;div class="action" style="float:left;"&gt;';
                  echo '&lt;input type="checkbox" name="affiliated[afid]" id="afid" value="'.$affiliated-&gt;fid.'"&gt;';
              echo '&lt;/div&gt;';
              echo '&lt;div class="facility-name"&gt;';
                  echo '&lt;input type="hidden" id="facility-name '.$i.'" name="affiliated[facility_name]" value="'.$affiliated-&gt;facility_name.'" /&gt;';
                  echo $affiliated-&gt;facility_name;
              echo '&lt;/div&gt;';
              echo '&lt;div class="facility-location"&gt;';
                  echo '&lt;input type="hidden" id="facility-location '.$i.'" name="affiliated[facility_location]" value="'.$affiliates-&gt;city. ', ' . $affiliated-&gt;state . ' ' . $affiliated-&gt;country.'" /&gt;';
                  echo $affiliated-&gt;city. ', ' . $affiliated-&gt;state . ' ' . $affiliated-&gt;country;
              echo '&lt;/div&gt;';
              echo '&lt;div class="facility-phone"&gt;';
                  echo '&lt;input type="hidden" id="facility-phone '.$i.'" name="affiliated[facility_phone]" value="'.$affiliated-&gt;phone.'" /&gt;';
                  echo $affiliated-&gt;phone;
              echo '&lt;/div&gt;';
              echo '&lt;div class="clear"&gt;&lt;/div&gt;';
              echo '&lt;input type="hidden" id="update" name="update" value="' . $affiliated-&gt;fid . '" /&gt;';
          }
  </code></pre>
  
  <p><code>$dbc-&gt;fetch</code> is equivalent to <code>mysql_fetch_assoc</code>.</p>
  
  <p>I am trying to save these values to the <code>$affiliated</code> array, but it should have sub-arrays for each selected item, so I can store each value in the database on a separate row. Where am I missing it? I tried a foreach within the while loop, but I got nowhere with that.</p>
  
  <p>Thanks for your help!</p>
  
  <p><strong>* UPDATE *</strong> </p>
  
  <p>I have followed @Pinetree's suggestion and added <code>$i</code> like so: </p>
  
  <p><code>&lt;input type="checkbox" name="affiliated[afid]['.$i.']" id="afid" value="'.$affiliated-&gt;fid.'"&gt;</code></p>
  
  <p>However, now I receive the following output: </p>
  
  <pre><code>Array
  (
      [afid] =&gt; Array
          (
              [1] =&gt; 5289
              [2] =&gt; 5290
              [3] =&gt; 5291
              [4] =&gt; 5292
          )
  
      [facility_name] =&gt; Array
          (
              [1] =&gt; Test Company
              [2] =&gt; Test Company 2
              [3] =&gt; Test Company 3
              [4] =&gt; Test Company 4
          )
  
      [facility_location] =&gt; Array
          (
              [1] =&gt; Address, State US
              [2] =&gt; Address, State US
              [3] =&gt; Address, State US
              [4] =&gt; Address, State US
          )
  
      [facility_phone] =&gt; Array
          (
              [1] =&gt; 555-555-5555
              [2] =&gt; 555-555-5555
              [3] =&gt; 555-555-5555
              [4] =&gt; 555-555-555
          )
  
  )
  </code></pre>
  
  <p>I need this to instead look like this so I can insert each complete row into a table in the database: </p>
  
  <pre><code>Array
      (
          [0] =&gt; Array
              (
                  [afid] =&gt; 5289
                  [facility_name] =&gt; Test Company
                  [facility_location] =&gt; Address, ST USA
                  [facility_phone] =&gt; 555-555-5555
              )
  
      [1] =&gt; Array
          (
              [afid] =&gt; 5290
              [facility_name] =&gt; Test Company 1
              [facility_location] =&gt; Address, ST USA
              [facility_phone] =&gt; 555-555-5555
          )
  )
  </code></pre>
  
  <p>Suggestions?</p>