mysql十进制和双数据问题

时间:2015-11-26 02:18:55

标签: php mysql

我正在尝试将货币数据存储在mysql中。在这里,我使用了decimaldouble数据类型。

这是我的表结构:

CREATE TABLE IF NOT EXISTS transactions (
  tb_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  tb_our_rate DECIMAL(15, 2) NOT NULL,
  tb_bank_rate DECIMAL(15, 2) NOT NULL,
  tb_lkr_funds DOUBLE(15, 2) NOT NULL,
  tb_aud_funds DOUBLE(15, 2) NOT NULL,  
  PRIMARY KEY (tb_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

我尝试在这些列中插入一些值。这是我插入的值。

133.11
120.20
-12312,123,123.02
-12312,231,423.32

这是它插入数据库的方式:

mysql> select * from transactions\G
*************************** 1. row ***************************
   tb_our_rate: 13311.00
  tb_bank_rate: 12020.00
  tb_lkr_funds: -1231212312302.00
  tb_aud_funds: -1231223142332.00
    date_added: 2015-11-26 07:36:01
*************************** 2. row ***************************

更新: 这就是我的插入查询的工作方式:

$our_rate   = filter_input(INPUT_POST, 'our_rate', FILTER_SANITIZE_NUMBER_FLOAT);
$bank_rate  = filter_input(INPUT_POST, 'bank_rate', FILTER_SANITIZE_NUMBER_FLOAT);
$lkr_funds  = filter_input(INPUT_POST, 'lkr_funds', FILTER_SANITIZE_NUMBER_FLOAT);
$aud_funds  = filter_input(INPUT_POST, 'aud_funds', FILTER_SANITIZE_NUMBER_FLOAT);

$query = "INSERT INTO transaction_banks( 
                              tb_our_rate
                            , tb_bank_rate
                            , tb_lkr_funds
                            , tb_aud_funds
                            , date_added
                            ) VALUES (?, ?, ?, ?)";
$insert_stmt = $mysqli->prepare($query);

if ($insert_stmt){
    $insert_stmt->bind_param('dddd', 
                                $our_rate
                              , $bank_rate
                              , $lkr_funds
                              , $aud_funds
                            );
  $insert_stmt->execute(); 
}

有谁能告诉我这是什么问题?

2 个答案:

答案 0 :(得分:1)

删除FILTER_SANITIZE_NUMBER_FLOAT。这会删除输入中的.

选中https://maven.apache.org/plugins/maven-dependency-plugin/examples/purging-local-repository.html

它会对输入进行清理,并且只允许+-

您可以使用FILTER_FLAG_ALLOW_FRACTION

$our_rate   = filter_input(INPUT_POST, 'our_rate', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$bank_rate  = filter_input(INPUT_POST, 'bank_rate', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$lkr_funds  = filter_input(INPUT_POST, 'lkr_funds', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$aud_funds  = filter_input(INPUT_POST, 'aud_funds', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);

检查documentation之前有关此问题的问题。

答案 1 :(得分:0)

这对我来说很好看。不要在数字中使用逗号插入。我甚至害怕尝试。

create table thing99
(   id int auto_increment primary key,
    tb_our_rate DECIMAL(15, 2) NOT NULL,
    tb_bank_rate DECIMAL(15, 2) NOT NULL,
    tb_lkr_funds DOUBLE(15, 2) NOT NULL,
    tb_aud_funds DOUBLE(15, 2) NOT NULL
);


insert thing99(tb_our_rate,tb_bank_rate,tb_lkr_funds,tb_aud_funds) values
(133.11,120.20,-12312123123.02,-12312231423.32);

select * from thing99;
+----+-------------+--------------+-----------------+-----------------+
| id | tb_our_rate | tb_bank_rate | tb_lkr_funds    | tb_aud_funds    |
+----+-------------+--------------+-----------------+-----------------+
|  1 |      133.11 |       120.20 | -12312123123.02 | -12312231423.32 |
+----+-------------+--------------+-----------------+-----------------+

编辑:

刚刚找到它。你必须使用下面的选项2.

$our_rate   = filter_input(INPUT_POST, 'our_rate', FILTER_SANITIZE_NUMBER_FLOAT);

$our_rate2=filter_input(INPUT_POST,'our_rate',
FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);



echo $our_rate."<br>";
echo $our_rate2."<br>";

<强>测试

来自Filter Flags上的手册页和FILTER_FLAG_ALLOW_FRACTION:

enter image description here