0

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..

The data is saved in an SQL table called "gallery". An example row might look like:

gallery_id(key) = some number
product_id      = 500
photo           = photo.jpg
caption         = 'look at this picture'

My form inputs are generated like this:

$sql = mysql_query("SELECT * FROM gallery WHERE product_id = 500");

 while($row = mysql_fetch_array($sql)) {
  $photo=$row['photo'];
  $caption=$row['caption'];
  echo '<img src="$photo"/>';
  echo '<input name="cap['.$caption.']" id="cap['.$caption.']" value="'.$caption.'" />';
  }

So once I submit the form I start to access my inputs like this but I hit a wall..

    if( isset($_POST['cap']) && is_array($_POST['cap']) ) {
       foreach($_POST['cap'] as $cap) {
        mysql_query("UPDATE gallery 
                     SET caption=$caption
                     WHERE ???????");
       }

    }

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.

4

1 回答 1

0

$_POST 是一个数组,如果我没有错(我现在无法测试它并且我在 PHP 中是新的)你可以做

foreach ($_POST as $p) {
    $id=$p['id'];'
    $cap=$p['caption'];
mysql_query("UPDATE gallery 
                 SET caption=$cap
                 WHERE photoid=$id");

}
于 2013-02-25T02:24:46.070 回答