Codeigniter Update Entries

I'm having trouble updating records using the Codeigniter framework. I use the MVC pattern and active records.

The code is designed to update user profiles.

In my Profile_model model, I have a function to update ...

 function profile_update() { $this->db->where('user_id', 2); $this->db->update('user_profiles', $data); } 

The Profile_crud controller must retrieve data from the form and send data to the model.

 function update() { $data = array ( 'country' => $this->input->post('country'), 'website' => $this->input->post('website') ); $this->load->model('Profile_model'); $this->Profile_model->profile_update($data); } 

and the form in my view is profile.php In submit mode, it runs the update function in my controller.

Update

 <?php echo form_open('profile_crud/update'); ?> <p> <label for="country">Country</label> <input type="text" name="country" id="country" /> </p> <p> <label for="website">Website</label> <input type="text" name="website" id="website" /> </p> <p><input type="submit" value="Save" /></p> <?php echo form_close(); ?> 

When I submit the form, I get 3 types of errors.

PHP error occurred

Severity Level: Note

Message: Undefined variable: data p>

File Name: models / profile_model.php

Line Number: 27

line 27 is $this->db->update('user_profiles', $data);

PHP error occurred

Severity Level: Warning

Message: it is not possible to change the header information - headers already sent (the output started with / home 1 / requestg / public_html / housedwork / system / libraries / Exceptions.php: 166)

File Name: codeigniter / Common.php

Line Number: 356

and

Database error occurred

You must use the "set" method to update the record.

I'm not sure what I'm doing wrong. Can someone help?

+4
source share
3 answers

For your profile_update function profile_update you specify the $data argument:

 $this->Profile_model->profile_update($data); 

But in your model function, you did not specify one:

 function profile_update() { $this->db->where('user_id', 2); $this->db->update('user_profiles', $data); } 

It should be:

 function profile_update($data) { $this->db->where('user_id', 2); $this->db->update('user_profiles', $data); } 
+15
source

Your profile_update () is missing a data parameter

 function profile_update($data) { $this->db->where('user_id', 2); $this->db->update('user_profiles', $data); } 
+3
source

Your Profile_model model Profile_model not received any parameters.

 function profile_update($data) { return $this->db ->where('user_id', 2) ->update('user_profiles', $data); } 
+3
source

Source: https://habr.com/ru/post/1334400/


All Articles