2015-10-18 66 views
1

我为easyappointments.org库使用CodeIgniter框架。我试图通过行的ID更新字段data。其实这是我的更新代码:如何更新CodeIgniter更新中的特定字段?

return $this->db 
       ->update('ea_appointments', $appointment) 
       ->where('ea_appointments.id', $appointment_id); 

的问题是,该update方法需要两个参数(表进行更新,以分贝的字段名称列关联数组)。在我上面的函数中,我只通过了$appointment_id,所以变量$appointment是空的并且不包含任何关联数组。我想知道如何只更新字段data1。并保持其他领域在相同的价值条件。 这是表的结构:

id|book|start|end|notes|hash|unavailable|provider|data 

逻辑例如:

以前的状态行:

id => 0 
book => example 
start => 18/10/2015 
end => 19/10/2015 
notes => empty 
hash => akdjasldja 
unavailable => false 
provider => 5 
data => 0 

我在功能$appointment_id通过用0值。我等待这个新的结果:

id => 0 
book => example 
start => 18/10/2015 
end => 19/10/2015 
notes => empty 
hash => akdjasldja 
unavailable => false 
provider => 5 
data => 1 

所以,主要的问题是先检索特定行的所有字段值和以后更新?或者类似的东西。有人能帮助我吗?

回答

4

在我上面的函数中,我只传递$ appointment_id,所以变量 $ appointment是空的并且不包含任何关联数组。

如果你只是想列data更新为1为appointment_id 0然后在阵列传递与data密钥和1的价值。

$appointment_id = 0; 
$appointment = array('data' => 1);  
$this->db->where('id', $appointment_id); 
$this->db->update('ea_appointments', $appointment); 
+0

哦,你是对的!感谢这非常有用。只是另一个问题,如果我想选择字段“数据”0的ea_appointments表的所有行? – Dillinger

+0

@Dillinger https://ellislab.com/codeigniter/user-guide/database/active_record.html#select – FuzzyTree

+0

@Dillinger将你的'where'改为' - > where('data',0)' – FuzzyTree