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

Updating and deleting database records using the active record pattern - 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

True to form, updating and deleting database rows with the active record pattern is only a matter of using a couple of intuitive methods, called “update()” and “delete()” respectively. They can be included in a simple controller class, as I did before when explaining how to perform database insertions.

To dissipate any possible doubts about how to use these method, I coded two controllers. The first one updates an existing row of a sample MySQL table, and the second one deletes a specified record.

Here are the respective controllers, so you can examine them in detail:

class Users extends Controller{

function Users(){

// load controller parent

parent::Controller();

// load database class and connect to MySQL

$this->load->database();

}

// update row of 'users' MySQL table

function index(){

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

$this->db->where('id','3');

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

}

}


class Users extends Controller{

function Users(){

// load controller parent

parent::Controller();

// load database class and connect to MySQL

$this->load->database();

}

// delete row from 'users' MySQL table

function index(){

$this->db->where('id',2);

$this->db->delete('users');

}

}

Undoubtedly, after studying the signature of the above controllers, you’ll have to agree with me that updating and deleting database rows with Code Igniter is an extremely simple process! In the first case, the controller uses the “update()” method to update a specific row in the sample MySQL table, while the second class simply deletes an existing record.

And with this final example, I’m finishing this brief introduction to inserting, updating and deleting database rows with Code Igniter. Don’t forget to consult its user manual for a more detailed explanation of each of the methods covered in this tutorial.

Final thoughts

In this seventh part of the series, I provided you with a bunch of examples aimed at demonstrating how to perform insertions, updates and deletions on a selected MySQL table by using Code Igniter’s database class. As you saw before, all of these tasks are very intuitive and easy to grasp too.

In the forthcoming chapter, I’ll be taking a look at the email class of this handy PHP framework, so you can learn how to use this class within the context of the MVC pattern. Don’t miss the next part!



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: