在尝试更新mysql中的列行时,它抱怨
完整性约束冲突:1048列“总金额”不能为空
同时
总金额
脚本中的拼写与
总金额
在数据库表中。
这是更新代码
public function updateOrder(){
$updateOrders = 'UPDATE `orders` SET total_amount = (SELECT SUM(unit_cost * quantity) FROM `order_detail` WHERE ';
$updateOrders .= 'id = ' . $this->key . ') WHERE id = ' . $this->key;
$this->query($updateOrders);
$this->cartEmpty();
return $this->key;
}
这是清理用户输入的代码
private function query($sql, $parameters = []) {
$query = $this->pdo->prepare($sql);
$query->execute($parameters);
return $query;
}
2条答案
按热度按时间t30tvxxf1#
尝试查看表定义。田野
total_amount
可能没有标记为null,因此mysql需要提供该值,或者尝试这样做public function updateOrder(){ $updateOrders = 'UPDATE
订单SET total_amount = IFNULL((SELECT SUM(unit_cost * quantity),0) FROM
订单详情WHERE '; $updateOrders .= 'id = ' . $this->key . ') WHERE id = ' . $this->key; $this->query($updateOrders); $this->cartEmpty(); return $this->key; }
bfrts1fy2#
你可以用
COALESCE()
在子查询中,因为它总是返回一行:您还应该学会使用参数。