Home arrow PHP arrow Page 3 - Inserting, Updating and Deleting Database Rows with Code Igniter

Inserting new rows into a selected MySQL table - PHP

You've seen some of the things you can accomplish with the Code Igniter PHP framework in earlier parts of this series. In this seventh part of the series, you will learn, through copious examples, how to perform insertions, updates and deletions on a selected MySQL table by using Code Igniter’s database class. You'll see that these tasks are very intuitive and easy to grasp, thanks to Code Igniter's friendly learning curve.

TABLE OF CONTENTS:
  1. Inserting, Updating and Deleting Database Rows with Code Igniter
  2. Performing conditional SELECT statements with Code Igniter
  3. Inserting new rows into a selected MySQL table
  4. Updating and deleting database records using the active record pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 23
September 24, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: