我正在挣扎于一些PHP代码,它将项目的价格插入到MySQL数据库中。价格正在被插入,但它总是被四舍五入。
例如,如果我插入一个9.99的数字,它将被舍入到数据库中的9.00。
我认为这是PHP代码中的一个问题,因为我已经用phpMyAdmin中的一个sql语句手动插入了数据,而且数据没有四舍五入。
我使用item_price DECIMAL(7,2) NOT NULL来创建列,因此它应该接受十进制。
我还使用echo检查正确的值是否到达html表单中的.php文件,并显示一个小数点的值。
我定义的$total变量如下:
// Is repeated across multiple functions
$price = test_input($_POST["price-2"]);
$total += $price下面是将信息插入数据库的代码部分:
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);
}有人能理解为什么会发生这种事吗?谢谢!
发布于 2019-08-17 16:56:11
您正在告诉PHP将值保存为整数,并使用以下代码行:
mysqli_stmt_bind_param($stmt, "issssi", $submitter, $summary, $costCentre, $location, $locationDetails, $total);i代表“整数”。如果要插入双值,则必须使用d。
在你的例子中,你的台词变成:
mysqli_stmt_bind_param($stmt, "issssd", $submitter, $summary, $costCentre, $location, $locationDetails, $total);https://stackoverflow.com/questions/57538224
复制相似问题