Welcome Guest, Not a member yet? Register   Sign In
Insert data in to a database.(CRUD part 01)
#1
Thumbs Up 

This lesson will help you to learn you about the basic data inserting to a database.
That is first step of the CRUD.

Step 01 :

First create a database called Contact.

Then a table called Students inside that database.

Students table should have two fields. ID and Mobile.
ID is the primary key of the table with varchar(20) data type.
Mobile is a field with varchar(20) data type.

Step 02 :

Now you need to do few modifications to the files.

First open database.php inside the \CodeIgniter\application\config directory.

Edit that file as follows.

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root'; //Enter your username of the database.
$db['default']['password'] = '';
$db['default']['database'] = 'contact'; //Enter the database name 'contact'
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;


Second open autoload.php inside \CodeIgniter\application\config directory.

Find this code and edit it as follows.

$autoload['libraries'] = array('database','session','form_validation');

$autoload['helper'] = array('url','form');


Finally open config.php inside \CodeIgniter\application\config directory.

$config['encryption_key'] = '2345678789';



Step 03 :

Now we have to create the controller as dataForm.php

<?php
class dataForm extends CI_Controller{

public function insert(){

$this->load->view('insert');

}

public function insertData(){

$this->load->library('form_validation');
$this->form_validation->set_rules('ID', 'ID', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('Mobile', 'Mobile', 'trim|required|min_length[4]|xss_clean');

if($this->form_validation->run()==FALSE)
{

$this->load->view('insert');
echo '<script>alert("You have not fill the form correctly.Recheck");</script>';
}

else
{

$data = array(

'ID' => $this->input->post('ID'),
'Mobile' => $this->input->post('Mobile')

);

$this->load->model('dataModel');
$this->dataModel->insert($data);
echo '<script>alert("You have successfully enter the data.");</script>';
$this->load->view('insert');
}


}

}
?>


Step 04 :

Now create the model as dataModel.php

<?php
class dataModel extends CI_Model{

public function insert($data)
{

$this->db->insert('students',$data);

}


}

?>

Step 05 :

Now create the view as insert.php



<?php echo form_open('dataForm/insertData'); ?>

<fieldset>

<legend> Enter the data </legend></br>
ID : <input style="margin-left:40px" type="text" name="ID" placeholder="Enter ID"/></br></br>
Mobile : <input style="margin-left:10px" type="text" name="Mobile" placeholder="Mobile"/></br>

</br></br>

<input style="margin-left:50px" type="submit"/>

</fieldset>

<?php echo form_close(); ?>

Step 06 :

Now go to web browser and type http://localhost/codeigniter/index.php/dataForm/insert

And you will see the form in it.Fill it and click on submit button.

And you will see all the data has inserted in to a database.

That is all about the data insertion.
That is my style with Codeigniter Idea . Start with simple and ends in deep.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB