用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1130|回复: 5
收起左侧

[已解决] 用CI作修改页面,总是提示我未定义的ID

[复制链接]
发表于 2016-12-15 13:12:12 | 显示全部楼层 |阅读模式
初学CI框架,修改页面总提示我有未定义的ID,不知如何修改,谢谢帮助。下面是我的代码:

控制器代码:

public function upArticle(){   
       
        $this->load->helper('form');
        $this->load->library('form_validation');
       
        $this->load->model('News_model','admin');

        $config = array(
       
            'title' => $this->input->post('title'),
            'time' => $this->input->post('time'),
            'content' => $this->input->post('content')
               
                );
               
        $this->admin->set_article($id,$config);
       
        $this->form_validation->set_rules('title', '* 文章标题', 'required|max_length[25]');
        $this->form_validation->set_rules('time', '* 发表时间', 'required');
       $this->form_validation->set_rules('content', '* 内容', 'required');
                               
        if($this->db->affected_rows() > 0)
        {
           $this->News_model->set_article();          
           echo "<script>alert('修改文章成功'); window.location.href='/CI2/index.php/News/article'</script>";        
          }else          
        {
           echo "<script>alert('修改文章失败'); window.location.href='/CI2/index.php/News/article'</scrip";
          }
}



模型代码:

public function set_article($id,$config){
               
    $this->load->helper('url');
    $this->db->update('news',$config,array('id'=>$id));
       
}



谢谢帮助。
发表于 2016-12-15 13:38:06 | 显示全部楼层
$this->News_model->set_article();

你并没有传 $id 当然就是未定义了。。。。
 楼主| 发表于 2016-12-15 15:23:45 | 显示全部楼层
Hex 发表于 2016-12-15 13:38
$this->News_model->set_article();

你并没有传 $id 当然就是未定义了。。。。

我加了 ‘id’=> $this->input->post('id')  还是提示我未定义
 楼主| 发表于 2016-12-15 15:29:09 | 显示全部楼层
       $this->admin->set_article($id,$config);   提示我这个ID未定义
发表于 2016-12-15 15:45:23 | 显示全部楼层
你微笑的看着我 发表于 2016-12-15 15:29
$this->admin->set_article($id,$config);   提示我这个ID未定义

你看一下你的代码:
$this->admin->set_article($id,$config);
这里的$id你定义的吗?
所以在这行代码前你要加上:
$id=$this->input->post('id');

你刚才说加了 ‘id’=> $this->input->post('id')  还是提示我未定义,
这样加的id并不是变量$id啊。
整个代码改成这样:
  1. public function upArticle(){   
  2.         
  3.         $this->load->helper('form');
  4.         $this->load->library('form_validation');
  5.         
  6.         $this->load->model('News_model','admin');

  7.         $id=$this->input->post('id');
  8.         $config = array(
  9.         'id'=>$id,
  10.             'title' => $this->input->post('title'),
  11.             'time' => $this->input->post('time'),
  12.             'content' => $this->input->post('content')
  13.                
  14.                 );
  15.                
  16.         $this->admin->set_article($id,$config);
  17.         
  18.         $this->form_validation->set_rules('title', '* 文章标题', 'required|max_length[25]');
  19.         $this->form_validation->set_rules('time', '* 发表时间', 'required');
  20.        $this->form_validation->set_rules('content', '* 内容', 'required');
  21.                                 
  22.         if($this->db->affected_rows() > 0)
  23.         {
  24.            $this->News_model->set_article();           
  25.            echo "<script>alert('修改文章成功'); window.location.href='/CI2/index.php/News/article'</script>";         
  26.           }else         
  27.         {
  28.            echo "<script>alert('修改文章失败'); window.location.href='/CI2/index.php/News/article'</scrip";
  29.           }
  30. }
复制代码
 楼主| 发表于 2016-12-16 11:05:08 | 显示全部楼层
lostincoding 发表于 2016-12-15 15:45
你看一下你的代码:
$this->admin->set_article($id,$config);
这里的$id你定义的吗?

谢谢,感谢

本版积分规则