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
Release: 2024-12-06 00:35:11
Original
1050 people have browsed it

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 />";
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template