Insert Data

In this tutorial, we are going to learn how to create add user details in database.

 

Step 1 : we will create  add_user_detail fucntion so that we can load insert page.

public function add_user_detail()
{

	$this->load->view('add_user_detail');
	
}

 

Step 2 : Create insert page in view folder.

<form>
  
  <label>Name : </label>
  <input type="text" name="name" placeholder="Enter Name" class="form-control">
  
  <label>Email : </label>
  <input type="text" name="email" placeholder="Enter Email" class="form-control">

  <label>Contact: </label>
  <input type="text" name="contact" placeholder="Enter Mobile Number" class="form-control">

  <label>Status : </label>
  <select class="form-control" name="status">
    <option value="">---Select Status---</option>
    <option value="1">Active</option>
    <option value="0">Inactive</option>
  </select>

  <button type="submit" class="btn btn-primary">Add User</button>

</form>

 

Step 4 : Create form action to work with insert detail and also create method post. So that you can send user data in database.

<form action="<?php echo base_url(); ?>dashboard/add_user_detail" method="post">

 

Step 5 : Create validaion on add_user_detail function. So that, if the user submit his data without filling the form then he will get the warning message. Please fill your details.

public function add_user_detail(){

	$this->load->helper('form');
	$this->load->library('form_validation');

	//set validation
	$this->form_validation->set_rules('name','Full Name','required');
	$this->form_validation->set_rules('email','Email','required');
	$this->form_validation->set_rules('contact','Contact','required');
	$this->form_validation->set_rules('status','Email','required');
	if($this->form_validation->run())
	{
		
			
	}
	else{
		$this->load->view('add_user_detail');
	}
	
}

 

Show warning message on add user view page with the help of form_error.

<form action="<?php echo base_url(); ?>dashboard/add_user_detail" method="post">

  <label>Name : </label>
  <input type="text" name="name" placeholder="Enter Name" class="form-control">
  <?php echo form_error('name','<p class="text-danger">','</p>'); ?> 

   <label>Email : </label>
  <input type="text" name="email" placeholder="Enter Email" class="form-control">
  <?php echo form_error('email','<p class="text-danger">','</p>'); ?>
 
  <label>Contact : </label>
  <input type="text" name="contact" placeholder="Enter Mobile Number" class="form-control">
  <?php echo form_error('contact','<p class="text-danger">','</p>'); ?>
  
  <label>Status : </label>
  <select class="form-control" name="status">
    <option value="">---Select Status---</option>
    <option value="1">Active</option>
    <option value="0">Inactive</option>
  </select>
  <?php echo form_error('status','<p class="text-danger">','</p>'); ?>

  <button type="submit" class="btn btn-primary">Add User</button>
  
</form>

 

like : 

 

Step 6 : Now, if the validation is successfully run, we will write the insert code in it so that we will submit the details of the user in the database.

public function add_user_detail(){

	$this->load->helper('form');
	$this->load->library('form_validation');
	$this->form_validation->set_rules('name','Full Name','required');
	$this->form_validation->set_rules('email','Email','required');
	$this->form_validation->set_rules('contact','Contact','required');
	$this->form_validation->set_rules('status','Email','required');
	if($this->form_validation->run())
	{

		//user details insert code here
		$data = array(
			'username' => $this->input->post('name'),
			'email' => $this->input->post('email'),
			'contact' => $this->input->post('contact'),
			'status' => $this->input->post('status'), 
		);

		$this->load->model('dashboard_model','userdata');

		// send user details in model to insert
		if($this->userdata->add_user($data))
		{
			echo 'success';
		}
		else{
			echo 'error';
		}
	}
	else{
		$this->load->view('add_user_detail');
	}
	
}

 

Step 7 :  In dashboard model, we will create add_user function. So that we will insert the user details in database.

public function add_user($data)
{
	return $this->db->insert('userdata',$data);
}

 


Result :

Source Code:

Codeigniter tutorial for beginners

In this project. We are providing you, how to create small project in Codeign....

Source Code