Sugumar Venkatesan
Sugumar Venkatesan

Reputation: 4038

mysql : updating existing column using the column value in prepared query

Old query was like this

update tbl_order set `pack_id` =0, `pack_down_count`=pack_down_count + '".intval($_POST['item_number'])."',`price`='".$_POST['mc_gross']."', `otime`=Now(), `ptime`=Now(), status='paid', pack_download=pack_download + '".intval($_POST['item_number'])."' where member_id='".$_MYVAR['userid']."'"

I liked to change it to prepared statment , I am using a class from https://github.com/joshcam/PHP-MySQLi-Database-Class

for updating

I changed the above query to

       $data = array(
       'pack_id'              =>$_MYVAR['pack_id'], 
       'pack_apply_count'      =>`pack_apply_count` + intval($_POST['item_number']),
       'price'                =>$_POST['mc_gross'], 
       'otime'                =>date('Y-m-d H:i:s'), 
       'ptime'                =>date('Y-m-d H:i:s'), 
       'status'               =>'paid', 
       'pack_apply'        =>`pack_apply` + intval($_POST['item_number'])
       );
       $db->where('member_id',$_MYVAR['userid']);
       $db->update("tbl_order",$data);

also tried without backticks around pack_apply_count and pack_apply but didn't update my table

       $data = array(
       'pack_id'              =>$_MYVAR['pack_id'], 
       'pack_apply_count'      =>pack_apply_count + intval($_POST['item_number']),
       'price'                =>$_POST['mc_gross'], 
       'otime'                =>date('Y-m-d H:i:s'), 
       'ptime'                =>date('Y-m-d H:i:s'), 
       'status'               =>'paid', 
       'pack_apply'        =>pack_apply + intval($_POST['item_number'])
       );
       $db->where('member_id',$_MYVAR['userid']);
       $db->update("tbl_order",$data);

Please tell correct way to update the table

Upvotes: 0

Views: 38

Answers (1)

Frankey
Frankey

Reputation: 3757

The link you provided will give you the information you need, look at the section Update Query.

It seems you can increment with $db->inc($_POST['item_number'])

As shown in the example:

$data = Array (
    'firstName' => 'Bobby',
    'lastName' => 'Tables',
    'editCount' => $db->inc(2),
    // editCount = editCount + 2;
    'active' => $db->not()
    // active = !active;
);

Upvotes: 1

Related Questions