Jump to content

Code Igniter: Update function fails with "Severity: Notice"


kristo5747

Recommended Posts

Hello,

 

I am a newbie with PHP and CodeIgniter. I am building a CRUD app that works well expect for the update operation.

 

My controller:

<?php

class Site extends CI_Controller {

       function __construct()
       {
            parent::__construct();
        $this->is_logged_in();
       }

    function members_area() 
    {
        $data = array();
        
        if($query = $this->site_model->get_records()) // site model is autoloaded.
        {
            $data['records'] = $query;
        }
        
        $this->load->view('members_area', $data);
    }

    function create()
    {
        $data = array(
            'title' => $this->input->post('title'),
            'content' => $this->input->post('content')
        );
        
        $this->site_model->add_record($data);
        $this->members_area();
    }

    function delete()
    {
        $this->site_model->delete_row();
        $this->members_area();
    }

    function update()
    {
        $data = array(
            'title' => $this->input->post('updtitle'),
            'content' => $this->input->post('updcontent')
        );
        
        $this->site_model->update_record($data);
        $this->members_area();
    }

    function is_logged_in()
    {
    $is_logged_in = $this->session->userdata('is_logged_in');

    if(!isset($is_logged_in) || $is_logged_in != true)
        {
                $this->load->view('login_form');
        }
    }
} 

 

My model:

<?php

class Site_model extends CI_Model {
    
    function get_records()
    {
        $query = $this->db->get('data');
        return $query->result();
    }

    function add_record($data) 
    {
        $this->db->insert('data', $data);
        return;
    }

    function delete_row()
    {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('data');
    }

    function update_record($data) 
    {
        $this->db->where('id', $id);
        $this->db->update('data', $data);
    } 

 

My view:

<h2>update</h2>
    <?php echo form_open('site/update');?>
    <p>
        <label for="update title">Update Title:</label>
        <input type="text" name="updtitle" id="updtitle" />
    </p>   
    <p>
        <label for="update content">Update Content:</label>
        <input type="text" name="updcontent" id="updcontent" />
    </p>        
    <p>
        <input type="submit" value="Update" />
    </p>
    <?php echo form_close(); ?>    
    <hr />
</body>
</html> 

If I attempt to update a record in my db, I get an error in my model (“Severity: Notice”, “Message: Undefined variable: id”) and the update does NOT happen.

 

What am I missing? I feel like it's something trivial....

In your model update_record function you need to pass the $id to the function

 

function update_record($id, $data)
{
$this->db->where('id', $id);
$this->db->update('data', $data);
}	

 

Then in the controller pass the $id variable to the update function

function update($id)

 

And in your view change the form_open to

<?php echo form_open('site/update/'.$id); ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.