我正在努力處理一些 PHP 代碼,該代碼將商品的價(jià)格插入 MySQL 數(shù)據(jù)庫(kù)。該價(jià)格被插入,但它總是被舍去。例如,如果我插入一個(gè)數(shù)字 9.99,它會(huì)在數(shù)據(jù)庫(kù)中四舍五入為 9.00。我認(rèn)為這是 PHP 代碼的問(wèn)題,因?yàn)槲以?phpMyAdmin 中使用 sql 語(yǔ)句手動(dòng)插入了數(shù)據(jù),并且數(shù)據(jù)沒(méi)有被四舍五入。我曾經(jīng)item_price DECIMAL(7,2) NOT NULL創(chuàng)建過(guò)列,所以它應(yīng)該接受小數(shù)位。我還曾經(jīng)echo檢查過(guò)正確的值是否從 html 表單到達(dá) .php 文件,并顯示了一個(gè)帶小數(shù)點(diǎn)的值。我$total像這樣定義變量:// Is repeated across multiple functions$price = test_input($_POST["price-2"]);$total += $price這是將信息插入數(shù)據(jù)庫(kù)的代碼部分:echo $total; // Returns a decimal value (eg 9.99)$sql = "INSERT INTO purchase_requests (submitted_by, request_type, summary, cost_centre, location, location_details, transport, total_price, approved, approver, paid) VALUES (?,'purchase',?,?,?,?,'false',?,'pending','pending','pending');";$stmt = mysqli_stmt_init($conn);if(!mysqli_stmt_prepare($stmt, $sql)) { echo "SQL Error";} else { mysqli_stmt_bind_param($stmt, "issssi", $submitter, $summary, $costCentre, $location, $locationDetails, $total); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); mysqli_close($conn);}誰(shuí)能理解為什么會(huì)發(fā)生這種情況?謝謝!
1 回答

12345678_0001
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
您告訴 PHP 將值保存為整數(shù),使用以下代碼行:
mysqli_stmt_bind_param($stmt, "issssi", $submitter, $summary, $costCentre, $location, $locationDetails, $total);
該i
代表“整數(shù)”。如果要插入雙精度值,則必須使用d
.
在您的情況下,您的線路變?yōu)椋?/p>
mysqli_stmt_bind_param($stmt, "issssd", $submitter, $summary, $costCentre, $location, $locationDetails, $total);
- 1 回答
- 0 關(guān)注
- 186 瀏覽
添加回答
舉報(bào)
0/150
提交
取消