I have PHP code that uses numerical input values from a form to make certain calculations. Those new calculations are stored in other variables, for example:
$weight1 = $priority1 * $savingsAmount;
I would like to add the newly calculated variables to my MySQL database when the input form is submitted (they are calculated the same time the form is submitted). I tried to create an SQL query to do this, but only the form input variables are added to the database; the variable values that are produced through the calculations are not added.
I tried to return/call the two calculation functions but that made my webpage go blank. I would appreciate if someone could tell me what is the best way to capture the value of these variables (that are calculated after form submissions) and add them to the database along with the form input values?
EDIT I had tried calling the two functions like this, but my webpage shows up blank, so I am guessing the issue lies in how I am calling the functions.
function weight(&$weight1, &$weight2, &$weight3, $savingsAmount) {
$priority1 = .40;
$priority2 = .30;
$priority3 = .20;
$weight1 = $priority1 * $savingsAmount;
$weight2 = $priority2 * $savingsAmount;
$weight3 = $priority3 * $savingsAmount;
}
weight(&$weight1, &$weight2, &$weight3, $savingsAmount);
function totalCostPercent(&$totalCost, &$percentage, &$percentageRounded, $cost1, $cost2, $cost3, $savingsAmount) {
$totalCost = $cost1 + $cost2 + $cost3;
$percentage = ($savingsAmount / $totalCost) * 100;
$percentageRounded = ceil($percentage);
}
totalCostPercent(&$totalCost, &$percentage, &$percentageRounded, $cost1, $cost2, $cost3, $savingsAmount);
Here is the PHP code, including the SQL query:
$savingsAmount = "";
$goal1 = "";
$goal2 = "";
$goal3 = "";
$cost1 = "";
$cost2 = "";
$cost3 = "";
$weight1 = "";
$weight2 = "";
$weight3 = "";
$totalCost = "";
$percentageRounded = "";
$display = "display:none;";
// Calcuations
function weight(&$weight1, &$weight2, &$weight3, $savingsAmount) {
$priority1 = .40;
$priority2 = .30;
$priority3 = .20;
$weight1 = $priority1 * $savingsAmount;
$weight2 = $priority2 * $savingsAmount;
$weight3 = $priority3 * $savingsAmount;
}
function totalCostPercent(&$totalCost, &$percentage, &$percentageRounded, $cost1, $cost2, $cost3, $savingsAmount) {
$totalCost = $cost1 + $cost2 + $cost3;
$percentage = ($savingsAmount / $totalCost) * 100;
$percentageRounded = ceil($percentage);
}
if(isset($_POST['submit'])) {
$savingsAmount = $_POST['savings-amount'];
$goal1 = $_POST['goal-1'];
$cost1 = $_POST['cost-1'];
$goal2 = $_POST['goal-2'];
$cost2 = $_POST['cost-2'];
$goal3 = $_POST['goal-3'];
$cost3 = $_POST['cost-3'];
$display = "display:block;";
$query2 = "INSERT INTO Numbers(Savings, Goal1, Goal2, Goal3, Cost1, Cost2, Cost3, Weight1,
Weight2, Weight3, TotalCost, PercentRound) VALUES ('$savingsAmount', '$goal1', '$goal2',
'$goal3', '$cost1', '$cost2', '$cost3', '$weight1', '$weight2', '$weight3', '$totalCost',
'$percentageRounded')";
$result = mysqli_query($dbc, $query2)
or die ('Error querying request');
}
Here is the input form, just in case it comes handy:
<form action="form.php" method="post" id="savings-form">
<table>
<!-- SAVINGS AMOUNT -->
<tr>
<td>Savings Amount: $ </td>
<td> <input type="text" id="savings-amount" name="savings-amount" /></td>
<td><div id="savingsError"></div></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<!-- TABLE HEADER-->
<tr>
<td>Priority </td>
<td>Savings Goal/Item </td>
<td>Cost ($)</td>
</tr>
<!-- ROW 1 INFO -->
<tr>
<td>#1</td>
<td><input type="text" id="goal-1" name="goal-1" /></td>
<td><input type="text" id="cost-1" name="cost-1" /></td>
</tr>
<!-- ROW 2 INFO -->
<tr>
<td>#2</td>
<td><input type="text" id="goal-2" name="goal-2" /></td>
<td><input type="text" id="cost-2" name="cost-2" /></td>
</tr>
<!-- ROW 3 INFO-->
<tr>
<td>#3</td>
<td><input type="text" id="goal-3" name="goal-3" /></td>
<td><input type="text" id="cost-3" name="cost-3" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td><div id="costError"></div></td>
</tr>
<!-- SUBMIT -->
<tr>
<td><button type="submit" id="submit" name="submit" onclick="return validateSavings() && validateCost();">Submit</button></td>
</tr>
</table>
</form>
Here is the MySQL table structure:
CREATE TABLE `Numbers` (
`Number` varchar(11), -- Priamry/Foriegn Key
`Savings` varchar(25),
`Goal1` varchar(25),
`Goal2` varchar(25),
`Goal3` varchar(25),
`Cost1` varchar(25),
`Cost2` varchar(25),
`Cost3` varchar(25),
`Weight1` varchar(25),
`Weight2` varchar(25),
`Weight3` varchar(25),
`TotalCost` varchar(25),
`PercentRound` varchar(25)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;