从DB中减去PHP变量

时间:2014-03-06 06:30:47

标签: php mysql codeigniter activerecord subtraction

我正在使用codeigniter并试图从数据库中减去我的一个输入值而没有运气。我目前的模特是:

    $newstock = $this->input->post('f2');
            $itemname = $this->input->post('f1');
            $this->db->where('ItemName', $itemname); 
            $this->db->set('Stock', 'Stock-'.[$newstock], FALSE);

我收到此错误:

Severity: Notice

Message: Array to string conversion

Filename: models/inventory.php

3 个答案:

答案 0 :(得分:1)

找到解决方案:

   $ItemName = $this->input->post('f2');
    $query = $this->db->select('Stock')->from('inventory')->get();
    foreach ($query->result() as $row)
        {
             $row->Stock;

        }
        $oldstock = $row->Stock;
        $numtosub = $this->input->post('f2');
        $numtosub;
        $newstock = $oldstock - $numtosub;
         $newstock;

    $data = array('Itemname' => $this->input->post('f1'),
                      'Stock' => $newstock,
                    );
    $data2 = array('Itemname' => $this->input->post('f1'),
                      'QuantitySold' => $this->input->post('f2'),
                      'Date' => standard_date()
                    );
            $this->db->insert('transactions', $data2);
            $itemname = $this->input->post('f1');
            $this->db->where('ItemName', $itemname); 
            $this->db->update('inventory', $data);

感谢你们的帮助。

答案 1 :(得分:0)

尝试:

$this->db->set('Stock', 'Stock-'.$newstock, FALSE);

答案 2 :(得分:0)

尝试这个代码我让我不确定这会工作,但我想我给你一个想法

function update(){
    $newstock = "Stock-".$this->input->post('f2');
    $itemname = $this->input->post('f1');

    $data = array("stock" => $newstock);
    $this->db->where('ItemName',$itemname);
    $this->db->update('inventory', $data);
}