How to Insert Record in Database Table Using CodeIgniter Active Record Class

Published on : May 31, 2013

Author:

Category: Our Blog


We already knew about CodeIgniter theming. Today we will concentrate on Database (MySql).  Basic database in CodeIgniter will be discussed today. For theming we used only “view” and “controllers”. But we knew that CodeIgniter is a MVC (Model, View, Controller).   All logical operation we will do in Model. Check out more details on ellislab. Before starting MySql database with CodeIgniter we need to learn “Active Record Class”. You can use plan MySql query(s) instead of “Active Record Class” but we will use “Active Record Class”. Our first topic is: How to insert record in database table using CodeIgniter Active Record Class. So, are you ready to insert record using CodeIgniter DB classes? Create a table and name it “report

CREATE TABLE ` report` ( `id` int(10) unsigned NOT NULL auto_increment, `full_name` varchar(20) collate latin1_general_ci NOT NULL, `email` int(50) unsigned NOT NULL, PRIMARY KEY  (`id`) ); Create a php file into model in the name “insert_model.php” with following code

class insert_model extends CI_Model
{
	function __construct()
	{
		parent:: __construct();
	}
}

Create a php file into controller with the name “insert.php” using the following code

class Insert extends CI_Controller
{
	function __construct()
	{
		parent:: __construct();
		$this->load->helper(array('form', 'url', 'html'));
		$this->load->library('form_validation');
		$this->form_validation->set_message('alpha','Invalid Name');
		$this->form_validation->set_message('valid_email', 'Invalid Email Address');
		$this->form_validation->set_message('required', 'Require');
		$this->load->model('insert_model');
	}
}

Now create a form into “views/pages” &  name it “insert.php” with the following code

<?=form_open('insert/data_submit/'); ?>
	<table width="485" cellspacing="0" cellpadding="0">
		<tr>
			<td width="85"></td>
			<td width="200" colspan="2"><?php if(isset($ins_msg)){echo $ins_msg;}?></td>
		</tr>
		<tr>
			<td>Full Name:</td>
			<td><input name="full_name" value="<?=set_value('full_name');?>" type="text" /></td>
			<td><?=form_error('full_name');?></td>
		</tr>
		<tr>
			<td>Email:</td>
			<td><input name="email" value="<?=set_value('email');?>" type="text" /></td>
			<td><?=form_error('email');?></td>
		</tr>
		<tr>
			<td width="223">&nbsp;</td>
			<td><input name="submit" type="submit" value="Login" alt="login" /></td>
		</tr>
	</table>
<?=form_close();?>

You have submitted your form to “insert/data_submit/” where “insert” is controller name and “data_submit” is a controller function name. A controller may have one or more function(s). Now update your controller with the following code

class Insert extends CI_Controller
{
	function __construct()
	{
		parent:: __construct();
		$this->load->helper(array('form', 'url', 'html'));
		$this->load->library('form_validation');
		$this->form_validation->set_message('alpha','Invalid Name');
		$this->form_validation->set_message('valid_email', 'Invalid Email Address');
		$this->form_validation->set_message('required', 'Require');

		$this->load->model('insert_model');
	}
	public function data_submit($page = 'insert')
	{
		if ( ! file_exists('application/views/pages/'.$page.'.php'))
		{
			show_404();
		}
		$data['title'] = ucfirst($page);
		$this->form_validation->set_rules('full_name', 'Full Name', 'required|alpha');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('templates/header', $data);
			$this->load->view('pages/insert',$data);
			$this->load->view('templates/footer', $data);
		}
		else
		{
			$data['query'] = $this->insert_model->insert();
			if($data['query'] != NULL)
			{
				$data['ins_msg'] = 'data insert';
				$this->load->view('templates/header', $data);
				$this->load->view('pages/insert',$data);
				$this->load->view('templates/footer', $data);
			}
			else
			{
				$data['ins_msg'] = 'Not inset';
				$this->load->view('templates/header', $data);
				$this->load->view('pages/insert',$data);
				$this->load->view('templates/footer', $data);
			}
		}
	}
}

 

And update your model with following code

class insert_model extends CI_Model
{
	function __construct()
	{
		parent:: __construct();
		$this->load->helper(array('form', 'url', 'html'));
		$this->load->library('form_validation');
		$this->form_validation->set_message('alpha','Invalid Name');
		$this->form_validation->set_message('valid_email', 'Invalid Email Address');
		$this->form_validation->set_message('required', 'Require');
		$this->load->model('insert_model');
	}
	function insert()
	{
		$data = array
		(
			'full_name' => $this->input->post('full_name'),
			'email' => $this->input->post('email')
		);
		$this->db->insert('report',$data);
	}

}

 

Now visit – http://localhost/your_root_folder/index.php/controllers_name/controllers_function_name

Example : http://localhost/CodeIgniter/index.php/insert/data_submit


Leave a Reply

Your email address will not be published. Required fields are marked *