Welcome Guest, Not a member yet? Register   Sign In
Database Update Question

Currently, I'm using a hidden form field to capture the id of the database record I want to update. Next I save the post vars in my controller and send them to my model. Is this the standard way to perform this operation? Are there alternative ways, better ways?

Code from controller
function update_news()

    $update_info = array(
                        'article' => $this->input->post('article'),
                        'order_date' => $this->input->post('order_date'),
                        'date' => $this->input->post('date')
    $id = $this->input->post('id');
    $this->Drift_model->update_entry($id, $update_info);


Code from model
function update_entry($id, $update_info)
    $query = $this->db->update('news', $update_info, array('id' => $id));    


[eluser]Derek Allard[/eluser]
Not a very helpful response from me, but that's nearly exactly how I do it - I don't see a more elegant way off hand.

Another approach would be to set the id as the last URL segment
of your form/action-parameter and get the id in the method by
using the url_segment function. But like I said, it's just an
other approach and not meant to be more elegant than using the
hidden form field.

I took an alternative approach. I saved the id of the target table to the cookie (and sessions table) and used an inherited method to update the table.

Note, this involves modifying the Sessions class to accept non-default fields and the Model superclass, so that any derived classes inherit the method. In each model's constructor, I retrieve the id and place it in a protected property. So when I call $this->$model->update(), there is no need to pass arguments and retrieve hidden-data.

@Derek, thanks for the quick reply. I also wanted to say thanks for the great tutorial you have on your site, it really helped me to see how to integrate Code Igniter with JavaScript, my first love. I just started a blog of my own, codelicker.com. My second entry was a short note on available code igniter tutorials. While the list is short, I think it definitely covers the basics for those needing a quick introduction. Collecting these tutorials and others like them into one place on the code igniter site might be a great resource for newbies.

Well, I just finished driftstudio.org, it's a very simple site showcasing work and news by five collaborating members. One is my brother, Geoff Halber, who is a senior designer at Dwell magazine.

The tagline reads: Drift is a cross-disciplinary design studio working in the fields of architecture and graphic design.

While there's no work yet, I'm hoping they'll have some time to get their contributions together soon.

Anyway, I'm excited about getting to know code igniter better and exploring some of its other features in future projects.


I originally was trying to do that, but since I was using a submit button, there is no uri segment. Am I missing something?

You're doing it the typical way. Just make sure you're performing some validation to see if you're working with a valid record (ID) before you use it in a query.

What I meant was:

In your view file you have the form tag:
<?= form_open('controller_update/method_update/'.$id_to_update);?>

By submitting the form via your button, the form send the post data
to your controller - for example:


In your method you get the value using:
$id_to_update = $this->uri->segment(3)


Wow, that was a really simple solution, thank you. :coolsmile:

Now one more question. Is this type of thing a matter of style or is one method considered more appropriate?


your are welcome!

Regarding to your last question:
If Derek uses to take the hidden-form-field approach I would tend
to do it his way Wink
But maybe it's just a matter of style.

Your question is interesting for me too so it would be great
if other members would post some advantages and disadvantages
for both mehtods.

Theme © iAndrew 2016 - Forum software by © MyBB