Codeigniter更新编辑页面

时间:2013-10-21 09:41:54

标签: php database codeigniter sql-update submit

我创建了一个编辑页面,将其信息重新填充到正确的字段中(基于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); 
}
   }

1 个答案:

答案 0 :(得分:0)

这里的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 );?>