sum函数用于更新已经数据

时间:2018-01-25 07:03:58

标签: codeigniter

我有2个表,第1个表格名trans_tmbh_qty,用于记录第2个frm_fancpu表格的记录历史值 这是我的结构表第1页

enter image description here

这个tabel名称为frm_monitor

enter image description here

这是我在控制器trans_tmbh_qty

上的全部功能
public function create_action() 
{
    $this->_rules();

    if ($this->form_validation->run() == FALSE) {
        $this->create();
    } else {
        $data = array(
    'merk' => $this->input->post('merk',TRUE),
    'av_mobo' => $this->input->post('av_mobo',TRUE),
    'av_prc' => $this->input->post('av_prc',TRUE),
    'av_ram' => $this->input->post('av_ram',TRUE),
    'av_hdd' => $this->input->post('av_hdd',TRUE),
    'av_psu' => $this->input->post('av_psu',TRUE),
    'av_fancpu' => $this->input->post('av_fancpu',TRUE),
    'user_modify_av' => $this->input->post('user_modify_av',TRUE),
    'date_modify_av' => $this->input->post('date_modify_av',TRUE),
    );

        $fancpu = $this->input->post('merk', TRUE);
         $av_fancpu = $this->input->post('av_fancpu', TRUE);
         $this->M_fancpu->av_fancpu($fancpu,$av_fancpu

    );
        $this->M_fancpu->av_fancpu($fancpu,$av_fancpu);
        $this->M_trans_tmbhqty->insert($data);
        $this->session->set_flashdata('message', 'Create Record Success');
        redirect(site_url('trans_tmbhqty'));
    }
}

我想更新qty_available_fancpu上的库存。如果以前填写10,我在表格上插入5。我需要结果15.我现在的代码只用5替换10号和我的控制器

 $fancpu = $this->input->post('merk', TRUE);
        $av_fancpu = array(
        'qty_available_fancpu' => $this->input->post('av_fancpu'),
    );
        $this->M_fancpu->av_fancpu($fancpu,$av_fancpu);

它是我在M_fancpu上的模型

function av_fancpu($fancpu,$av_fancpu)
{ 
$this->db->where('tipe_fancpu', $fancpu);
$this->db->update('tbl_fancpu', $av_fancpu);
}

1 个答案:

答案 0 :(得分:0)

您可以针对您的问题尝试此解决方案: -

Controller.php 文件

class fancpu {
    $fancpu = $this->input->post('merk', TRUE);
    $av_fancpu = $this->input->post('av_fancpu', TRUE);
    $this->M_fancpu->av_fancpu($fancpu,$av_fancpu);
}

Model.php 文件

function av_fancpu($fancpu,$av_fancpu) { 
    $this->db->set('qty_available_fancpu', 'qty_available_fancpu + ' . (int) $av_fancpu, FALSE);
    $this->db->where('tipe_fancpu', $fancpu);
    $this->db->update('tbl_fancpu');
}

我希望它会有所帮助。