Codeigniter更新查询不起作用

时间:2014-04-26 14:40:14

标签: php mysql sql codeigniter

我遇到的问题是这个模型。如果我删除where语句,查询会正确运行,但我需要where语句才能根据id更改一行。当我使用where语句运行查询时,我没有错误,但数据库中没有任何受影响的行。

 public function update_goods()
 {
   $id=$this->input->post('ID');
   $data=array(
     'ID'=>$this->input->post('ID'),
     'Title'=>$this->input->post('Title'),
     'Value'=>$this->input->post('Value'),
     'Description'=> $this->input->post('Description'),

  );

  $this->db->where('ID', $id);
  $this->db->update('goods',$data);


 }

1 个答案:

答案 0 :(得分:0)

根据the docs,这应该可以正常工作。

$data = array(
               'title' => $title,
               'name'  => $name,
               'date'  => $date
            );

$this->db->where('id', $id);
$this->db->update('mytable', $data); 

// Produces:
// UPDATE mytable 
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id

我认为您应该从数组中获取ID,但不需要更新。

 public function update_goods()
 {
   $data = array(
     'Title'       => $this->input->post('Title'),
     'Value'       => $this->input->post('Value'),
     'Description' => $this->input->post('Description'),

   );
   $this->db->where('ID', $this->input->post('ID'));
   $this->db->update('goods', $data);
 }

另外,验证$this->input->post('ID')是指goods表中的记录。