Get $500+ of the best After Effects files, video templates and music for only $20!
CodeIgniter From Scratch: Day 5
videos

CodeIgniter From Scratch: Day 5 – CRUD

This entry is part 5 of 17 in the CodeIgniter From Scratch Session
« PreviousNext »

The most requested tutorial for day five of our CodeIgniter screencast series was for an introduction to CRUD operations. We’ll review how to easily create, read, update, and delete records using CI’s active-records class.

Catch Up

Day 5: CRUD Operations


Tags: Videos
Add Comment

Discussion 248 Comments

Comment Page 3 of 4 1 2 3 4
  1. mike says:

    great course .. one question..when I use $this->index(); to return to the options_view page i get an error – 404 page not found
    the url is http://localhost/ci/index.php/index.php/site/create
    i.e. index.php is repeated thus causing the error
    why has that happened?

    • Dave says:

      I am getting the same error and cannot update records because I keep getting 404 page not found errors.

      • Matt says:

        Check your base_url in th config.php in the config folder. Sounds to me like you guys might have index.php in the base url so when you reload with that function it’s writing it twice.

  2. Joe says:

    Great tutorial!

    I’ve made it about halfway through to where you’re initially testing your submission form, but on my copy nothing happens when I press submit. CodeIgniter is something new to me so it’s probably something quite silly…

    I’m running MAMP on OS X 10.6 with PHP 5.

    Thanks in advance for your help.

  3. Joe says:

    In regards to my last comment: it also seems that some portion of my form is working because, even though the form is not reloaded without any data in it, I see entries have been added to the database with 0′s in the fields.

    • Bob says:

      You’re not the first person in the comments section to mention the “0′s in the table fields” problem, but no one has addressed it correctly yet.

      The zeros are caused if you load “index.php/site/create” directly into your browser, instead of just “index.php/site”. Since the create() function that gets called when you load “index.php/site/create” is written to load data into the database, and since you’re accessing “index.php/site/create” directly, the create() function loads null values (i.e., zeros) into the database.

      Hope this helps.

      • Tom says:

        How do you prevent the create() function from loading twice? is there a way to prevent functions being loaded automatically?

    • JJ says:

      With regard to the 0′s in the DB, – shot in the dark here….

      I noticed that I had 0′s initially, but checking this out I found that the “data” database table from previous example (blog, I think) had a field called “contents”, but in this example the field is called “content”.

      I’m not sure if this is a fix, – but as I did initially have 0′s, but subsequently this fixed, it might be the reason.

      • krauses says:

        If you’re getting 0′s in your row for either title or content, make sure you have your table set up correctly.

        You might being seeing those zeros either because you have the row set to INT instead of CHAR or VARCHAR. It might also be that you have not set the NOT NULL value on that row.

  4. Richard says:

    Wish I could find a downloadable version of this one like the others in podcast format. These tutorials have helped a lot.

  5. Well i mostly see this kind of things in all blogs that peoples are always commenting about different matters and topics instead of the topic they are on to, so this kind of things really makes me upset, because i always expecting to spread conversation at this kind of blogs and forum for improves my knowledge base. http://www.Gordoniihoodia.net

    • Gabriel says:

      So wait, are you saying CodeIgniter will enlarge your penis? Funny, I heard the same thing, I’m glad you’re spreading conversation at this kind of blogs.

  6. alex says:

    reading up on codeigniter, as i am planning to use it for a project.

    I do have a question about SEO url’s and CI vs CI use of controller/method/etc
    How do we get the best SEO url’s???

  7. adhie says:

    I can’t play for this Day 5. Please help to advise? Thanks

  8. mnkysrbtr says:

    This is awesome thank you so much, really cleared a lot of things up with CI

  9. Frank says:

    Your tutorials helped me get up to speed on Codeigniter.
    I would like to see a tutorial on Codeigniter Shoppingcart when displaying items in a list and allowing users to add to cart.

  10. Radi says:

    Thx Very Good Work!

  11. T_Wrecks says:

    HEY JW,

    Great screencasts, I subscibe via itunes but this is the only video not in the feed. Can you PLEASE add this to the video to the itunes feed?

    Thanks

  12. Goob says:

    You could really do with a proper IDE or some plug-ins for TextMate. It’s painful to watch someone have to type closing html tags! Oh, and cut down on the coffee. You probably hit the backspace key as much as all the others combined.

    All criticisms over with….I really love this series. I’m learning a lot. I hope there is an advanced course to come. Keep up the good work. And maybe change to drinking apple juice or something ;)

  13. adelle says:

    This Tutorial is What i needed exactly .. But The sound quality is not that good today .. Thanks Very Much ..

  14. Michael says:

    Loving the series so far!

    However, I’d like to know how do I get my update to work?
    Basically so it’s like my create script, but just updates a single entry.

    And how do I create a view to display just one entry?

    Anyone care to help?

  15. mehdi says:

    hi ,
    tanks for the good tutorials . i learn a lot but recently all video site are blocked in my country could you please put the videos on third party site like rapid share or something so we can use them . tanks

  16. Wondering says:

    What happens, if insert in database fails? Or delete fails? How would you handle this?

  17. dave says:

    Once again, fantastic tutorial that covers the bases necessary without going too far into the fluffy stuff! :)

  18. Korenn says:

    You asked what the best practice is about the controller or the view picking up the segment to process. The answer is most definitely the controller. The controller is in charge of program flow, it decides where data comes from, how it’s processed and where it goes. The model should only process the data it’s given. When you make the model directly access input methods, you’re breaking that task division. A model shouldn’t care or know how data is supplied.

  19. It is a very well done tutorial, thanks

  20. michael says:

    would liike to download this screencast.. i loaded every possible codeigniter tutorial, but some are not downloadable ;(

  21. xRommelx says:

    why this is not available in the PODSCAT

  22. Madalin says:

    Hello everybody,
    I have a little problem who really piss me off.
    Everything is working fine beside UPDATE.

    I have to hard code if I wanna get a satisfy result. I can mention that I was changing this function in every possible way and .. still not working.

    My code :

    function updateCommentsPerId($data) {

    $this->db->where(‘id’, $this->uri->segment(3)); // hard coding param2 => 18
    $this->db->update(‘comments’, $data);
    }

    The call :

    function updating() {

    $this->load->model(‘update_model’);

    $data = array(
    ‘author’ => $this->input->post(‘author’),
    ‘body’ => $this->input->post(‘body’),
    );

    $this->update_model->updateCommentsPerId($data);
    $this->index();
    }

    All I wanna do is making param2 from where clause dynamic.
    Seems like I have to get into black magic or asking a wizard for help.
    If you need the source code I can upload it somewhere.

    Thanks for help. I hope a wizard will hear my pain. :)

    • Michael says:

      Fantastic job.

      I went further than your tutorial did and added an edit link to each post, from where each post can then be updated. It all eventually went well with a bit of struggle here and there.

      My main problem was passing the ‘id’ from the edit page to the site_model class. I learned the big lesson that form_open() does not take variables (if you think otherwise kindly enlighten me), I tried the option form_open(“site/update/$this->uri->segment(3)”) n more ways than I thought I could imagine but to no avail.

      I eventually passed the id via a hidden form field and it sailed through.

      Your tots are welcome.

      any one interested in my edit/update code pm me.

  23. Martino Wullems says:

    Catching up these series, very well introduction to codeigniter and MVC in general. I just have one question:

    When you create a new post (or any action actually) the URL gets appended with site/create to call the corresponding controller. Then the index controller is called again* to show the view of all the posts:

    function create(){

    $data = array(
    ‘title’ => $this->input->post(‘title’),
    ‘content’ => $this->input->post(‘content’)
    );

    $this->site_model->add_record($data);
    * $this->index(); //return to index
    }

    However, the URL still contains the site/create at the end when the action is completed, even after calling $this->index().

    I had to woraround for this, but I think theyre a little sloppy:
    1) Redirect to the index page directly.
    2) instead of calling the $this->create() to create a new post, check when the creation form is submitted:
    if($this->input->post(‘submit’)){//create post}

    I’m having a hard time trying to explain what I mean so I hope someone can help me out haha.
    Thanks in advance

  24. Casper Larsen says:

    Hi, is there anyone who can post a link to a download in .MP4 format?

  25. Trope says:

    I spiced up the look , and came up with this for my first blog CRUD app in CI ….

    (screenshot)
    http://img685.imageshack.us/img685/6782/crud.jpg

  26. Chris Ellis says:

    Anyone found the solution to the problem of directly loading “index.php/site/create” directly into your browser. Apparently, for me at least, “index.php/site/delete/” with the id after it deletes as well. The question is how to prevent direct access.

    • komsaikoi says:

      you could make use of forms instead of the uri->segment(), and make a conditional statement

      if ($this->input->post(‘delete’))

      this tutorial is good and simple but it should be at the same time robust in handling uncontrolled data.

    • Flow says:

      I’m also wondering how to prevent direct access. Someone figured this out?

      @Jeff big up for this awesome series! I’m picking it up real quickly :)

  27. Eoghan says:

    Thanks again Jeff, a wonderful course.

  28. ci9 says:

    @Jeffrey

    Thanks for this very nice tutorial i learned alot.

    I’ll pray for your good health.

    God Bless you Jeffrey.

  29. BPfohl says:

    Love this tutorial… Newby question though

    I am having trouble with this working locally – I keep geeting this error:

    A Database Error Occurred
    Error Number: 1054
    Unknown column ‘$data’ in ‘field list’
    INSERT INTO `data` (`$data`) VALUES (”)

    Anyone else have that problem?

  30. Ronen says:

    What is the exact meaning of

    if($query = $this->site_model->get_records()

    Thanks.

  31. Rick says:

    Ok – so is ANYONE ever going to answer the question re: getting this one on iTunes? Jeff?

  32. Stefan says:

    Hum, its strange, i seem to have hit a wall half way through, just about kept up with the other 4 tuts in this series but im getting the following…

    Fatal error: Class ‘Site_model’ not found in /home/scross/development/ci/system/libraries/Loader.php on line 184

    Ill download the source and see if i can figure it out from that…

    Thanks for this series, its a real help!

  33. Toni says:

    Jeff….this video is not avaliable anymore?

  34. mikkelz says:

    Thanks for ALL the great tutorials – I really appreciate them!

  35. jewel says:

    hi
    i cannot get any tutorial video on this page.
    it a add clip of Spice whisks. its a dings what i do it now.
    so, how i get the video tutorial ……………………………??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

  36. Erfan says:

    Hi,
    Thanks for great lesson on CRUD.
    But i have a request if you can tell us how we will do all these orations dynamically, Because most the things you mentions in this lesson are hard coded.
    it would be highly appreciated if you can make it dynamically.

    once again thank you for your help.

  37. ErikS says:

    Just started watching these a few days ago. Thru #5 at this point. Very helpful. Wish I had something like this years ago when I was starting out.

  38. uokesita says:

    For the people getting errors of “0 records” being created when loading “index.php/site/create”

    You can do something like if input(s) is(are) empty then go to the index page else create the record.

    You can also (and most secure) use the form_validation library that are reviewed in the tutorial 4 as this: https://gist.github.com/780711

  39. Matthew says:

    Great tutorial. New to PHP and Codeigniter. I don’t know if anyone is still responding to comments, but anyone mind telling me how I would go about reading a single, individual post? I’m not sure if this was already covered, and apologies if it has, but how might I go about creating sort of “read more” link that directs the user to read just a single post?

  40. Nic says:

    Wow.
    That is the most complex way to do a CRUD I have ever seen.
    I gave up on your video when 15 minutes in you had failed to do the C in CRUD……..
    Why make it so complex?
    Doesnt code igniter have a CRUD plugin?

  41. Nice screencast. Are you guys going to update the vids over the time as CI evolves and matures more?

  42. Colin says:

    Here is a way to update records Dynamically.

    View

    Update

    <option value="id; ?>">
    title; ?>

    Title

    Content

    No Records to Update :(

    Controller

    function update()
    {
    $data = array(
    'title' => $this->input->post('title'),
    'content' => $this->input->post('content')
    );

    $rowid = $this->input->post('rowid');

    $this->site_model->update_record($data, $rowid);

    $this->index();
    }

    Model

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

  43. Colin says:

    Here is a way to update records Dynamically.

    View

    Update

    <option value="id; ?>">
    title; ?>

    Title

    Content

    No Records to Update :(

    Controller

    function update()
    {
    $data = array(
    'title' => $this->input->post('title'),
    'content' => $this->input->post('content')
    );

    $rowid = $this->input->post('rowid');

    $this->site_model->update_record($data, $rowid);

    $this->index();
    }

  44. Madoc says:

    Hi there,

    Great Tutorials so far ! Can’t wait to get stuck on #6.

    Quick question:
    - Are you guys really using the url method to delete and update records on your CI projects ?
    - If so how do you prevent users from deleting random records by using the url and changing the id for instance ?

  45. jeric realubit says:

    thanks! ;)

  46. Victor says:

    Can someone make a tutorial about updating entries please (video)

  47. Michael Insalaco says:

    I have CI 2.0.2 and autoloading the Site model breaks the app. it seems like any framework that is updated to a new version makes previous tutorials obsolete.

Comment Page 3 of 4 1 2 3 4

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.