0

I've been trawling the web for hours now and trying different methods, and I can't work out why PDO can't insert any row where one of the values contains a decimal.

For example, if the value entered into the cost field has no decimal value then it works fine. But anything like with a decimal and it just ignores the whole row.

200 works, even 200.00 works. But things like 39.99 don't.

Here's the code:

$invoice_id = $db->lastInsertId('id');

$item_name = $_POST['item_name'];
$item_qty = $_POST['item_qty'];
$item_cost = $_POST['item_cost'];
$item_vat = $_POST['item_vat'];

for($i = 0; $i < count($item_name); $i++) {
$item_query = $db->prepare("INSERT INTO hm_invoice_items(invoice, item, qty, amount, vat) VALUES(:invoice, :item, :qty, :amount, :vat)");
$item_query->bindParam(":invoice", $invoice_id);
$item_query->bindParam(":item", $item_name[$i]);
$item_query->bindParam(":qty", $item_qty[$i]);
$item_query->bindParam(":amount", $item_cost[$i]);
$item_query->bindParam(":vat", $item_vat[$i]);

if (!$item_query->execute())
    {
        die(showMessage("There has been a problem adding the invoice items.", "Error!"));
    }
}

A var_dump tells me that the insert query is receiving the values, but it does not like dealing with decimals.

4

3 に答える 3

0

小数点記号に問題がある可能性があります。
このようなケースをデバッグするときは、e-ve-ry-thing が不可欠です 詳細な検査のために、値を var_dump してみませんか? POST やその他の値を使用せずに、なぜ 10 進数だけで遊ばなかったのですか?var_dump()

「PDO で 10 進値を挿入できません」というタイトルの質問には、10 進値が読者に提示される短い再現可能なコードと結果が含まれている必要があります。
間接的な尺度で判断することはあなたにとって何の役にも立たず、特に見知らぬ人からの助けは得られません。

「var_dump your values」は、次のようなすべての疑わしい値を意味します

var_dump($item_cost[$i]);


出力が得られない場合は 、ループ内に空の値があるため、何も挿入されないのも不思議ではありません。

ちなみに、明らかに10進数のitem_cost値を明らかに整数amountフィールドにバインドしています。タイプミスですか?

しかし、繰り返しになりますが、1 つの挿入クエリ、1 つのハードコードされた 10 進数値、および 1 つの結果を含む特定の再現可能なプルーフコードはどこにありますか? うーん - そしてもちろんテーブル定義。

于 2013-02-18T18:07:20.463 に答える
0

これを試して?

$item_query->bindParam(":amount", floatval($item_cost[$i]));

少なくとも、PDO を使用して MySQL の 10 進データ型を処理する場合はうまくいきます。

于 2015-10-31T05:52:42.560 に答える
-1

以下を使用して、PHP スクリプトに POST されたすべての値の内容を表示します。

print_r($_POST);
于 2014-01-01T22:16:23.057 に答える