Home >Database >Mysql Tutorial >How Can I Bulk Update Database Rows Using a Form with Array Inputs?

How Can I Bulk Update Database Rows Using a Form with Array Inputs?

Susan Sarandon
Susan SarandonOriginal
2024-12-06 00:35:111047browse

How Can I Bulk Update Database Rows Using a Form with Array Inputs?

Bulk Update of Database Rows Via Form

In this scenario, you have a form that allows backend users to edit the titles and tags of multiple photos associated with a specific gallery. The goal is to update all selected photo records in the database upon form submission.

Form Configuration

The form must be modified slightly to submit the edited field values as arrays, since multiple fields share the same names.

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

Update Query

Once the form is submitted, the code iterates through the submitted arrays to retrieve the updated values and execute the database update for each photo.

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>

By making these adjustments to the form and update query, you ensure that the titles and tags of all selected photos are successfully updated in the database upon form submission.

The above is the detailed content of How Can I Bulk Update Database Rows Using a Form with Array Inputs?. 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