【发布时间】:2016-09-09 08:28:49
【问题描述】:
我认为这个问题已经很清楚了。以下是一些细节...
样本数据:
array(2) {
[0]=>
array(5) {
["index"]=>
string(1) "1"
["source"]=>
string(0) ""
["target"]=>
string(0) ""
["price"]=>
string(6) "153.00"
["order"]=>
string(5) "19442"
}
[1]=>
array(5) {
["index"]=>
string(1) "2"
["source"]=>
string(5) "Test1"
["target"]=>
string(5) "Test2"
["price"]=>
string(4) "0.00"
["order"]=>
string(0) ""
}
}
然后我只是循环槽数据并将其插入数据库,如下所示:
$sql = "INSERT INTO `os_bill_position`
(`position_index`, `source_name`, `target_name`, `price_netto`, `FID_bill`, `FID_order`)
VALUES
(:index, :source, :target, :price, :billID, :orderID)";
$stmt = $link->prepare($sql);
foreach ($allPositions as $position) {
$stmt->bindValue(':index', $position['index']);
$stmt->bindValue(':source', $position['source']);
$stmt->bindValue(':target', $position['target']);
$stmt->bindValue(':price', $position['price'], PDO::PARAM_STR);
$stmt->bindValue(':billID', $billID);
$stmt->bindValue(':orderID', $position['order']);
$stmt->execute();
}
$billID 是一个常数
数据库:
CREATE TABLE `os_bill_position` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`position_index` tinyint(3) unsigned NOT NULL,
`source_name` varchar(255) NOT NULL,
`target_name` varchar(255) NOT NULL,
`price_netto` decimal(10,2) DEFAULT NULL,
`FID_bill` int(10) unsigned NOT NULL,
`FID_order` int(10) unsigned DEFAULT NULL,
`position_tax` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `FID_bp_bill` (`FID_bill`),
KEY `FID_bp_order` (`FID_order`),
CONSTRAINT `FID_bp_bill` FOREIGN KEY (`FID_bill`) REFERENCES `os_bill` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `FID_bp_order` FOREIGN KEY (`FID_order`) REFERENCES `os_order` (`ID`) ON DELETE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
到目前为止我尝试了什么:
1.根据PDO::PARAM for type decimal
小数/浮点数没有任何 PDO::PARAM,您必须使用 PDO::PARAM_STR
$stmt->bindValue(':price', $position['price'], PDO::PARAM_STR);
2。
$stmt->bindValue(':price', $position['price']);
3。
$stmt->bindValue(':price', (float) $position['price']);
我总是成功插入任何东西,但不能准确插入0.00...它可以在任何地方,如果它在一开始或中间或最后的某个地方,循环不会中断它仍然循环。 asd
编辑我被要求添加错误消息:
在我的循环中,我收集错误然后输出它们:
$stmt->execute();
$errors[] = $stmt->errorInfo();
array(1) {
[0]=>
array(3) {
[0]=>
string(5) "00000"
[1]=>
NULL
[2]=>
NULL
}
}
【问题讨论】:
-
您收到的错误信息是什么?
-
其实什么都不清楚。 没有人知道为什么你不能插入东西。如果您尝试插入 0.00,我们甚至都不知道会发生什么
-
请检查编辑。我添加了错误(什么都没有)。如何编辑我的问题以使其更清楚?我提供了所有我能想到的有助于回答的细节。
-
它可能将“0.00”作为字符串提交,因为点将其丢弃。我建议您尝试@DevDonkey 的建议。这会将变量绑定为浮点数,而不是任何其他类型。
-
当您尝试插入 0.00 时,您可以知道实际结果是什么。一个空值?一个错误?爆炸毁了你的房子?