Skip to content Skip to sidebar Skip to footer

CRUD (Create, Read, Update, Delete) tutorial in Codeigniter Framework

CRUD stands for Create, Read, Update and Delete which is often used in data processing applications that mostly use the CRUD function in it. This function is used to add data, delete data, and update data.

Read also:

In this tutorial, the author will give an example of creating a website that can display, delete and edit data. Here are the steps to do it:

1. Make database by the name list_books. Then make table named book in MySQL as a place to store the contents of the book list.

2. Creating a structure table book as follows:

3. Enter record in the table book:

4. Go to the Codeigniter folder then open the folder config then change configuration database.php like this:

$db['default']['hostname'] = 'localhost';$db['default']['username'] = 'root'; //username di mysql$db['default']['password'] = ''; //password$db['default']['database'] = 'daftar_buku'; //nama database

5. Still in the folder config to change files autoload.php like this:

$autoload['libraries'] = array('database'); //function untuk memanggil database $autoload['helper'] = array('url'); //function untuk memanggil helper url

6. Changing files config.php in the folder config.

$config['base_url'] = 'http://localhost/CI/'; //function ini digunakan untuk memanggil secara otomatis base url

7. Create s fileshowbuku.php in the folder application – controller.

load->model('showbuku_model'); } function index() { $data['title'] = 'Ini contoh menampilkan data dengan Codeigniter'; //memanggil function selectAll di model showbuku_model, dimasukkan ke $data['buku'] $data['buku'] = $this->showbuku_model->selectAll(); //meload view bernama buku_view.php dengan data variable adalah $data $this->load->view('buku_view', $data); } function input() { $this->load->view('inputbuku_view'); } function tambah() { $this->showbuku_model->input(); redirect ('showbuku'); } function delete($id) { $this->showbuku_model->delete($id); redirect('showbuku'); } function edit($id) { if($_POST==NULL) { $data['no'] = $this->showbuku_model->select($id); $this->load->view('editbuku_view',$data); }else { $this->showbuku_model->update($id); redirect('showbuku'); } } } ?>

8. Make files named show_model.php in the folder applications – models.

db->order_by("Judul","desc"); return $this->db->get('buku’)->result(); } function input() { $Id = $this->input->post('id'); $Judul = $this->input->post('Judul'); $Penulis=$this->input->post('Penulis'); $data = array( 'id'=>$Id, 'Judul'=>$Judul, 'Penulis'=>$Penulis ); $this->db->insert('buku',$data); } function delete($id) { $this->db->delete('buku', array('id'=>$id)); } function update($id) { $this->db->where('id',$id)->update('buku', $_POST); } function select($id) { return $this->db->get_where('buku', array('id'=>$id))->row(); } } ?>

9. If controller files and model has been created, now it’s time to create views in the folder applications – views by the name book_view.php.

  Contoh Input   

">Tambah Data
Id Judul Penulis Aksi
id ?> Judul ?> Penulis ?> id, ' Ubah ') . '| | ' . anchor('showbuku/delete/'.$b->id, ' Hapus '); ?>

10. Create a display to enter data with the name inputbuku_view.php in the folder applications – views.

  Data Buku  

Data Buku


11. Create a view for editing data with the name editbuku_view.php in the folder applications – views.

  Ubah Data Buku  

Form Ubah Data Buku


12. Open a browser then type the URL http://localhost/CI/index.php/showbuku

14. When you want to add data records so, it will appear form like this. Form this appears because it has created a file inputbuku_view.php.

15. Then if you want to change data records when there is an error whileinput data, will appear form like this. Same with name files what was made earlier is editbuku_view.php.

Well, that was the author’s discussion of CRUD using Codeigniter.