【问题标题】:Codeigniter Updating Edit PageCodeigniter 更新编辑页面
【发布时间】:2013-10-21 09:41:54
【问题描述】:

我创建了一个编辑页面,将他们的信息重新填充到正确的字段中(基于 ID),他们可以修改详细信息...

我现在想要的是提交按钮来更新数据库表中的这些信息?

这是我的控制器中的编辑部分:

function edit($id = 0){


    $this->load->model('blogmodel');



if( $this->input->post() ){ #if form submitted

    $data   = array();

    $id     = $this->input->post('id'); #to be used in the where clause for updation

    $data['name'] = $this->input->post('name');
    $data['address'] = $this->input->post('address');
    $data['sEmail'] = $this->input->post('sEmail');

    $this->blogmodel->update_blog($data,$id);

}else{  

    // Form Validation required
    $this->load->library('form_validation');

    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('address', 'Address', 'required');
    $this->form_validation->set_rules('sEmail', 'Sender Email', 'required|valid_email');

    $this->form_validation->set_message('required', '%s is required');
    $this->form_validation->set_error_delimiters('<span class="error">', '</span><br />');


    $sql  = 'SELECT * FROM databse.blogs WHERE id = ?';

    echo $sql . ' - '. $id;
    $sql_params = array($id);
    $query = $this->db->query($sql, $sql_params);
    $form = $query->row_array();

    //$date['form'] = $form;

    $data['inputs'] = array('name' => $form['name'],
                            'address' => $form['address'],
                            'semail' => $form['email'],
                            'nickname' => $form
    );

    $data['Name']= $form['name'];
    $data['Address']= $form['address'];
    $data['Email']= $form['email'];

    $this->load->view('blog_content',$data);


    $data = $this->input->post();
    $result = $this->blogmodel->update_blog($data,$id);
    $this->load->view('blog_success');

}

}
}

到目前为止,这是我的模型:

function send_blog(&$data){



    $data['name'] = $this->input->post('name');
    $data['address'] = $this->input->post('address');
    $data['sEmail'] = $this->input->post('sEmail');


    $sql = "UPDATE database.blogs (name, address, email) WHERE id = $id
    VALUES (".$this->db->escape($data['name']).",
            ".$this->db->escape($data['address']).",
            '".$data['sEmail']."');";

    $this->db->query($sql);

}

function update(&$data,$id){

    $data['name'] = $this->input->post('name');
    $data['address'] = $this->input->post('address');
    $data['sEmail'] = $this->input->post('sEmail');

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

【问题讨论】:

    标签: php database codeigniter sql-update submit


    【解决方案1】:

    这里的current_url() 是什么?我猜它是function edit()。在您的函数编辑中,只需进行以下更改:

    function edit($id = 0){
        if( $this->input->post() ){ #if form submitted
            $data   = array();
            $id     = $this->input->post('id'); #to be used in the where clause for updation
            /*
                Retrieve all the post variables in the $data and send to model for updation
            */
            $this->your_model->update_function( $data, $id );
            redirect('controller/edit/'.$id, 'refresh'); #redirect to the same function to show the form or success page
        }else{
            /*
            Your rest of the function as it is
            */
        }
    }
    

    更新
    您的控制器更改:

    function edit($id = 0){
        if( $this->input->post() ){ #if form submitted
    
            $data   = array();
    
            $id     = $this->input->post('id'); #to be used in the where clause for updation
    
            $data['name']           = $this->input->post('name');
            $data['address']        = $this->input->post('address');
            $data['sEmail']         = $this->input->post('sEmail');
            $data['nickname']       = $this->input->post('nickname');
            $data['blogregion']     = $this->input->post('blogregion');
            $data['startdate']      = $this->input->post('startdate');
            $data['blogtitle']      = $this->input->post('blogtitle');
            $data['timesperweek']   = $this->input->post('timesperweek');
            $data['profiletext']    = $this->input->post('profiletext');
            $data['abouttext']      = $this->input->post('abouttext');
            $data['status']         = 0;
            $data['userid']         = $id;
    
            $this->newblogsetupmodel->update_blogforus( $data, $id );
    
            redirect('newblogsetup/edit/'.$id, 'refresh'); #redirect to the same function to show the form or success page
    
        }else{
            #rest of the controller here
        }   
    }
    

    你的模型改变了:

    function update_blogforus($data, $id){
        echo "<pre>";print_r( $data );die; #to print the data received from controller, see if its coming okay or make correction in the controller for the same
        /*$data['name']           = $data['name'];
        $data['address']        = $data['address'];
        $data['sEmail']         = $data['sEmail'];
        $data['nickname']       = $data['nickname'];
        $data['blogregion']     = $data['blogregion'];
        $data['startdate']      = $data['startdate'];
        $data['blogtitle']      = $data['blogtitle'];
        $data['timesperweek']   = $data['timesperweek'];
        $data['profiletext']    = $data['profiletext'];
        $data['abouttext']      = $data['abouttext'];
        $data['status']         = 0;
        //$data['userid'] = $data['user']->id;
        */
        $this->db->where('id', $id);
        $this->db->update('newblogsetup', $data);
        echo $this->db->last_qeury();die; #to see the query being executed, run it in your phpMyadmin and see if its updating as required.
    }
    

    新变化
    控制器:

    $data['id'] = $id;  #save the data in an element to echo in the hidden field of the form
    $this->load->view('newblogsetup_sent', $data); #load the data to the page
    

    在您的编辑页面中更改此行

    <?php echo form_hidden('id');?>
    

    收件人:

    <?php echo form_hidden( 'id', $id );?>
    

    【讨论】:

    • model function update_blogforus 的末尾写上echo $this-&gt;db-&gt;last_query();die;,就在这一行之后:$this-&gt;db-&gt;update('newblogsetup', $data);。它将显示update query。像这样调试你的函数。有关我建议的模型更改,请参见上述内容。
    • 看到 `[userid] =>` 是空白的,这意味着这个字段没有正确生成。如果隐藏字段的值中正确的用户 ID 是 echoed,请检查您的表单中的隐藏字段。正确生成后,在模型中注释此行并查看查询是否正确生成。进展顺利。 :D
    猜你喜欢
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多