0

データベース内の特定のユーザーを更新したい場合、(whereステートメント)を2回選択する必要があるかどうか疑問に思っています。

例、

// 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」ステートメントを実行する必要がありますか?

4

2 に答える 2

1

where条件を2回記述する必要がありますが、次のように1行で試すことができます。

$this->db->where(array('day'=>$this->current_date, 'month'=>$this->current_month, 'user_id'=>$user_id, 'budget_group_id'=>$budget_group_id));
于 2012-11-22T09:30:14.670 に答える
0

の新しいwhere条件を作成する必要がありupdateます。すべてのwhere条件をに保存し、必要に応じてarrayそれを使用することarrayをお勧めします。selectupdate

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

あなたの場合、2つのクワイアは必要ないようですupdate。クエリを実行するだけです。$this->db->affected_rows()何かが更新されたかどうかを確認するために使用できます。

于 2012-11-22T09:30:37.240 に答える