Dynamically Insert and Update Values in CodeIgniter

Dynamically Insert and Update Values in CodeIgniter

In this Post We Will Explain About is Dynamically Insert and Update Values in CodeIgniter With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Dynamic Insert, Update Operations using CodeIgniter Example

In this post we will show you Best way to implement Dynamic insert update delete in codeigniter using ajax, hear for Dynamic Insert Update using the Update component in CodeIgniter with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




INSERT INTO table (x, y, z) VALUES (21,22,53)
  ON DUPLICATE KEY UPDATE z=z+1;

A Generic CodeIgniter Function for Update and Insert
 public function dataUpdateOnDup($table, $alldta ) {
     if (empty($table) || empty($alldta)) return false;
     $fake_duplicat_dta = array();
     
     foreach($alldta AS $key => $value) {
        $fake_duplicat_dta[] = sprintf("%s='%s'", $key, addslashes($value));
     }

     $querysql = sprintf("%s ON DUPLICATE KEY UPDATE %s", $this->db->insert_string($table, $alldta), implode(',', $fake_duplicat_dta));
     
     $this->db->query($querysql);
     return $this->db->insert_id();
}

ON DUPLICATE KEY UPDATE

Also Read :   Insert Image Into Database and folder in VueJS Laravel MySQL

note : The second data parameter of the function must be given all the data as an PHP associated array.

eg $alldta = array('name' => $name, 'email' => $email, 'website' => $website);

IT is a CodeIgniter model function and methods calling must be done in the CodeIgniter controller So since CodeIgniter is a major more coupled MVC Based you can data use it in controller example too.

Function Explanation

The PHP sprinf function or methods is a PHP Data core function or methods that will data return a some data formatted string. Eg

<?php
$num = 5;
$places = 'Gondal Chokdi';

$content_format = 'Live24u.com main are %d monkeys in the %s';
echo sprintf($content_format, $num, $places);
?>

The insert_string is a CodeIgniter mysql query make methods or function which returns a data correctly formatted string insert query string. Example

Also Read :   Dynemically HTML table from AJAX response using PHP

$Alldata = array('name' => $name, 'email' => $email, 'website' => $website);

$str = $this->db->insert_string('your_table_nm', $Alldata);

The first simple parameter data is the main table name, and then the second data is an associative Data types of the array with the main simple data to be here more inserted. The above example dynemic INSERT data value:

INSERT INTO your_table_nm (name, email, website) VALUES ('Jaydeep', 'jaydeep@live24u.com', 'live24u.com')

The PHP implode Join array HTML elements with a Data string. Eg Like as a

<?php

$myArr = array('userlname', 'email', 'mobile');
$data_comma_value = implode(",", $myArr);

echo $data_comma_value; // userlname,email,mobile

The Last insert_id is second CodeIgniter mysql query crate function or methods to insert your numeric id number when data store database insert.

Also Read :   AngularJS Image Upload using PHP

So we can Display that simple query $sql will Learning the same simple syntax as official MySQL UPDATE Data ON DUPLICATE Data syntax.

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example





Example

I hope you have Got What is Dynamically Insert and Update Values in CodeIgniter And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.