将数据插入数据库表字段,类型为decimal(12,2)

时间:2021-11-26 22:25:24

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.

我正在处理csv导入到数据库,我成功读取了csv文件内容,以下代码将内容导入数据库。

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

imp_input_vat_per_client是一个十进制类型的字段名称(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,

我成功地将数据插入数据库,但是所有行中字段imp_input_vat_per_client的值是,

  0.00

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

你能解释一下会发生什么吗?怎么办呢?

1 个解决方案

#1


0  

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']))),

#1


0  

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']))),