Update multiple fields in database, or maybe just one at a time?

Ok, I have a database that lists food items that are available in a kitchen. Each item has a amount field. I have a script that lists out the Items, amounts, and a text input where I want the user to be able to enter a number to be added (or subtracted) to the ‘Amount’ field of that item. At the moment, I have a ‘Submit’ button after each listed item.

What I can’t figure out, is how to update each individual field, or all of the ones that have values added. Whether it be with one button for each item, or one button for everything.

I thought about using $_GET instead of $_POST but even with the separate buttons, it is sending every single value from the whole page. Worse, the only variable that is being sent with a value is the one from the text field. So how do I tell it that that value matches a specific item to be able to make the update?

Ok, I actually figured that one out, it was alot simplier than I thought. Kinda feel like an idiot for not seeing it earlier.
What I needed to do was add a few hidden values (<input type=hidden>) with both a name (such as ingredient) and then a value which I assigned a ariable to. So, When I clicked the button, I passed say the following variables: ingredient = Chicken, amount = 5, Add_amount = 2. And from there it was an easy task of writing a simple function that would add the new and old values together and enter them into the database.

But this presented a new problem…Even though the function that updates the database is called first, and then the function that displays the database values is called afterwords, the old values are what is displayed, until I refresh the screen…Am I missing something?

Here is my code, you tell me:

The Adding Function

function update($ingredient, $amount, $global_dbh)
{
echo $amount;
$query = “UPDATE pantry SET amount=\”$amount\" WHERE ingredient=\“$ingredient\”";
mysql_query($query, $global_dbh);
}

This correctly takes my new value and updates the database.

The display function:

function display_pantry($global_dbh, $result)
{
while ($row = mysql_fetch_row($result))
{
print_r(“<form method=\“post\” action=\“pantry_handler.php\”>”);
print("<tr>
“);
print(”<td align=center>$row[1]</td>
“);
print(”<td align=center>$row[2]</td>
“);
print(”<td align=center>$row[3]</td>
“);
print(”<td align=center><input type=\“text\” domain size=10 name=newamount></td>
“);
print(”<td align=center><input type=\“submit\” name=\“submit\” value=\“Update\”></td>
“);
print(”<td align=center><input type=\“hidden\” name=\“ingredient\” value=$row[1]></td>
“);
print(”<td align=center><input type=\“hidden\” name=\“amount\” value=$row[2]></td>
“);
print(”</tr>
“);
print(”</form>
");
}
}

This properly displays all items in the database, with a field to add/subract the amount value, and a submition button.

function calls:

if($_POST[‘submit’] == “Update”)
{
update($ingredient, $amount, $global_dbh);
}
start_page($start_str);
display_pantry($global_dbh, $result);
finish($end_str);

First, I check to see if the ‘Update’ button was clicked. If it was, I go to the fuction that updates the database.
Second I load the function the prints out the beginning of my page (formats the first part of the HTML)
Second, I call the display function to list my items.
Last I call the function that finishes out my page.

Now, I would assume, that since my database gets updated before being displayed, that it would display the updated values. Is the update not happening fast enough or something?
What can I do to make sure the updated values are the ones displayed?

Ok, i have a function that updates my database (if new values are given) and a function that displays my database. If I call the update function first, before displaying it, shouldn’t the display show the updated values? Here is my code:

Here is my code, you tell me:

The Adding Function

function update($ingredient, $amount, $global_dbh)
{
echo $amount;
$query = “UPDATE pantry SET amount=\”$amount\" WHERE ingredient=\“$ingredient\”";
mysql_query($query, $global_dbh);
}

This correctly takes my new value and updates the database.

The display function:

function display_pantry($global_dbh, $result)
{
while ($row = mysql_fetch_row($result))
{
print_r(“<form method=\“post\” action=\“pantry_handler.php\”>”);
print("<tr>
“);
print(”<td align=center>$row[1]</td>
“);
print(”<td align=center>$row[2]</td>
“);
print(”<td align=center>$row[3]</td>
“);
print(”<td align=center><input type=\“text\” domain size=10 name=newamount></td>
“);
print(”<td align=center><input type=\“submit\” name=\“submit\” value=\“Update\”></td>
“);
print(”<td align=center><input type=\“hidden\” name=\“ingredient\” value=$row[1]></td>
“);
print(”<td align=center><input type=\“hidden\” name=\“amount\” value=$row[2]></td>
“);
print(”</tr>
“);
print(”</form>
");
}
}

This properly displays all items in the database, with a field to add/subract the amount value, and a submition button.

function calls:

if($_POST[‘submit’] == “Update”)
{
update($ingredient, $amount, $global_dbh);
}
start_page($start_str);
display_pantry($global_dbh, $result);
finish($end_str);

First, I check to see if the ‘Update’ button was clicked. If it was, I go to the fuction that updates the database.
Second I load the function the prints out the beginning of my page (formats the first part of the HTML)
Second, I call the display function to list my items.
Last I call the function that finishes out my page.

Now, I would assume, that since my database gets updated before being displayed, that it would display the updated values. Is the update not happening fast enough or something?
What can I do to make sure the updated values are the ones displayed?

Your update() function is self-contained in that it is being passed the db connection, and does an update.

Your display() function though seems to be passed a $result which appears to have been generated elsewhere.

You seem to be needlessly passing a db connection to your display() function - is there more code we should be aware of?

Where is $result coming from?

Thank you for the response. $result was actually coming from the beginning of the page, and that was exactly my problem. $result was the main query grabbing info from the DB. The problem was that even after I updated the DB, those values were still sitting there in limbo ready for me to use. What I had to do was have seperate $query and $esult vars inside the display() so that when it ran (after pdate() ) it would pull new data from the database. It works fine now.