am i writing this update query correctly?
this is the sql query开发者_如何学JAVA i used before i switch to codeigniter
UPDATE transaction SET due_amount = to_pay-$payment_amount WHERE id = '$id'
with codeigniter
$this->db->set('due_amount', 'to_pay-'.$payment_amount, FALSE);
$this->db->where('id', $id);
$result = $this->db->update('transaction');
in the codeigniter code am i writing $this->db->set correctly?
Regards
You should do it like this:
$this->db->where('id', $id);
$result = $this->db->update('transaction',array('due_amount'=>'to_pay-'.$payment_amount));
Hope this helps.
There are multiple variants to use the update
Docs function. Yours doesn't look specifically bad (I would however don't bypass escaping if not for a specific reason), maybe you prefer to use an array (or object) to provide the data being updated:
$data = array(
'due_amount' => 'to_pay-'.$payment_amount,
);
$this->db->update('transaction', $data, sprintf('id = %d', $id));
I think here could be some issue with $this->db->set
Standard way of updating content in codeignitor is this
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Produces: UPDATE mytable SET title = '{$title}', name = '{$name}', date = '{$date}' WHERE id = $id
精彩评论