2014-03-06 81 views
0

我正在使用codeigniter,试图从数据库中减去我的一个输入值,但没有运气。我现在的模式是:从DB中减去PHP变量

$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 
+0

删除$ newstock中的括号 – Denmark

+0

@Denmark我这样做时得到这个错误:http://d.pr/i/EXZS –

回答

0

尝试:

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

它摆脱了错误,但功能无法正常工作... –

+0

你需要运行$ this-> db-> update – Ramesh

+0

我得到这个数据库错误http://d.pr/i/EXZS –

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); 
} 
+0

我一直在遇到这种情况:你的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获取在第1行'Stock' ='Stock-''附近使用的正确语法 UPDATE'stock' SET'=''WHERE'Stock' ='Stock-' –

+0

你使用了什么代码?你可以给我准确的SQL查询,你想要做的 – Denmark

+0

我想运行:更新库存SET股票=股票 - $ this->输入 - >后('f2')WHERE ItemName = $ this-> input->帖子('f1') –

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

感谢你们的帮助。