How to insert and select data from database in codeigniter beginner guide

Read this tutorial for how to insert and select data from database in codeigniter beginner guide. I hope you already know how to setup and install codeigniter. Read this tutorial for installation guide Codeigniter installation beginner guide. Once you are done with the installation.

First create a table in your database

CREATE TABLE IF NOT EXISTS `task` (
 `id` int(10) NOT NULL,
 `name` varchar(50) NOT NULL,
 `email` varchar(100) NOT NULL,
 `contact` int(12) NOT NULL,
 `address` varchar(100) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Now open your view folder and create a file name Taskview.php and copy paste the below code. We will use this file to insert the data in the database and to show the inserted data in the same view.

Just follow the instruction and you can modify the code later on according to your requirment.

<form method = "POST" action = "<?php echo site_url('Task_controller/addData');?>">
	Name:<input type="text" name = "name" >
	Email:<input type="text" name = "email" >
	Contact:<input type="text" name = "contact" >
	Address:<input type="text" name = "address" >
	<input type="submit" value="submit">
</form>
 
<table>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
<th>Address</th>
<?php
if(!empty($showData)){
	foreach($showData as $row)
	{ ?>
<tr>
 
<td><?php echo $row->name; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo $row->contact; ?></td>
<td><?php echo $row->address; ?></td>
 
</tr>
<?php }} ?>
</table>

Now open your controller and create a file name Task_controller.php file . In the controller we will do some code to insert and select the data. Controller basically used to call the view and model. Make sure that your file name and the class name in the controller should be same. So just copy and paste the below code in your controller file.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Task_controller extends CI_Controller {
 
 
	public function __construct() {
 
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('Task_model');
	}
    public function index()
    {		
        $this->load->view('Taskview');
    }
	public function addData()
	{
		//here wee will egt the data from the form and put in array $data
		$data = array('name'=>$this->input->post('name'),
				'email'=>$this->input->post('email'),
				'contact'=>$this->input->post('contact'),
				'address'=>$this->input->post('address')
		);
		//now load your model
		$this->Task_model->addData($data);
 
		//after adding the data in database we will select the data from the database to show in the view
		$data['showData'] = $this->Task_model->viewData();
 
		// now send the selected data in to the view
		$this->load->view('Taskview',$data);
 
	}
}

Now open your model and create a file name Task_model.php. We will do some code here to insert the data in database and select the data from the database. Copy and paste the below code in your model.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Task_model extends CI_Model {
 
 
	public function __construct() {
 
		parent::__construct();
	}
    public function addData($data)
    {
             $this->db->insert('Task',$data);
    }
	 public function viewData()
    {
             $this->db->select('*');
			 $this->db->from('Task');
			 $query = $this->db->get();
			 $result = $query->result();
			 return $result;
    }
}

Now open your browser and run this URL http://localhost/CI/index.php/Task_controller/

Your screen will look like this

Codeigniter insert and select data

Now add the data and click on submit button. After adding some data your data will show below in the table.

Hope this tutorial how to insert and select data from database in codeigniter beginner guide will help you.