codeigniter更新表行与新数据数组在哪里

时间:2013-05-14 14:03:04

标签: database arrays codeigniter sql-update

我有一个带有uniqueID / PrimaryKe(OrderNumber)的数据库记录

我想用同一个PrimaryKey,OrderNumber。

的新数据更新记录

我的控制器是:

 $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);

我的模特是:

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

所以我想要的是:

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)

此更新声明无效,任何指针都将不胜感激。

一如既往地谢谢,

2 个答案:

答案 0 :(得分:2)

$data数组中删除'ordernumber'并单独传递

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

查询应该像

function update_order_data($key,$q){
 $this->db->where('CustomerOrderID', $key);
 $query = $this->db->update('Customer_Order_Summary',$q);
}

答案 1 :(得分:2)

尝试:

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

注意:通常,在更新函数中,我有2个参数,例如:

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