0
votes

I have a table name "Category" which contains (cat_id, name, description). I can insert, retrieve, and delete without any problems. But when I update my Category, no data inserted in my database. I check my table and the result is nothing.

The POST model "Category_Model extends CI_Model":

public function custom_query($data)
    {
        $q = $this->db->query($data);
        return $q;
    }

The POST controller "Category extends CI_Controller":

public function edit_category()
    {
        $data['title'] = "Edit Category Page";

        $this->load->view('edit_category', $data);
    }

    public function update_category()
    {
        $id = $this->input->post('cat_id'); // I try $id = $this->uri->segment(3); but no result
        $name = $this->input->post('name');
        $desc = $this->input->post('description');

        $this->post_model->custom_query("update category set cat_name='".$name."', description='".$desc."' where cat_id='".$id."'"); // when I delete 'where cat_id='".$id."'' clause, all my records were changing/updating
        // I change to $this->db->where('cat_id', $id); $this->db->update('category'), but no result.

        redirect ('category/view_categories');
    }

Here is my EDIT CATEGORY view:

<form action="<?php echo base_url(); ?>category/update_category" method="POST">
            <fieldset>
                <legend>Edit Category</legend>
                <label for="cat">Name :</label>
                <input type="text" name="name"/>
                <label for="desc">Descriptions :</label>
                <textarea name="description" cols="40" rows="2"></textarea>
                <input type="submit" value="Update">
            </fieldset>
        </form>

Please anyone tell me what was wrong with my code? Thank in advance

best regards.

*note: I put 'database' in autoload config.

4
Why would you expect $this->db->where('cat_id', $id); $this->db->update('category'); to work? - Shomz
I follow CI active records query. Could you tell me how it'll be? - learn4life
You're missing the set part. Try: $this->db->update('category', array('cat_name'=>$name, etc...), array('cat_id'=>$id)); or just change your second part to $this->db->update('category', $setarray); See here. - Shomz
I use this custom query $this->post_model->custom_query("update category set cat_name='".$name."', description='".$desc."' where cat_id='".$id."'");. I think the goal is similar to CI User Guide - learn4life
Yeah, yeah, that seems fine for a raw query (with all its security issues)... I was just refering to the part you commented out. - Shomz

4 Answers

0
votes

First of all, are you sure you writing table name correctly?

..."update kategori..."

If this is ok, try to output your query before sending it to database, like this:

$query = "update kategori set cat_name='".$name."', description='".$desc."' where cat_id='".$id."'";
error_log('My query: ' . print_r($query, true));
$this->post_model->custom_query($query);

Then, if you won't see any problems in that query, give it to us.

0
votes

It looks like your query might not be getting the cat_id as I don't see it anywhere in the passing view. Try a hidden field in the HTML which contains the cat_id. This might also be easier than trying to get it via URI segments.

0
votes

You could be learn about CI models, it will simplify your life. I believe with, for some reason, the redirect could be close your connection before the commit... It doesn't occur if you use the model object.

0
votes

A little sample for models...

Create a class on application/models, like this

file "category_model.php"... attention for this name, because the CI is very restrictive with model name. Must by equal class name, but all lowercase.

class Category_model extends CI_Model {

// your fields
var $id = null;
var $name = null;

// call parent constructor... it's essential
function __construct() {
    parent::__construct();
}

// create a set function, for fill all fields directly from get or post
function set($data) {
    $this->id = isset($data['id']) ? $data['id'] : null;
    $this->name = isset($data['name']) ? $data['name'] : null;
}

// execute update on database
function update($id) {
    $this->db->update('category', $this, array('id' => $this->id));
}
}

on the controller, instance and invoke the model

$this->load->model('Category_Model', null, true);
$this->Category_Model->set($this->post());
$this->Category_Model->update();

after this, proceed you normal code.