我有PHP代码,它使用表单中的数字输入值进行某些计算。这些新计算存储在其他变量中,例如:
$weight1 = $priority1 * $savingsAmount;
我想在提交输入表单时将新计算的变量添加到我的MySQL数据库中(它们是在提交表单的同时计算的)。我尝试创建一个SQL查询来执行此操作,但只将表单输入变量添加到数据库中;不会添加通过计算生成的变量值。
我试图返回/调用两个计算函数,但这使我的网页变为空白。如果有人能告诉我捕获这些变量值(在表单提交后计算)的最佳方法是什么,并将它们与表单输入值一起添加到数据库中,我将不胜感激?
编辑我曾尝试调用这两个函数,但我的网页显示为空白,所以我猜测问题在于我如何调用函数。
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);
这是PHP代码,包括SQL查询:
$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');
}
这是输入表单,以防它方便:
<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>
这是MySQL表结构:
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;
答案 0 :(得分:0)
这些函数在提交函数之外被调用,因此它们将被设置为空白字符串,因为它们设置在页面顶部。
你想做这样的事情:
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;";
totalCostPercent(&$totalCost, &$percentage, &$percentageRounded, $cost1, $cost2, $cost3, $savingsAmount);
weight(&$weight1, &$weight2, &$weight3, $savingsAmount);
$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');
}