gpt4 book ai didi

php - 代码仅更新数据库中的 false 值,但不更新 true 值

转载 作者:行者123 更新时间:2023-11-29 11:21:43 31 4
gpt4 key购买 nike

我有一个奇怪的情况,只有错误状态保存到数据库中。当“状态”应该为 true 时,我的查询仍然执行 false。

我有具有此功能的 Controller

public function change_active_state_post()
{
$id['id_location'] = $this->input->post('id_location');
$state['active'] = $this->input->post('state');
var_dump($state['active']);
$this->locations->update($state, $id);
}

这是MY_Model更新函数

function update($data,$conditions,$tablename=""){
if($tablename=="")
$tablename = $this->table;
$this->db->where($conditions);
$this->db->update($tablename,$data);
var_dump($this->db->last_query());
return $this->db->affected_rows();
}

当我请求停用位置时,我的日志中会收到以下数据,并且数据库已正确更新

Location.php:196:string 'false' (length=5)
MY_model.php:46:string 'UPDATE `Locations` SET `active` = 'false'
WHERE `id_location` = '2'' (length=67)

但是当我请求激活位置时,我在日志中收到以下数据,并且数据库值未更新

Location.php:196:string 'true' (length=4)
MY_model.php:46:string 'UPDATE `Locations` SET `active` = 'true'
WHERE `id_location` = '2'' (length=66)

问题是事件列永远不会更新为值 1 或 true。它保持为 0,但如果我不想将其从 1 变为 0,它将始终有效。

数据库中的事件列类型为tinyint(1)

如果您需要任何其他信息,请告诉我,我将提供:提前谢谢您

更新

这项额外检查帮助我正确插入数据

if($this->input->post('state') == 'true'){
$state['active'] = true;
}else{
$state['active'] = false;
}

最佳答案

这里'true'被引用,因此它被作为字符串插入:

MY_model.php:46:string 'UPDATE `Locations` SET `active` = 'true'

在 MySQL 中,两个字符串 'true''false'转换为0当放入tinyint(1)时列,因为两者都不是正确的数值。

使用值 01将工作。 MySQL还定义了常量FALSE ( 0 ) 和 TRUE ( 1 ),因此只要您将它们不加引号插入而不是作为字符串插入,它们也将起作用。

关于php - 代码仅更新数据库中的 false 值,但不更新 true 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38803409/

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