title: nested foreach loops with a many to many relationship
body: <p>I have two tables (groups and parties) in a many to many relationship using a lookup table called groupparty. I'd like to be able to assign prices to each party, such that a party that belongs to two groups might have one price in one group and a different price in another group.  In the table groupparty, I have three columns: groupid, partyid and totalprice.  So, to assign prices, I have the following form:</p>
  
  <pre><code>&lt;form action="" method="post"&gt;
  &lt;?php foreach ($groups as $group): ?&gt;
   &lt;input type="hidden" name="groupids[]" 
          value="&lt;?php echo $group['id']; ?&gt;"/&gt;
   &lt;?php htmlout($group['groupname']); ?&gt;
   &lt;label for="totalprice"&gt;Price: 
   &lt;input type="text" name="totalprices[]" id="totalprice" 
          value="&lt;?php htmlout($totalprice); ?&gt;"/&gt;&lt;/label&gt;&lt;br /&gt;
   &lt;?php endforeach; ?&gt;
  &lt;input type="hidden" name="id" value="&lt;?php htmlout($id); ?&gt;"/&gt;
  &lt;input type="submit" name="action" value="Set"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>On the mysql side, I've come as far as the following script.  It almost works, except that it inserts the last totalprice value entered in the form above into all the associated groups.  Any other totalprice values are lost - I'm left with only one value assigned:</p>
  
  <pre><code> if (isset($_POST['action']) and $_POST['action'] == 'Set')
   {
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/connect.inc.php';
    $id = mysqli_real_escape_string($link, $_POST['id']);
  
    foreach($_POST['groupids'] as $groupid) 
    foreach($_POST['totalprices'] as $totalprice)
    {
     $sql = "UPDATE groupparty SET 
       totalprice = '$totalprice'
       WHERE groupid = '$groupid'
       AND partyid = '$id'";
     mysqli_query($link, $sql);
    }
   }
  </code></pre>
  
  <p>Any suggestions would be welcome.  Thanks.</p>