Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter updating records

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

The code is for updating user profiles.

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

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

The controller Profile_crud should retrieve data from a form and send the 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 profile.php On submit it triggers 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.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: models/profile_model.php

Line Number: 27

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

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home1/requestg/public_html/housedwork/system/libraries/Exceptions.php:166)

Filename: codeigniter/Common.php

Line Number: 356

and

A Database Error Occurred

You must use the "set" method to update an entry.

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

like image 207
CyberJunkie Avatar asked Nov 29 '22 17:11

CyberJunkie


2 Answers

For your profile_update function, you are specifying the argument of $data:

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

But in your model function, you have not specified 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);              
}
like image 92
Sarfraz Avatar answered Dec 26 '22 20:12

Sarfraz


Your profile_update() is missing $data parameter

function profile_update($data) 
{
    $this->db->where('user_id', 2);
    $this->db->update('user_profiles', $data);              
}
like image 27
bharath Avatar answered Dec 26 '22 20:12

bharath