Home >Backend Development >PHP Tutorial >How to Update Multiple Database Rows from a Single Form Submission Using PHP and MySQL?

How to Update Multiple Database Rows from a Single Form Submission Using PHP and MySQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-08 03:27:02624browse

How to Update Multiple Database Rows from a Single Form Submission Using PHP and MySQL?

Updating Multiple Rows from a Post Form with MySQL

This article explores the update operation on multiple rows using PHP and MySQL. We'll walk through a scenario where users can modify the title and tags of multiple photos from a form submission simultaneously.

Form Structure

The HTML form fetches photos belonging to a specific gallery and displays them with editable fields for title and tags:

<code class="html">if(isset($_GET['id']))
{
    $id=$_GET['id'];
    $result = $db->prepare("SELECT * FROM photos WHERE gallery_id = :gallery_id ");      
    $result->bindParam(':gallery_id', $id);
    $result->execute();

    echo '<form action="" method="POST">';
    echo "<ul id='photos'>";

    for ($i = 0; $row = $result->fetch(); $i++)
    {
        $id = $row['id'];
        $title = $row['title'];
        $tags = $row['tags'];
        $src = $row['src'];

        echo "<li><a class='lightbox' href='images/$src'><img src='images/$src' id='$id' alt='$title' /></a><br />";
        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' />";
        echo "</li>";
    }

    echo "</ul>";
}
?>

<div style="clear:both"></div></code>

The above is the detailed content of How to Update Multiple Database Rows from a Single Form Submission Using PHP and MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn