【问题标题】:Codigniter Update query does not workCodeigniter 更新查询不起作用
【发布时间】:2014-06-12 07:20:22
【问题描述】:

我的问题在于这个模型。如果我删除 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);


 }

【问题讨论】:

  • 尝试回显 $id 。你拿到身份证了吗?
  • echo $this->db->last_query(); - 在更新查询结束时尝试此操作。

标签: php mysql sql codeigniter


【解决方案1】:

根据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 表中的记录。

【讨论】:

  • 谢谢我的朋友,但我的问题不同,我的表单中有一个隐藏的输入,并且该输入没有通过 id...
  • 哈哈,请注意我在答案中的最后一点;)很高兴听到你想通了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 2015-01-19
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多