2012-11-22 2 views
0

나는 데이터베이스에서 spesific 사용자를 업데이 트하려는 경우 (어디 진술) 두 번 선택해야할까요 궁금하네요.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

당신은 두 곳 조건을 작성해야하지만, 다음과 같이 한 줄에 시도 할 수 있습니다 :

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

당신은 update 새로운 where 조건을 작성해야합니다. where 조건을 모두 array에 저장하고, 필요한 경우 selectupdate에 대해 array을 사용하는 것이 더 좋습니다. 귀하의 경우에는

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

그것은 그냥 update 쿼리를 실행하는 데 필요한 두 첩의 필요를 보인다. $this->db->affected_rows()을 사용하여 무언가가 업데이트되었는지 확인할 수 있습니다.