I think the question is clear enough. Here are some details...
Sample data:
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) ""
}
}
Then I am just looping trough data and inserting it into database like this:
$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
is a constant
Database:
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;
What I tried so far:
1.According to PDO::PARAM for type decimal
There isn't any PDO::PARAM for decimals / floats, you'll have to use PDO::PARAM_STR
$stmt->bindValue(':price', $position['price'], PDO::PARAM_STR);
2.
$stmt->bindValue(':price', $position['price']);
3.
$stmt->bindValue(':price', (float) $position['price']);
I always successfully insert anything but cant insert exactly 0.00
... It can be anywhere, loop doesnt break it still loops if its on very beginning or somewhere in the middle or at the end. asd
EDIT I was asked to add error message:
In my loop I collect errors and then output them:
$stmt->execute();
$errors[] = $stmt->errorInfo();
array(1) {
[0]=>
array(3) {
[0]=>
string(5) "00000"
[1]=>
NULL
[2]=>
NULL
}
}