title: How Can I Update an SQL database with a foreach loop?
body: <p>I have a photo gallery and want to update multiple captions at once via form input.  I've tried to research this but I think I'm in way over my head.  This is what I have so far but it's not working..</p>
  
  <p>The data is saved in an SQL table called "gallery".  An example row might look like:</p>
  
  <pre><code>gallery_id(key) = some number
  product_id      = 500
  photo           = photo.jpg
  caption         = 'look at this picture'
  </code></pre>
  
  <p>My form inputs are generated like this:</p>
  
  <blockquote>
    <p>$sql = mysql_query("SELECT * FROM gallery WHERE product_id = 500");   </p>
  
  <pre><code> while($row = mysql_fetch_array($sql)) {
    $photo=$row['photo'];
    $caption=$row['caption'];
    echo '&lt;img src="$photo"/&gt;';
    echo '&lt;input name="cap['.$caption.']" id="cap['.$caption.']" value="'.$caption.'" /&gt;';
    }
  </code></pre>
  </blockquote>
  
  <p>So once I submit the form I start to access my inputs like this but I hit a wall..</p>
  
  <pre><code>    if( isset($_POST['cap']) &amp;&amp; is_array($_POST['cap']) ) {
         foreach($_POST['cap'] as $cap) {
          mysql_query("UPDATE gallery 
                       SET caption=$caption
                       WHERE ???????");
         }
  
      }
  </code></pre>
  
  <p>I don't know how to tell the database where to put these inputs and as far as I can tell you can't pass more than one variable in a foreach loop.</p>