Issue with saving an update information in Codeign

2019-09-01 02:37发布

问题:

My goal is to update job_contract. There are two ways that this SHOULD be done. 1. through the client's page AND 2. through the provider's page.

What I currently have is in my job model is:

 public function update_job_contract($post_obj)
{
    $id = $post_obj['id'];
    $data = array 
    (
        'client_feedback' => $post_obj['client_feedback'],
        'client_notetoself' => $post_obj['client_notetoself'],
        'contract_status' => $post_obj['contract_status'],
        'client_id' => $this->auth_model->get_user_id()
    );

    $this->db->insert('job', $data);
}

public function provider_update_job_contract($post_obj)
{
    $id = $post_obj['id'];
    $data = array 
    (
        'provider_feedback' => $post_obj['provider_feedback'],
        'provider_notetoself' => $post_obj['provider_notetoself'],
        'provider_id' => $this->auth_model->get_user_id()
    );

    $this->db->insert('job', $data);
}

I have the following lines in my client controller page:

public function update_job_contract() 
{
    $this->validateRole('client');
    $this->load->model('job_model');

    $id = $this->uri->segment(3,0);


    $data['job'] = $this->job_model->get_job($id);  

    $this->load->view('client/update_job_contract', $data);
}   

public function update_job_contract_submit() 
{
    $this->validateRole('client');
    $this->load->model('job_model');

    if ( '0' == $_POST['id'] ) {
    $this->job_model->update_job_contract($_POST);
    //} 
    redirect('client/manage_job_contracts?message=Congratulations!');
}

And this in my provider controller page:

 public function provider_update_job_contract() 
{
    $this->validateRole('provider');
    $this->load->model('job_model');

    $id = $this->uri->segment(3,0);


    $data['job'] = $this->job_model->get_job($id);  

    $this->load->view('provider/provider_update_job_contract', $data);
}   

public function provider_update_job_contract_submit() 
{
    $this->validateRole('provider');
    $this->load->model('job_model');

    if ( '0' == $_POST['id'] ) {
    $this->job_model->provider_update_job_contract($_POST);
    } 
    redirect('provider/job_contracts?message=Congratulations!');
}

Problem is, they don't really update the entries. Please help..

回答1:

You are using $this->db->insert(), but with Active Record to update the row you should be using $this->db->update()

$data = array(
   'title' => $title,
   'name' => $name,
   'date' => $date
);

$this->db->where('id', $id);
$this->db->update('mytable', $data);

Another words your functions should look like this:

public function update_job_contract($post_obj)
{
    $id = $post_obj['id'];
    $data = array 
    (
        'client_feedback' => $post_obj['client_feedback'],
        'client_notetoself' => $post_obj['client_notetoself'],
        'contract_status' => $post_obj['contract_status'],
        'client_id' => $this->auth_model->get_user_id()
    );
    $this->db->where('id', $id);
    $this->db->update('job', $data);
}

public function provider_update_job_contract($post_obj)
{
    $id = $post_obj['id'];
    $data = array 
    (
        'provider_feedback' => $post_obj['provider_feedback'],
        'provider_notetoself' => $post_obj['provider_notetoself'],
        'provider_id' => $this->auth_model->get_user_id()
    );
    $this->db->where('id', $id);
    $this->db->update('job', $data);
}

Also, inside the controllers $_POST should be replaced with $this->input->post()

Using this one as example:

public function provider_update_job_contract_submit() 
{
    $this->validateRole('provider');
    $this->load->model('job_model');

    $post = $this->input->post();
    if ( '0' == $post['id'] )
    {
        $this->job_model->provider_update_job_contract($post);
    } 
    redirect('provider/job_contracts?message=Congratulations!');
}

Make sure you update other controllers in the same way