0

I have a database record with a uniqueID/PrimaryKe (OrderNumber)

I want to update the record with new data for that same PrimaryKey, OrderNumber.

My Controller is:

 $data = array(
      'CustomerName' =>  $this->input->post('customer'),
      'CustomerAccountCode' =>  $this->input->post('accountcode'),
      'PeriodStart' =>  substr($this->input->post('period'), 0,10),
      'OrderUnitOfMeasure' =>  $this->input->post('buom'),
      'CreditLimit' =>  $this->input->post('creditlimit'),
      'BalanceBeforeOrder' =>  $this->input->post('currentbalance'),
      'BalanceAfterOrder' =>  $this->input->post('newbalance'),
      'OrderLines' =>  $this->input->post('orderlines'),
      'TotalCost' =>  $this->input->post('grandtotal'),
      'AverageDiscount' =>  $this->input->post('avediscount'),
      'TotalCubes' =>  $this->input->post('grandtotalcubes'),
      'TreatedCubes' =>  $this->input->post('grandtotaltreatedcubes'),
      'SpecialComments' =>  $this->input->post('specialcomments'),
      'CustomerReference' =>  $this->input->post('customerref'),
      'ordernumber' =>  $this->input->post('ordernumber')
      );

    $this->sales_model->update_order_data($data);

My model is:

function update_order_data($q){
    $this->db->where('CustomerOrderID', 'OrderNumber'); //ordernumber being post input ordernumber in array
    $query = $this->db->update('Customer_Order_Summary');
}

So what I want is :

update 'Customer_Order_Summary' 
set 'CustomerName'="$this->input->post('customer')",
set 'CustomerAccountCode'="$this->input->post('accountcode')",
//rest of set statements for each column and corresponding post
where 'CustomerOrderID'='OrderNumberInArray'//(post value)

This update statement is not working, any pointers would be appreciated.

Thanks as always,

2 Answers 2

2

Remove 'ordernumber' from your $data array and pass it separately

$this->sales_model->update_order_data($this->input->post('ordernumber'),$data);‌

The query should be like

function update_order_data($key,$q){
 $this->db->where('CustomerOrderID', $key);
 $query = $this->db->update('Customer_Order_Summary',$q);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks again. okay, so progress definitely. problem is the $q['OrderNumber'] is my primary key and I am passing it as part of array $q. I then use it in the update statement set and the where so get error: Cannot update identity column 'CustomerOrderID'. How do I pass an array for the update columns and a variable for the where clause? Thanks.
@Smudger Oops forgot abt that... Just remove the order number from your data array and pass two parameters to the model like $this->sales_model->update_order_data($this->input->post('ordernumber'),$data);.. Check the edited answer
2

Try:

function update_order_data($q)
{
    $this->db->where('CustomerOrderID', $q['OrderNumber']);
    $this->db->update('Customer_Order_Summary',$q);
    return $this->db->affected_rows();
}

Note: Usually, in update functions, I have 2 arguments eg:

function update_something($pk,$data)
{
   $this->db->where('primary_key', $pk);
   $this->db->update('database_table',$data);
   return $this->db->affected_rows();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.