Post form and update multiple rows with mysql

Since you have multiple html fields with same names, you have to submit them as an arrays:

echo "<input type="text" name="photo_title[]" value="$title" /><br />";
echo "<input type="text" name="photo_tags[]" value="$tags" />";
echo "<input type="hidden" name="photo_id[]" value="$id" />";

After submitted, loop through any array variable like

foreach ($_POST['photo_id'] as $key => $photo_id) {
    $id = $photo_id;
    $title = $_POST['photo_title'][$key];
    $tags = $_POST['photo_tags'][$key];

    $sql = "UPDATE photos SET title=?, tags=? WHERE id=?";
    $query = $db->prepare($sql);
    $query->execute(array($title, $tags, $id));
}

Leave a Comment