suzana_maria suzana_maria - 2 years ago 83
PHP Question

Editing and updating an item in CodeIgniter

I'm having trouble to edit and update an item in my first CodeIgniter project. When I click on the edit button, first of all, it loads the view with the inputs filled with the informations of that item on the database, but it doesn't load the bootstrap that I'm using.

Then, when I click the "Edit" button, returns lots of errors saying that:

  • my function on the controllers is missing an argument and the variable is undefined, and

  • my view has an undefined offset and is trying to get property of non-object.

All the other commands for the CRUD are working, except this.

The version of CodeIgniter I'm using is 2.2.6


  • Model:

    function edit($id)
    $this->db->where('id', $id);
    $query = $this->db->get('products');
    return $query->result();

    function update($data)
    $this->db->where('id', $data['id']);
    return $this->db->update('products');

  • Controller:

    function edit($id)
    $data['title'] = "Edit Product";
    $data['product_data'] = $this->model->edit($id);
    $this->load->view('productEdit', $data);

    function update()
    $this->form_validation->set_error_delimiters('<span>', '</span>');
    $validations = array(
    'field' => 'name',
    'label' => 'Name',
    'rules' => 'trim|required|max_length[255]'
    'field' => 'price',
    'label' => 'Price',
    'rules' => 'trim|required|max_length[255]'
    'field' => 'stock_quantity',
    'label' => 'In Stock',
    'rules' => 'trim|required|max_length[255]'
    if ($this->form_validation->run() === FALSE) {
    } else {
    $data['id'] = $this->input->post('id');
    $data['name'] = $this->input->post('name');
    $data['price'] = $this->input->post('price');
    $data['stock_quantity'] = $this->input->post('stock_quantity');
    if ($this->model->update($data)) {
    } else {
    log_message('error', 'Error');

  • View:

    <?php echo form_open('products/edit/', 'id="form-products"'); ?>

    <input type="hidden" name="id" value="<?php echo $product_data[0]->id; ?>"/>

    <label for="nome">Product Name:</label><br/>
    <input type="text" name="name" value="<?php echo $product_data[0]->name; ?>"/>
    <div class="error"><?php echo form_error('name'); ?></div>

    <label for="email">Price:</label><br/>
    <input type="text" name="price" value="<?php echo $product_data[0]->price; ?>"/>
    <div class="error"><?php echo form_error('price'); ?></div>

    <label for="email">In Stock:</label><br/>
    <input type="text" name="stock_quantity" value="<?php echo $product_data[0]->stock_quantity; ?>"/>
    <div class="error"><?php echo form_error('stock_quantity'); ?></div>

    <input type="submit" name="update" value="Update" />

    <?php echo form_close(); ?>

Thank you for your time.

Answer Source
  • Try this one Update your model function edit($id) { $this->db->where('id', $id); $query = $this->db->get('products'); return $query->row(0); }

and then in your view.

<input type="hidden" name="id" value="<?php echo $product_data->id; ?>"/>

also change your form action

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download