codeigniter
  1. codeigniter-database-insert

Database Insert

Database Insert is a core function of CodeIgniter Database that allows users to insert data into a database. The insert function is used to add new rows into a database table with data provided in the form of an array.

Syntax

The syntax for Database Insert is as follows:

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

Example

Consider the following example where we want to insert a new record into the employee table:

$data = array(
    'id' => '1001',
    'first_name' => 'John',
    'last_name' => 'Doe',
    'email' => 'john.doe@example.com'
);

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

This will insert a new record with the given data into the employee table.

Explanation

In the example above, we have created a data array with key-value pairs for each field in the table. We pass this data array along with the table name to the insert function. The insert function then generates an SQL INSERT statement and executes it on the database server.

Use

Database Insert is used to add new data into a database table. It can be used in a variety of applications such as web applications, enterprise software, and mobile apps to add new data into a database.

Important Points

  • The data to be inserted must be provided in the form of an associative array.
  • Fields that have not been specified in the data array will be set to their default value.
  • If the table has an AUTO_INCREMENT field, it will be automatically incremented for each new record.

Summary

Database Insert is a core feature of CodeIgniter Database that allows users to insert data into a database table. It requires the data to be provided in the form of an associative array, and can be used in a variety of applications to add new data into a database. Fields that are not specified in the data array are set to their default value, and AUTO_INCREMENT fields are automatically incremented for each new record.

Published on: