onlinecode

CodeIgniter Insert Query-how to run Insert Query in CodeIgniter

CodeIgniter Insert Query-how to run Insert Query in CodeIgniter

In this CodeIgniter Insert Query-how to run Insert Query in CodeIgniter we will show you how CodeIgniter Insert Query run. we will show you how to run Insert Query in CodeIgniter with query(), With Query Bindings, Standard Insert, insert_string(), insert_batch(), Escaping Insert Queries, Get Inserted ID, Get Affected Rows.

CodeIgniter “Insert” query execute by following functions

CodeIgniter Insert Query with $this->db->query()

Program 1:

$sql_query = "insert into tbl_all_user (user_name, user_age, group_name)
            values ('codeigniter, 35, 'Group 1')";
$this-> db-> query($sql_query);

Program 2:

$user_name      = $_POST['user_name'];
$user_groupname = $_POST['group_name'];
$user_age       = $_POST['user_age'];
$user_data = array(
    'user_name'  = >  $user_name  ,
    'group_name'= >  $user_groupname,
    'user_age'   = >  $user_age );
$this-> db->insert('tbl_all_user', $user_data);


CodeIgniter Insert Query with Insert With Query Bindings

Use Of Query Bindings Benefit of using binds is that the values are automatically escaped, producing safer queries
// insert query single manually 
$sql_query = "insert into tbl_all_user (user_name, user_age, group_name)
            values (?, ?, ?)";
// pass data of users and execute querry		 	
$this-> db-> query($sql_query,array('onlinecode, 25, 'user_name of Group 1'));

CodeIgniter Insert Query with Standard Insert

// insert query single manually 

$sql_query = "INSERT INTO tbl_all_user (user_name, group_name, user_age)
        VALUES (".$this->db->escape($user_name).", ".$this->db->escape($group_name).".", ".$this->db->escape($user_age).")";
		
// pass data of users and execute querry			
$this->db->query($sql_query);

CodeIgniter Insert Query with $this->db->insert_string()

Note : Values are automatically producing and escaped safer queries.

// get user data 
$user_name      = $_POST['user_name'];
$user_groupname = $_POST['group_name'];
$user_age       = $_POST['user_age'];

// pass data of user in array
$user_data = array(
    'user_name'  = >  $user_name  ,
    'group_name'= >  $user_groupname,
    'user_age'   = >  $user_age );
	
// pass data of users and execute querry	
$this-> db->insert_string('tbl_all_user', $user_data);

CodeIgniter Insert Query with $this->db->insert_batch()

// pass data of user in array
$user_data = array(
   array(
    'user_name'  = >  'user name 1' ,
    'group_name'= >  'user_name of Group 1',
    'user_age'   = >  '25'
   ),
   array(
    'user_name'  = >  'user name 2' ,
    'group_name'= >  'user_name of Group 2',
    'user_age'   = >  '26'
   )
);
// pass data of users and execute querry	
$this->db->insert_batch('tbl_all_user', $user_data);

// sel insert query somting like this 
//INSERT INTO mytable (user_name, group_name, user_age) VALUES ('user name 1', 'name of Group 1', 25'), ('user name 2', 'name of Group 2', '26')

CodeIgniter Insert Query with Escaping Insert Queries

$this->db->escape()

This function determines the data type so that it can escape only string data. It also automatically adds single quotes around the data so we do not have to ::

// pass data using escape
$sql_query = "INSERT INTO tbl_all_user (user_name) VALUES(".$this->db->escape($user_name).")";

CodeIgniter Insert Query with $this->db->escape_str()

This function escapes the data passed to it, regardless of type. Most of the time we will use the above function rather than this one. Use the function like this ::

$sql_query = "INSERT INTO tbl_all_user (user_name) VALUES('".$this->db->escape_str($user_name)."')";

CodeIgniter Insert Query with Get Inserted ID

The insert use ID number when performing database inserts.

$this->db->insert_id()

CodeIgniter Insert Query with Get Affected Rows

Displays the number of affected rows, when doing “write” type queries (insert, update, other query.).

$this->db->affected_rows();

You also like codeigniter select query and codeigniter join tables and Codeigniter Delete Query

Exit mobile version