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?