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!
blog comments powered by Disqus |
|
|
|
|
|
|
|