To insert data, Codeigniter has provided insert function for single row insert and insertBatch function for multiple data insert. In this function, we can pass data in array format or in object format.
public function insert_record()
{
$user_data = array(
'name' => 'lokesh meena',
'age' => 20,
'salary'=> 5000
);
$query = $this->seller->table('users')
->insert($user_data);
if($query == true){
echo "data insert";
}
else{
echo "data fail";
}
}
When inserting duplicate data, we get an error from the database. If we pass true in the ignore function, then we can stop the error on duplicate entry.
public function insert_record()
{
$user_data = array(
'id' => 36,
'name' => 'lokesh meena',
'age' => 20,
'salary'=> 5000
);
$query = $this->seller->table('users')
->ignore(true)
->insert($user_data);
if($query == true){
echo "data insert";
}
else{
echo "data fail";
}
}
With insertBatch, we can insert multiple records at once. In this, the data passes multiple records into the array.
public function insert_record()
{
$user_data = array(
array(
'name' => 'lokesh meena',
'age' => 20,
'salary'=> 5000
),
array(
'name' => 'priti tailor',
'age' => 21,
'salary'=> 5500
)
);
$query = $this->seller->table('users')
->insertBatch($user_data);
if($query == true){
echo "data insert";
}
else{
echo "data fail";
}
}
My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in