Codeigniter Single & Multiple Insert Query

Welcome to this CodeIgniter Insert Query Tutorial, where we will show you how to insert single or multiple records into a database using CodeIgniter. Additionally, we will also cover how to get the last inserted record’s ID.

Inserting records into a database is a common task when building applications. CodeIgniter provides an easy-to-use interface for executing insert queries, which can save time and effort when developing your application.

In this tutorial, we will provide examples of CodeIgniter insert record queries and show you how to use them to insert data into a database. We will also explain how to retrieve the last inserted ID, which can be useful when working with related tables.

By the end of this tutorial, you will be able to confidently use CodeIgniter’s insert query functions and understand how to get the last inserted ID.

In summary, this tutorial covers the basics of inserting records into a database using CodeIgniter, with examples and an explanation of how to retrieve the last inserted ID. With this knowledge, you can start building powerful applications that can interact with a database in a seamless and efficient manner.

Codeigniter Insert Query

Contents

  • Insert Query
  • Single Record Insert
  • Insert Multiple Record
  • Insert String Query
  • Get Inserted ID
  • Affected Row
$query = "insert into users (name, contact_no, email)
        values ('torqueprogramming, 8888888888, 'support@torqueprogramming.co.in')";
$this->db->query($query);

Single Record Insert

Syntax

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

You can use the below query for insert single record into database.

$data = array( 
        'name'  =>  'torqueprogramming', 
        'contact_no'=> '8888899999', 
        'email'   =>  'support@torqueprogramming.co.in'
    );
$this->db->insert('users', $data);

Insert Multiple Records

Syntax

$this->db->insert_batch('table_name', $array_of_data);

You can use the below query for insert multiple recode into database. You can use insert_batch method for inserting or storing multiple record in single query.

$data = array( 
          array( 
            'name'  =>  'torqueprogramming', 
            'contact_no'=> '8888899999', 
            'email'   =>  'support@torqueprogramming.co.in'
        ),
        array( 
            'name'  =>  'torqueprogramming.co.in', 
            'contact_no'=> '8888899999', 
            'email'   =>  'support-to@torqueprogramming.co.in'
        ),
      ),
$this->db->insert_batch('users', $data);

Insert Syntax

	
$this->db->insert_string('table_name', $data);

This function simplifies the process of writing database inserts. This gives the correctly formatted SQL inserted string.

$data = array( 
        'name'  =>  'torqueprogramming', 
        'contact_no'=> '8888899999', 
        'email'   =>  'support@torqueprogramming.co.in'
    );
$this->db->insert_string('users', $data);

Get Inserted ID

Syntax

$this->db->insert_id()

After successfully insert a record into database. Last inserted record from database, You can get this last insert id using the insert_id() function of codeigniter.

$data = array( 
        'name'  =>  'torqueprogramming', 
        'contact_no'=> '8888899999', 
        'email'   =>  'support@torqueprogramming.co.in'
    );
$this->db->insert('users', $data);
return $this->db->insert_id();

Affected Row

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

$this->db->affected_rows();

You May Like

Login Register Form in Codeigniter with Validation & Session

Integrate Stripe Payment Gateway in Codeigniter

Export Data to Excel/CSV in Codeigniter Using PHPExcel

Leave a Reply

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