In this tutorial you will learn about the CodeIgniter Insert Query and its application with practical example.
In CodeIgniter, insert() method is used to insert record in database table. In order to generate insert statement, insert() method can be used in following ways –
Syntax:-
1 |
insert($table = '', $set = NULL, $escape = NULL) |
Here,
$table(String):- Table Name
$set(array):- An associative array of column/value.
$escape(bool):- Whether to escape values and identifiers (TRUE/FALSE)
Returns(bool):- It returns TRUE on success and FALSE on failure.
Example:- Let’s say you have a MySQL table named ’employee_master’ with the following fields –
emp_name, emp_email, emp_phone, emp_address, emp_code and emp_dept
Inserting Data using $this->db->insert()
This is how you can insert a record into ’employee_master’ table using $this->db->insert().
1 2 3 4 5 6 7 8 9 |
$data = array( 'emp_name' => 'John Doe', 'emp_email' => 'john.doe@xyz.com', 'emp_phone' => '1234567890', 'emp_address' => 'street 97 CA', 'emp_code' => 'EMP01', 'emp_dept' => 'sales' ); $this->db->insert('employee_master',$data); |
Inserting Data using $this->db->query()
This is how you can insert a record into ’employee_master’ table using $this->db->query().
1 2 3 |
$sql = "insert into employee_master (emp_name,emp_email,emp_phone,emp_address,emp_code,emp_dept) values ('John Doe','john.doe@xyz.com','1234567890','street 97 CA','EMP01','sales')"; $this->db->query($sql); |
Inserting Data with Query Bindings
This is how you can insert a record into ’employee_master’ table with Query Binding.
1 2 |
$sql = "insert employee_master (emp_name,emp_email,emp_phone,emp_address,emp_code,emp_dept) values (?,?,?,?,?,?)"; $this->db->query($sql,array('John Doe','john.doe@xyz.com','1234567890','street 97 CA','EMP01','sales')); |
Inserting Data using $this->db->set()
This is how you can insert a record into ’employee_master’ table using $this->db->set().
1 2 3 4 5 6 7 |
$this->db->set('emp_name','John Doe') $this->db->set('emp_email','john.doe@xyz.com') $this->db->set('emp_phone','1234567890') $this->db->set('emp_address','street 97 CA') $this->db->set('emp_code', 'EMP01') $this->db->set('emp_dept','sales') $this->db->insert('employee_master'); |
Inserting Data using $this->db->insert_batch()
The $this->db->insert_batch() method is used to insert batch of records in single query, this is how you can insert batch of record into ’employee_master’ table using $this->db->insert_batch().
1 2 3 4 5 6 |
$data =array( array('John Doe','john.doe@xyz.com','1234567890','street 97 CA','EMP01','sales'), array('Steve','steve@xyz.com','1234567890','street 98 NY','EMP02','sales') ) ); $this->db->insert('employee_master',$data); |
Escaping Insert Queries
In CodeIgniter, values can be escaped using $this->db->escape_str() function to produce safer queries.
1 |
$sql = "INSERT INTO employee_master (emp_name) VALUES('".$this->db->escape_str($name)."')"; |
Retrieve Last Inserted ID
The insert_id() method is used to retrieve the last Inserted ID when performing database inserts.
1 |
$this->db->insert_id() |
Retrieve Affected Rows
The affected_rows() method is used to displays the number of affected rows, when doing “write” type queries (insert, update, etc.).
1 |
$this->db->affected_rows(); |