[英]Please explain CodeIgniter ActiveRecord set() update()
我似乎無法在用戶指南中找到明確的解釋。
在 CodeIgniter 活動記錄中,要更新表行,似乎需要做三件事:(1)識別要更新的記錄,(2)定義需要更改的內容和(3)提交更改。
不知何故,無論我如何閱讀手冊,都不清楚。 https://www.codeigniter.com/userguide2/database/active_record.html#update
這似乎暗示“設置”是關於插入 - 這意味着在我的書中向表格中添加行。
而“更新”是關於“改變”現有信息。
對我有用的唯一方法是完成所有三個動作。
像這樣:
$this->db->where('id',$userid); //selecting the right user
$this->db->set($SubscriptionChoices); //setting the new values to be written
$this->db->update('userprefs'); //Do it. Update table userprefs
所以你所說的“設置”——它只是你正在更新的特定字段和你正在使用的值。 就像從一個簡單的形式來更新一個名字
$updatedata = array(
'first' => $this->input->post( 'first', TRUE ),
'last' => $this->input->post( 'last', TRUE ),
'statuscheck' => 'Name updated'
);
然后將該數據和記錄 ID 傳遞給一個方法來進行更新
function update( $id, $updatedata ) {
$this->db->where( 'id', $id );
$this->db->update( 'yourtablename', $updatedata );
if ( $this->db->affected_rows() == '1' ) {return TRUE;}
else {return FALSE;}
} //
注意一個煩人的細節——如果你更新一條記錄——使用相同的信息——比如在這種情況下,如果有人更新了記錄但名字和姓氏相同——那么它將返回“假”——因為沒有更新。
因此,如果您在更新數組中包含一個字段,例如日期時間秒總是不同的 - 那么您就不會遇到這個問題。
有點晚了,但其他人可以得到幫助。
您也可以在單個功能中使用它。
$updatedata = array(
'first' => $this->input->post( 'first', TRUE ),
'last' => $this->input->post( 'last', TRUE ),
'statuscheck' => 'Name updated'
);
function update( $id, $updatedata ) {
$where = array( 'id' => $id );
$this->db->update( 'yourtablename', $updatedata , $where);
if ( $this->db->affected_rows() == '1' ) {return TRUE;}
else {return FALSE;}
} //
但是除了使用它,您還可以使用下面提供 CRUD 功能的類。
這可以通過在 ci 中使用 set 進行更新。 model 零件。 示例:公共 function change_province_value() {
$this->db->where('customer_zone',0);
$this->db->set('customer_zone',NULL);
$query = $this->db->update('customer');
return $query;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.