【问题标题】:while loop inserts only the last row using checkbox php mysqlwhile 循环使用复选框 php mysql 仅插入最后一行
【发布时间】:2013-12-03 11:46:39
【问题描述】:

这是我的代码。

<?php
if(isset($_POST['submit']) {
    $currency  =  $_POST['currency'];
    $amount    =  $_POST['amount'];
    $integer   =  0;
    //Loop using while, check the count
    while(count($currency) > $integer) {

        $currencyArray         = array(
                                        'currency'      =>  $currency[$integer],
                                        'amount'            =>  $amount[$integer]
                                        );

        //Insert statement constructed to array
        //..... insert into        
        //Stop loop when reach to the limit
        $integer = $integer + 1;
    }
}
?>

<form action = "" method = "post">
<input type="checkbox" class="currency" name="currency[]" value="USD">USD
<input type="text" name="amount[]" value = "">

<input type="checkbox" class="currency" name="currency[]" value="EUR">EUR
<input type="text" name="amount[]" value = "">

<input type="checkbox" class="currency" name="currency[]" value="JPY">JPY
<input type="text" name="amount[]" value = "">

<input type="checkbox" class="currency" name="currency[]" value="PHP">PHP
<input type="text" name="amount[]" value = "">


<input type = "submit" name = "submit">

</form>

注意:带括号()的是金额

如果我按顺序输入货币,例如选择美元并输入金额(50),然后是欧元(2),它会保存两条记录,在字段中正确保存金额,但如果我选择 EUR (50) 和 JPY (500) 它会保存两条记录,但第一条记录将金额保存为 zero 第二条记录保存50。第一条记录必须是 50,第二条记录必须是 500。我有什么遗漏吗?

编辑:

<?php
if(isset($_POST['submit']) {
    $currency  =  $_POST['currency'];
    $amount    =  array();
    $integer   =  0;

    foreach($_POST['amount'] as $amount_value) {
       if($amount_value != '')    { 
         $amount[]   =  $amount_value;
       }
    }
    //Loop using while, check the count
    while(count($currency) > $integer) {

        $currencyArray         = array(
                                        'currency'      =>  $currency[$integer],
                                        'amount'            =>  $amount[$integer]
                                        );

        //Insert statement constructed to array
        //..... insert into        
        //Stop loop when reach to the limit
        $integer = $integer + 1;
    }
}
?>

编辑数组结果

金额: 数组([0] => [1] => 25 [2] => [3] => 50 [4] => [5] => 25 [6] => 50)

对于货币: 数组( [0] => 欧元 [1] => 日元)

【问题讨论】:

    标签: php mysql loops


    【解决方案1】:

    这就是为什么总是设置amount[],因为没有值也是文本字段中的值! 另一边的货币只有在选中复选框时才会设置。

    所以count($amount) 总是 4 而count($currency) 取决于选中的复选框。

    如果您现在选择 50 欧元和 100 日元并循环遍历数组,则会发生以下情况:

    $amount[0] = "",因为第一个金额字段为空

    $currency[0] = "EUR",因为美元被忽略了

    $amount[1] = "50",因为 50 在下一个字段中,即 EUR

    $currency[1] = "JPY",因为下一个复选框是日元

    【讨论】:

    • 你可以试试:$amount = array(); foreach($_POST['amount'] as $amount_value) { if($amount_value != "") $amount[] = $amount_value; } 而不是 $amount = $_POST['amount'];
    • 币种怎么样?
    • 只有在选中复选框时才应设置货币。是这样吗?
    • 当你试图用货币做同样的事情时? (foreach...)
    • 货币保存没有问题。
    【解决方案2】:

    根据您的代码,如果您打印出$amount,您将看到如下内容:

    $amount[0] equals ""
    $amount[1] equals "50"
    $amount[2] equals "500"
    $amount[3] equals ""
    

    虽然您的$currency[0] 等于“欧元”,$currency[1] 等于“日元”,但与您的想法不一致。

    在执行 while 循环之前,您需要手动从 $amount 中删除那些空值

    编辑:更新解决方案,使用array_filter() 删除$amount 数组中的空值

    $amount = array_filter($amount); 
    

    array_filter(): "如果没有提供回调,所有等于 FALSE 的输入条目都将被删除。"

    【讨论】:

    • 金额$amount[0]等于50,$amount[1]等于500怎么办?
    • 刚刚用解决方案更新了我的答案,在你的 while(count($currency) > $integer) 之前使用 array_filter() 行
    • 抱歉没有用。把 $amount = array_filter($_POST['amount'])
    • 一旦你删除了 $amount 中的空值,那么你的程序应该可以正常运行。无需为您的 $currency 数组做任何其他事情。
    猜你喜欢
    • 2018-04-01
    • 2017-02-12
    • 2014-07-07
    • 2012-09-14
    • 2012-08-11
    • 2012-04-23
    • 2019-01-21
    • 2015-01-14
    • 2022-01-07
    相关资源
    最近更新 更多