【发布时间】:2018-02-02 09:19:38
【问题描述】:
我正在努力做到这一点:
$articles_id = 105; // comes from argument
$type = 'units_out'; // comes from argument
$quantity = 4; // comes from argument
$date = date('Y-m-d');
$sql = "INSERT INTO stock (articles_id, date, units_in, units_out)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE $type = IF(VALUES($type) - $quantity >= 0, VALUES($type) - $quantity, 0)";
$res = $this->db->query($sql, array($articles_id, $date, 0, $quantity));
记录已经存在,所以它会进行更新。 BD中units_out的初始值为2,是一个UNSIGNED MEDIUMINT字段。所以,因为它是无符号类型,2 - 4 = -2,对于 UNSIGNED MEDIUMINT 意味着从最大值开始倒计时。
查询后,units_out=16777215。我想通过使用 MySQL 的 VALUES() 函数来避免这种行为,但如果我将它与 bd->query()
一起使用它就不起作用我已经尝试直接在 PHPMyAdmin 中执行它,它确实有效,units_out 结果为 0。为什么这在 Codeigniter 中不起作用?
【问题讨论】:
标签: php mysql codeigniter codeigniter-3