PHP MySQL不更新包含小数的字段
当我尝试使用PHP使用以下查询字符串更新表时:PHP MySQL不更新包含小数的字段,php,mysql,sql-update,Php,Mysql,Sql Update,当我尝试使用PHP使用以下查询字符串更新表时: UPDATE card_designs SET `card_price` = '6180', `annual` = '257.3', `initial_payment` = '6512.3' WHERE card_id = '1' 它没有正确更新卡片价格值输入正确。但是,年度收入为0,初始付款为6255.00 字段是VARCHAR、DECIMAL
UPDATE card_designs SET `card_price` = '6180',
`annual` = '257.3',
`initial_payment` = '6512.3'
WHERE card_id = '1'
它没有正确更新<代码>卡片价格值输入正确。但是,年度收入为0
,初始付款为6255.00
字段是VARCHAR
、DECIMAL
还是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);
我假设您的数据库表字段数据类型为十进制(9,2)
您是直接插入257.3
…`annual`=“257.3”…
,还是在变量中插入,即,$annual=257.3
,您正在执行。。。年度=“$annual”…
?使用PHP术语描述您的。php有数百种运行sql表达式的方法。你使用哪个图书馆?只显示代码,而不是生成的SQL脚本。同时显示DDL表。插入数值时,不要引用它们。你能在本期中添加你的表格描述吗?我也尝试过不加引号。我还试着提出这个问题。为什么这会使查询工作:array_push($updates,“$field=$value”);但这不是吗?如果(是数值($value))数组($push($updates,“$field=$value”);else数组_push($updates,“$field=”$value');我认为你的突破点是$field周围的单引号(使用mysql编辑器),它与$value周围的引号不同“$field
=”$value“。。。
// 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";
}