簡體   English   中英

代碼僅更新DB中的false值,而不更新true

[英]Code only updates false value in DB, but not true

當只有錯誤狀態保存到數據庫中時,我有一個奇怪的情況。 當“狀態”為true時,我的查詢仍然執行false。

我有具有此功能的控制器

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'在放入tinyint(1)列時都轉換為0 ,因為這兩個都不是正確的數值。

使用值01將起作用。 MySQL還定義了常量FALSE0 )和TRUE1 ),因此只要您將其插入無引號而不是字符串中,它們也將起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM