Jump to content

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


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); ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.