Codeigniter Insert Query Example: This tutorial will show you how to insert data into database using codeigniter. Insert is one of the CRUD, a primitive operation of a Database System. Insert is otherwise called as Create process and is used to add new records (entity) to database tables. Databases allows single or batch process, which means you can insert a single record at a time or multiple records all at once into the database.
Also check for codeigniter update query and codeigniter delete query.
Codeigniter Insert Query Example:
Active Record Class provides a way to create database independent queries which are automatically escaped by the system and safer to use.
In codeigniter, $this->db->insert();
function from active records library is used to insert records into database.
$this->db->insert(table_name, data);
The first parameter contains the table name for inserting and the second parameter can be an array or object containing the values. The insert() function generates an insert string with the data provided and execute the query.
Sample MySQL Table: Employees
MySQL Table - Before Insert |
How to Insert Record in MySQL using CodeIgniter?
This is a codeigniter insert query to insert row into the above given 'Employees' table.
$data = array( 'EmpName' => 'Quinn Flynn', 'Designation' => 'Support Lead', 'DeptName' => 'Software', 'DOJ' => '2013-03-03', 'Salary' => 342000 ); $this->db->insert('Employees', $data); // Produces Insert String: // INSERT INTO Employees (EmpName, Designation, DeptName, DOJ, Salary) VALUES ('Quinn Flynn', 'Support Lead', 'Software', '2013-03-03', 342000);
Resultset:
MySQL Table - After Insert |
Insert Form Data in Codeigniter:
If you want to create a codeignitor form and insert the form data into database, then you can set the array 'keys' with the posted data like this and insert it to database.
$data = array( 'EmpName' => $this->input->post('txt_empname'), 'Designation' => $this->input->post('txt_designation'), 'DeptName' => $this->input->post('txt_deptname'), 'DOJ' => $this->input->post('txt_doj'), 'Salary' => $this->input->post('txt_salary') ); $this->db->insert('Employees', $data);
Also the function returns TRUE or FALSE status when the query is executed. You can easily check if the database insert was completed successfully or not with if else statement.
if ($this->db->insert('Employees', $data)) { // success } else { // db insert fails }
Insert Multiple Rows At Once In Codeigniter:
Codeigniter also provides another one insert function for batch processing. You have to use $this->db->insert_batch();
to insert multiple records into database at once.
$data = array( array( 'EmpName' => 'Jil Winters', 'Designation' => 'Accounts Manager', 'DeptName' => 'Finance', 'DOJ' => '2011-07-25', 'Salary' => 210550 ), array( 'EmpName' => 'Caesar Leigh', 'Designation' => 'Support', 'DeptName' => 'Sales', 'DOJ' => '2011-12-12', 'Salary' => 106450 ) ); $this->db->insert_batch('Employees', $data); // Produces Insert Query String: // INSERT INTO Employees (EmpName, Designation, DeptName, DOJ, Salary) VALUES ('Jil Winters', 'Accounts Manager', 'Finance', '2011-07-25', 210550), ('Caesar Leigh', 'Support', 'Sales', '2011-12-12', 106450);
Related Read: Insert Form Data into Database in Codeigniter with Validations
That was all about codeigniter insert query example. Following the same pattern you can easily insert records into database in codeigniter framework.
No comments:
Post a Comment