gpt4 book ai didi

PHP MySQL 不更新包含小数的值的字段

转载 作者:行者123 更新时间:2023-11-28 23:38:35 24 4
gpt4 key购买 nike

当我尝试使用 PHP 使用以下查询字符串更新表时:

UPDATE card_designs SET `card_price` = '6180', 
`annual` = '257.3',
`initial_payment` = '6512.3'
WHERE card_id = '1'

它没有正确更新。 card_price 值输入正确。然而,每年以 0 的形式出现,而 initial_payment6255.00 的形式出现。

字段是VARCHARDECIMAL 还是DOUBLE 并不重要。如果该值有小数,则一切都搞砸了。

此外,如果我在 SQL 客户端中运行上述查询,查询工作正常。

这是构造查询的 PHP 代码。我正在使用 mysqli:

$sql = "UPDATE ". $table ." SET ";
$updates = array();
foreach ($variables as $field => $value) {
array_push($updates, "`$field` = '$value'");
}
$sql .= implode(', ', $updates);

//Add the $where clauses as needed
if (!empty($where)) {
foreach ($where as $field => $value) {
$value = $value;

$clause[] = "$field = '$value'";
}
$sql .= ' WHERE '. implode(' AND ', $clause);
}

if (!empty( $limit)) {
$sql .= ' LIMIT '. $limit;
}

$query = $this->mysqli->query($sql);

最佳答案

我假设您的数据库表字段数据类型是 Decimal(9,2)

// Prepare query
$table = "card_designs";
$variables = array(
"card_price" => "6180.00",
"annual" => "257.3",
"initial_payment" => "6512.3"
);

$where = array(
"id" => "1"
);

$sql = "UPDATE ". $table ." SET ";
$updates = array();
foreach ($variables as $field => $value)
{
array_push($updates, "$field = $value");
}

$sql .= implode(', ', $updates);
//Add the $where clauses as needed
if (!empty($where))
{
foreach ($where as $field => $value)
{
$value = $value;
$clause[] = "$field = $value";
}

$sql .= ' WHERE '. implode(' AND ', $clause);
}

if (!empty( $limit))
{
$sql .= ' LIMIT '. $limit;
}

// Run query
if ($mysqli->query($sql))
{
echo "Record updated successfully";
}

关于PHP MySQL 不更新包含小数的值的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com