Fil Fil - 6 months ago 10
PHP Question

Insert data to database table field with type decimal(12,2)

I'm working on

csv
import to database, I Successfully read the
csv
file contents, and the following code will import the content to the database.

<?php
foreach ($data as $value) {
$data = array (
'checklist_item_id' => ($this->table == 'checklist_item') ? $this->id : null,
'imp_sequence_no' => $value['Sequence No.'],
'imp_vendor_tin' => $value['Vendor TIN'],
'imp_vendor_name' => $value['Vendor Name'],
'imp_input_vat_per_client' => $value['Input VAT per client'],
'imp_gsi ' => $value['Goods/Services/Importations']
);
$this->db->insert("transaction", $data);
}
?>


This line

'imp_input_vat_per_client' => $value['Input VAT per client'],


of the code, has a value with a decimal point, and sometimes with comma.

for example:

624.00
312.00
1,137.57


imp_input_vat_per_client
, is a field name with a type
decimal(12,2)
.

I successfully insert the data to the database but, the value of the field
imp_input_vat_per_client
in all rows are,

0.00


Can you give explanation what happen?
What to do with it?

Answer

Change:

'imp_input_vat_per_client' => $value['Input VAT per client'],

to

'imp_input_vat_per_client' => floatval(str_replace(',', '', trim($value['Input VAT per client']))),