duancheng3042 2013-12-03 11:46
浏览 44

while循环使用复选框php mysql仅插入最后一行

Here is my code.

<?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>

Note: with parenthesis () is the amount

I have a situation if I input the currency sequentially like I select USD and enter an amount (50) and next is EUR (2) it saves two records, saves the amount correctly on the field but if I select EUR (50) and JPY (500) it saves two records but the first record saves the amount into zero the second record saves 50. the first record must be 50 and second must be 500. Is there anything i missing?

EDIT:

<?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;
    }
}
?>

EDIT FOR RESULT OF ARRAY

For the amount: Array ( [0] => [1] => 25 [2] => [3] => 50 [4] => [5] => 25 [6] => 50 )

For the currency: Array ( [0] => EUR [1] => JPY )

  • 写回答

2条回答 默认 最新

  • douwa1304 2013-12-03 11:57
    关注

    Based on your code, if you print out $amount, you will see something like:

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

    while your $currency[0] equals to 'EUR' and $currency[1] equals to 'JPY', it's inconsistent with what you think.

    You need to manually remove those empty value from $amount before execute your while-loop

    Edit: update with solution, use array_filter() to remove empty value in $amount array

    $amount = array_filter($amount); 
    

    array_filter(): "If no callback is supplied, all entries of input equal to FALSE will be removed."

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类