PHP
  Home arrow PHP arrow Page 3 - Inserting, Updating and Deleting Database Rows with Code Igniter
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Inserting, Updating and Deleting Database Rows with Code Igniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2008-09-24


    Table of Contents:
  • Inserting, Updating and Deleting Database Rows with Code Igniter
  • Performing conditional SELECT statements with Code Igniter
  • Inserting new rows into a selected MySQL table
  • Updating and deleting database records using the active record pattern

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Inserting, Updating and Deleting Database Rows with Code Igniter - Inserting new rows into a selected MySQL table
    ( Page 3 of 4 )

    In order to demonstrate how to insert a new row into the previous “users” MySQL table via the active record pattern, I’m going to work with only a controller class. Eventually, I could define a couple of additional views for displaying either an error message or a confirmation web page, depending on the result of the insert operation.

    However, this will be left momentarily as homework for you, in case you feel bored and want to have some fun coding PHP classes. So, returning to the topic, the simplest way to insert a new record into a selected table is by using a method of the database class, not surprisingly called “insert().”

    The prototype for using this brand new method can be seen in the code sample below.

    class Users extends Controller{

    function Users(){

    // load controller parent

    parent::Controller();

    // load database class and connect to MySQL

    $this->load->database();

    }

    // insert new row into 'users' MySQL table

    function index(){

    $data=array('firstname'=>'Jane','lastname'=>'Smith','email'=> 'jane@domain.com');

    $this->db->insert('users',$data);

    }

    }

    As shown before, the “insert()” method is so intuitive that it practically doesn’t require any additional explanations. Anyway, you can see above that I built a simple controller class, which utilizes this method, to add a new fictional user to a sample MySQL table.

    The data to be inserted is passed to the method in the form of an associative array, along with the name of the table that will be updated. In addition, it’s worth mentioning that it’s possible to utilize an object instead of an array, but for a detailed explanation of how to accomplish this, please check the Code Igniter’s user manual.

    So far, you have learned how to employ the “insert()” method that belongs to  Code Igniter’s database class to add a new record to a selected table. It’s feasible to achieve the same results by using another method called “set().”

    A typical usage of this method is illustrated below.

    class Users extends Controller{

    function Users(){

    // load controller parent

    parent::Controller();

    // load database class and connect to MySQL

    $this->load->database();

    }

    // insert new row into 'users' MySQL table using the 'set()' method

    function index(){

    $this->db->set('firstname','Jane');

    $this->db->set('lastname','Smith');

    $this->db->set('email','jane@domain.com');

    $this->db->insert('users',$data);

    }

    }

    As you can see, inserting new database rows with the “set()” method requires a few extra lines of code, but you may find it a bit more intuitive than its counterpart, “insert().” Anyway, both of them are valid, so you should choose the one that best suits your needs.

    At this point, I've shown you how to perform database insertions very easily with the active record pattern. The last thing I’m going to show you in this article will be how to update and delete records from a MySQL table by using the pattern in question.

    To learn the complete details of how to perform these database operations with Code Igniter, please read the upcoming section. We’re almost done!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek