你微笑的看着我 发表于 2016-12-15 13:12:12

用CI作修改页面,总是提示我未定义的ID

初学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');
        $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));
       
}



谢谢帮助。

Hex 发表于 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未定义

lostincoding 发表于 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啊。
整个代码改成这样:
public function upArticle(){   
      
      $this->load->helper('form');
      $this->load->library('form_validation');
      
      $this->load->model('News_model','admin');

      $id=$this->input->post('id');
      $config = array(
      'id'=>$id,
            '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');
      $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";
          }
}

你微笑的看着我 发表于 2016-12-16 11:05:08

lostincoding 发表于 2016-12-15 15:45
你看一下你的代码:
$this->admin->set_article($id,$config);
这里的$id你定义的吗?


谢谢,感谢
页: [1]
查看完整版本: 用CI作修改页面,总是提示我未定义的ID