doumiebiao6827
2017-10-11 07:06
浏览 72
已采纳

codeigniter和mysql更新错误?

when i try to update a table i get an error that time. error image

there is my controller code.

$this->hr_user->insert_user($info);

        $n=explode('--', $info['user_code']);
        $temp=$n[1];
        $lnum=ltrim($temp,'0');
        //print_r($lnum);
        //echo "<br>lastnumber".$lnum;
        $id=$info['user_type'];
        echo $id.''.$lnum;



        $this->hr_user->update_number($id,$lnum);



        redirect('hrm/index');

and here is my model code.

public function update_number($id,$lnum)
{
    $this->db->where('em_id',$id);
    $this->db->update('tbl_emaster.em_lastnumber',$lnum);

}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongtuojuan8998 2017-10-11 07:16
    已采纳

    The mysql error you got explains every thing. Your query order doesn't generate a qualified query, try:

    public function update_number($id,$lnum)
    {
        $this->db->set('eenter code herem_lastnumber', '$lnum');
        $this->db->where('em_id',$id);
        $this->db->update('emaster');
    }
    

    for more info www.codeigniter.com

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douchuopiao9002 2017-10-11 07:11

    Mysql update query syntax is :

    UPDATE table_name SET field1 = new-value1, field2 = new-value2
    [WHERE Clause]
    

    You can see, there are fields after SET. And because of your CI update syntax, column name is not being passed. And to pass that, you should pass array as 2nd argument of $this->db->update('tbl_emaster.em_lastnumber',$lnum);

    So you need to change it like this:

    $this->db->update('tbl_emaster',array('em_lastnumber' => $lnum);
    
    评论
    解决 无用
    打赏 举报
  • dongmaonao0505 2017-10-11 07:14
    $this->db->set('field', 'field+1');
    $this->db->where('id', 2);
    $this->db->update('mytable'); 
    // gives UPDATE `mytable` SET `field` = '   field+1' WHERE `id` = 2
    

    https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data

    评论
    解决 无用
    打赏 举报
  • dpa89292 2017-10-11 07:19

    change query

    public function update_number($id,$lnum)
    {
        $data=array('em_lastnumber'=>$lnum);
        $this->db->where('em_id',$id);
        $this->db->update('tbl_emaster',$data);
    
    }
    
    评论
    解决 无用
    打赏 举报