duanmu0834 2015-12-06 22:41
浏览 69
已采纳

将函数计算变量添加到MySQL

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;
  • 写回答

1条回答 默认 最新

  • dqaq59269 2015-12-06 22:50
    关注

    The functions are being called outside of the submit function, so they'll remain set as blank strings as they were set at the top of the page.

    You want to do something like this:

    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');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程