2012-11-22 22 views
0

我想知道是否需要選擇(where語句)兩次,如果我想tp更新數據庫中的特定用戶。codeigniter,選擇兩次或不是

例,

// Add new income for the day 
function add_new_income($user_id, $budget_group_id, $day, $month, $year, $income) 
{ 

    // Check if there has been posted anything at the same day 
    $this->db->where('day',$this->current_date); 
    $this->db->where('month',$this->current_month); 
    $this->db->where('user_id',$user_id); 
    $this->db->where('budget_group_id',$budget_group_id); 

    $query = $this->db->get('budget_day',1); 

    // Check if something was found 
    if($query->num_rows() > 0) 
    { 
     // If something was found, update the value 
     $data = array('income_total' => $income); 
     $this->db->update('budget_day',$data); 
    } 

} 

將這項工作?或者我必須運行新的「db-> where」語句?

回答

1

你需要寫where條件的兩倍,但是你可以按照如下嘗試在單行:

$this->db->where(array('day'=>$this->current_date, 'month'=>$this->current_month, 'user_id'=>$user_id, 'budget_group_id'=>$budget_group_id)); 
0

您必須編寫新where條件update。如果必須的話,最好將所有where條件存儲在array中,並將array用於selectupdate

​​

你的情況,似乎沒有需要兩個奎雷斯的,你只需要運行update查詢。您可以使用$this->db->affected_rows()來檢查是否有更新。