Codeigniter tutorial

Codeigniter Database Tutorial Part-2 Inserting Data With Codeigniter

Views: 668

Tutorial Part-2 Inserting Data In Codeigniter Database

Inserting data in codeigniter “$this->db->insert()” Tutorial

With the sql query handler in codeigniter the codeigniter insert() function makes it easy to insert data in your tables, you will have to pass an array with key field name and the key values as field value.

For example: You want to insert data in user table with fields’ employee_id, user_type, username and password. The array will be.

If you have missed our previous part you can read Codeigniter Database Create View Update Delete (CRUD) Tutorial Part-1

$data = array(

        'employee_id' => 'Employee id number',

        'user_type' => 'Type of User',

        'username' => 'Username of User',

        'password' => 'Password of the user'

);

Now we will insert data in database so make sure your database is loaded and then we will run the following query:

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

Note: The insert() function will do everything for you, it will build the query and insert in your user table. This insert() function will generate the query

INSERT INTO ‘user’(`employee_id`, `user_type`, `username`, `password`) VALUES ('Employee id number', 'Type of User', 'Username of User', 'Password of the user');

Inserting data in batch codeigniter “$this->db->insert_batch();”

With codeigniter it is also possible to insert data in database in a batch. You just need to create an array of your data arrays and perform the following query with table name and your array, rest will be taken care by codeigniter’s sql query handler.

This example shows batch codeigniter insert

$data = array(array(

        'employee_id' => 'Employee id number',

        'user_type' => 'Type of User',

        'username' => 'Username of User',

        'password' => 'Password of the user'),

                array(

        'employee_id' => 'Another Employee id number',

        'user_type' => 'Type of User',

        'username' => 'Username of User',

        'password' => 'Password of the user')

);

And then we can run the codeigniter batch insert helper for inserting data in batch

$this->db->insert_batch('user', $data);

Note: The above insert_batch() helper generate the query:

INSERT INTO ‘user’(`employee_id`, `user_type`, `username`, `password`) VALUES ('Employee id number', 'Type of User', 'Username of User', 'Password of the user'), ('Another Employee id number', 'Type of User', 'Username of User', 'Password of the user')

Debugging codeigniter insert query “get_compiled_insert()” helper

When you are not sure about what query is passed by codeigniter it is important that you know how to view the actual query so you can handle errors in it and make your app secure, there is another helper in codeigniter to help you with that, make note of this because you will need this helper every now and then.

The get compiled insert helper compiles the query but instead of processing it returns back the complete query, it then can be stored in a variable and echoed for debugging. See the example below.

$data = array(

        'employee_id' => 'Employee id number',

        'user_type' => 'Type of User',

        'username' => 'Username of User',

        'password' => 'Password of the user'

);




$result = $this->db->set($data)->get_compiled_insert('user');

echo $result;

This will print the under given sql query on screen.

INSERT INTO ‘user’(`employee_id`, `user_type`, `username`, `password`) VALUES ('Employee id number', 'Type of User', 'Username of User', 'Password of the user');

In next part of this tutorial we will learn about updating database with codeigniter.

Codeigniter Update Replace and Set Database Tutorial Part-3

If you have any other suggestions for this tutorial or any question let us know in the comments below or contact us, if you found this tutorial helpful then share it with your friends. Subscribe for latest tutorial updates.

Comments: 0

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