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 )

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."

That's why because amount[] is always set, because no value is also a value in a text field! Currency on the other side is only set if the checkbox is selected.

So count($amount) is always 4 whereas count($currency) depends on the selected checkboxes.

If you now select 50 EUR and 100 JPY and loop through the array then the follow will happen:

$amount[0] = "", because the first amount field is empty

$currency[0] = "EUR", because USD is ignored

$amount[1] = "50", because 50 is in the next field, which is EUR

$currency[1] = "JPY", because the next checkbox is JPY