I'm just writing a script to enter payments into a database and allocate them to various categories. It seemed to be working OK until I came to enter a particular set of values.
If I enter 20.56 into $bank_uk and allocate for example 11.75 and 8.81 to categories the script echos the total amount entered (20.56) and the total allocated amount (20.56) but says that they do not match!
If I enter 20.57 into $bank_uk and allocate for example 11.76 and 8.81 to categories the script succesfully enters the correct amounts into the database and displays the result!
Does anyone know why this strange behaviour should occur? I've tried all sorts of amounts and only the first case above seems to fail to validate.
$total_entered = 0; // initialise total amount to check against total allocated
if (!empty($_POST['bank_uk'])) {
$bank_uk = $_POST['bank_uk'];
$total_entered = $total_entered + $bank_uk;
$insert_query_fields = "bank_uk, ";
$insert_query_values = "'$bank_uk', ";
} else {
$bank_uk = NULL;
}
// check that funds have been allocated and that totals agree with amount of cash/bank entered
$allocation = 0; // initialise total amount allocated to a category
if (!empty($_POST['domains'])) {
$domains = $_POST['domains'];
$allocation = $allocation + $domains;
$insert_query_fields .= "domains, ";
$insert_query_values .= "'$domains', ";
} else {
$domains = NULL;
}
if (!empty($_POST['travel'])) {
$travel = $_POST['travel'];
$allocation = $allocation + $travel;
$insert_query_fields .= "travel, ";
$insert_query_values .= "'$travel', ";
} else {
$travel = NULL;
}
if (!empty($_POST['premises'])) {
$premises = $_POST['premises'];
$allocation = $allocation + $premises;
$insert_query_fields .= "premises, ";
$insert_query_values .= "'$premises', ";
} else {
$premises = NULL;
}
if ($allocation != $total_entered) {
$allocated = FALSE;
$allocation_msg = "Amount(s) entered do not match amount(s) allocated<br />Total entered = $total_entered<br />Allocated = $allocation<br />";
} else if ($allocation <= 0) {
$allocated = FALSE;
$allocation_msg = "Funds not allocated<br />";
} else {
$allocated = TRUE;
$allocation_msg = NULL;
}