Reputation: 7935
I have two functions in my model. First one:
public function updateOwn($game,$own,$user) {
$data = array(
'own' => $own
);
$q = $this->db->where(array(
'game' => $game,
'user' => $user
));
$q = $this->db->update('ownership',$data);
if($q) { return true; } else { return false; }
}
and the secound one:
public function updateRate($game,$rate,$user) {
$data = array(
'rate' => $rate
);
$q = $this->db->where(array(
'game' => $game,
'user' => $user
));
$q = $this->db->update('rates',$data);
$q = $this->db->update('ownership',$data);
if($q) { return true; } else { return false; }
}
The problem is, first one is working, secound one isn't. I mean, it works, but it updates every row, not just those with matching game
and user
. Variables are passed fine.
Upvotes: 1
Views: 820
Reputation: 673
Pass the where array as a third parameter to make sure it is working properly:
$q = $this->db->update('rates',$data,array(
'game' => $game,
'user' => $user
));
Upvotes: 0
Reputation: 792
First of all, why do you need to set $q variable every time? Second, in the last statement you check only if second query was done.
I would do something like:
public function updateRate($game,$rate,$user) {
$data = array(
'rate' => $rate
);
$this->db->where('game', $game)->where('user', $user);
$first_query = $this->db->update('rates',$data);
$this->db->where('game', $game)->where('user', $user);
$second_query = $this->db->update('ownership',$data);
return $first_query && $second_query;
}
Upvotes: 0
Reputation: 18290
CodeIgniter resets a lot of the query params that you build after you execute it. You'll need to re-add the WHERE clauses after the first update() call.
public function updateRate($game,$rate,$user) {
$data = array(
'rate' => $rate
);
$q = $this->db->where(array(
'game' => $game,
'user' => $user
));
$q = $this->db->update('rates',$data);
$q = $this->db->where(array(
'game' => $game,
'user' => $user
));
$q = $this->db->update('ownership',$data);
if($q) { return true; } else { return false; }
}
Upvotes: 0
Reputation: 160863
The where condition is only applied to the first update statement. Try below.
$data = array(
'rate' => $rate
);
$where = array(
'game' => $game,
'user' => $user
);
$q = $this->db->update('rates', $data, $where);
$q = $this->db->update('ownership', $data, $where);
Upvotes: 2