Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:35:35+00:00 2026-06-07T03:35:35+00:00

I am having the hardest time figuring out something that I think should be

  • 0

I am having the hardest time figuring out something that I think should be simple. I need to update multiple rows in my database with one submit button. I have it working with a submit for each row now, but I need to combine it. Here’s what I’m trying. Where have I gone wrong? (I’ve been going off of multiple tutorials I found online and I think I have things all mixed up).

Here’s the form:

<?php foreach ($teams as $team): 
$id[]=$team['id'];?>


<form action="?update" method="post">
    <div class="team-box">
        <h2><?php echo $team['name'] ?></h2>

        <label for="name">Name:</label>
        <input type="text" name="name" value="<?php echo $team['name'] ?>" />

        <label for="name">Match Wins:</label>
        <input type="text" name="mwins" value="<?php echo $team['mwins'] ?>" />

        <label for="name">Match Losses:</label>
        <input type="text" name="mlosses" value="<?php echo $team['mlosses'] ?>" />

        <label for="name">Match Ties:</label>
        <input type="text" name="mties" value="<?php echo $team['mties'] ?>" />

        <label for="name">Game Wins:</label>
        <input type="text" name="gwins" value="<?php echo $team['gwins'] ?>" />

        <label for="name">Game Losses:</label>
        <input type="text" name="glosses" value="<?php echo $team['glosses'] ?>" />

        <input type="hidden" name="id" value="<?php echo $team['id'] ?>" />
    </div>

Here’s the PHP to handle the UPDATE:

try
{
foreach($_POST['id'] as $id) {
$sql = 'UPDATE teams SET
    name = "' . $_POST['name'.$id] . '",
    mwins = "' . $_POST['mwins'.$id] . '",
    mlosses = "' . $_POST['mlosses'.$id] . '",
    mties = "' . $_POST['mties'.$id] . '",
    gwins = "' . $_POST['gwins'.$id] . '",
    glosses = "' . $_POST['glosses'.$id] . '"
    WHERE id = "' . $_POST['id'.$id] . '"';
$pdo->exec($sql);
}

}
catch (PDOException $e)
{
    $error = 'Error adding submitted team: ' . $e->getMessage();
    include 'error.html.php';
    exit();
}

header('Location: .');
exit();

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:35:38+00:00Added an answer on June 7, 2026 at 3:35 am

    There are a couple of things that need fixing.

    1. The FORM must be outside the foreach.

    2. The ‘…name=”name” value=”…’, to agree with the _POST code, should read instead:
      … name=”name” value=”…

    This way, a single POST will submit, say,

      name123="Rangers"
      mwins174="123"
    

    Then you need all IDs. You can do that by issuing, in the foreach, this:

     <input type="hidden" name="id[]" value="<?php print $team['id']; ?>" />
    

    This will result in HTML:

     <input type="hidden" name="id[]" value="123" />
     ...
     <input type="hidden" name="id[]" value="456" />
    

    and in $_POST[‘id’] being an array containing 123, 456 and so on.

    You could also put in HTML:

     <input type="text" name="name[<?php print $team['id']; ?>]" value="...
    

    so that $_POST[‘name’] would be a vector with the same keys as the values of id, and therefore:

    foreach($id as $team_id)
    {
        // Pseudocode
        UPDATE... SET name=$name[$team_id]... WHERE id = $team_id;
    }
    

    This way you have one SUBMIT, and multiple UPDATEs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having the hardest time figuring out something that seems like it should be
I am having the hardest time trying to figure out this (should be) simple
I am having the hardest time figuring this out, even though it is one
I'm having the hardest time figuring out why Firefox3.1 returns the value of 'content'
I'm having the hardest time figuring out how to remove home screen AppWidget's programmatically
I'm having the hardest time with what should be super simple. I can't grab
So I'm having the hardest time figuring out what this thing is called, or
I am having the hardest time figuring out how to move my Subversion repository
Check this link! I am having the hardest time trying to figure out why
I am having the hardest time to get a simple sendmail.php to work. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.