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 607041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:19:04+00:00 2026-05-13T17:19:04+00:00

I have two tables (groups and parties) in a many to many relationship using

  • 0

I have two tables (groups and parties) in a many to many relationship using a lookup table called groupparty. I’d like to be able to assign prices to each party, such that a party that belongs to two groups might have one price in one group and a different price in another group. In the table groupparty, I have three columns: groupid, partyid and totalprice. So, to assign prices, I have the following form:

<form action="" method="post">
<?php foreach ($groups as $group): ?>
 <input type="hidden" name="groupids[]" 
        value="<?php echo $group['id']; ?>"/>
 <?php htmlout($group['groupname']); ?>
 <label for="totalprice">Price: 
 <input type="text" name="totalprices[]" id="totalprice" 
        value="<?php htmlout($totalprice); ?>"/></label><br />
 <?php endforeach; ?>
<input type="hidden" name="id" value="<?php htmlout($id); ?>"/>
<input type="submit" name="action" value="Set"/>
</form>

On the mysql side, I’ve come as far as the following script. It almost works, except that it inserts the last totalprice value entered in the form above into all the associated groups. Any other totalprice values are lost – I’m left with only one value assigned:

 if (isset($_POST['action']) and $_POST['action'] == 'Set')
 {
  include $_SERVER['DOCUMENT_ROOT'] . '/includes/connect.inc.php';
  $id = mysqli_real_escape_string($link, $_POST['id']);

  foreach($_POST['groupids'] as $groupid) 
  foreach($_POST['totalprices'] as $totalprice)
  {
   $sql = "UPDATE groupparty SET 
     totalprice = '$totalprice'
     WHERE groupid = '$groupid'
     AND partyid = '$id'";
   mysqli_query($link, $sql);
  }
 }

Any suggestions would be welcome. Thanks.

  • 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-05-13T17:19:04+00:00Added an answer on May 13, 2026 at 5:19 pm

    You can adjust your structure to make the updating a lot easier – just pass the group id as an key to the totalprices[] array in your HTML:

    <form action="" method="post">
        <?php foreach ($groups as $group): ?>
            <?php echo($group['groupname']); ?>
            <label for="totalprice">Price:</label> 
                <input type="text" name="totalprices[<?php echo $group['id']; ?>]" id="totalprice" value="<?php echo($totalprice); ?>"/></label><br />
        <?php endforeach; ?>
        <input type="hidden" name="id" value="<?php htmlout($id); ?>"/>
        <input type="submit" name="action" value="Set"/>
    </form>
    

    Then, you can just loop through that one array:

    if (isset($_POST['action']) and $_POST['action'] == 'Set')
    {
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/connect.inc.php';
        $id = mysqli_real_escape_string($link, $_POST['id']);
    
        foreach($_POST['totalprices'] as $groupid => $totalprice)
        {
            // Remember to also escape these
            $groupid = mysqli_real_escape_string($link, $groupid );
            $totalprice = mysqli_real_escape_string($link, $totalprice);
    
            $sql = "UPDATE
                        groupparty
                    SET 
                        totalprice = '$totalprice'
                    WHERE
                        groupid = '$groupid'
                    AND
                        partyid = '$id'";
    
            mysqli_query($link, $sql);
        }
    }
    

    The problem with your current structure is that for every group id, you loop through all of the prices, so obviously when the last iteration is reached, the last value is overwriting the previous.

    Given that you have groups 1, 2 and 3 and prices 20, 25 and 30. Your loop currently loops through the values like this:

    Group   Price
    1       20
    1       25
    1       30    # This is the last price Group 1 gets
    2       20
    2       25
    2       30    # This is the last price Group 2 gets
    3       20
    3       25
    3       30    # This is the last price Group 3 gets
    

    When you actually wanted this:

    Group   Price
    1       20    # This is the last price Group 1 gets
    2       25    # This is the last price Group 2 gets
    3       30    # This is the last price Group 3 gets
    

    As you can see from the upper table, all groups get the price 30 as that is the last price each of those get.

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

Sidebar

Related Questions

I have two db tables. One table contains Forecast data. The second table contains
I have two Tables in two different databases Database1 - Table1 Database2 - Table2
So I have this application in ASP MVC 3. My database has two tables:
Basically I'm trying to combine two tables together into a new table. The first
I have table in MySQL database with two integer columns for example: userid |
I have a UIViewController called LoginViewController. The UIViewController has two UITextField, username and password.
I have these 2 tables: Table1 : CustomerID Area Type Revenue 1 Europe Institutional
I want to join three tables and to calculate the Sum(Quantity) of the Table
I have a database which takes user submitted data, the entries from which I
Edit 5/11/2011: I guess it's a bit worse than what's below; in my deployed

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.