Hey there.
I’ve got a php code that updates a database with user input however it’s supposed to update the webpage with the new database values. There are two items on the page that should update, however only one of the works. The other just stays the same. It’s a donation script where there are two tables - one for required items and one for donated items. The script takes the amount being donated and subtracts it from the required , showing the user how many more donations are needed.
$results = $db->query("SELECT * FROM required_items;");
$donations = $db->query("SELECT * FROM donations;");
// Print out the table tag and header
echo("<form name=\"donationForm\" action=\"" . $pageName ."\" method=\"POST\">
<fieldset>");
echo("<table>
");
echo("<tr>
<th>Item Name</th><th>Amount</th>
</tr>
");
try{
// For every row in the result of a query
foreach($results as $row) {
// Loop through every donation
foreach($donations as $donation){
// If the donated item matches the current row item
if($donation['item_id'] == $row['id']){
// Subtract donated amount from the required amount
$row['required_amount'] = $row['required_amount'] - $donation['donation_amount'];
// Round negative amounts to zero
if($row['required_amount'] < 0){
$row['required_amount'] = 0;
}
}
}
// Create a new table row
echo("<tr>");
// Create a new table cell for the name and required_amount fields
echo("<td>" . $row['name'] . "</td>");
echo("<td>" . $row['required_amount'] . "</td>");
echo("<td><input type=\"radio\" name=\"radioButtons\" value=\"". $row['id'] ."\"></input></td>");
echo("</tr>
");
}
} catch(PDOException $e) {
// Catch all errors and print them out
echo("Error: ");
echo($e->getMessage());
}
As I said before, there are two items in the database but only one amount is being shown as updated. Any idea why the second one does not update?