{"id":66,"date":"2017-03-04T19:45:46","date_gmt":"2017-03-04T11:45:46","guid":{"rendered":"http:\/\/apps.badjoerichards.com\/apps\/developerhack\/?p=66"},"modified":"2017-03-10T01:01:04","modified_gmt":"2017-03-09T17:01:04","slug":"add-duplicate-key-insert-query-builder-codeigniter-3","status":"publish","type":"post","link":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/","title":{"rendered":"How to add On Duplicate Key Insert into Query Builder in CodeIgniter 3?"},"content":{"rendered":"<p>This hack in CodeIgniter 3 does the following:<br \/>\nIf a primary or unique key in the database is the same as one of the insert values then it updates if not then it inserts.<\/p>\n<p>&nbsp;<\/p>\n<p>Add this function into\u00a0mysqli_driver.php in CI\\system\\database\\drivers\\mysqli.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n\/**\r\n* ON DUPLICATE UPDATE statement\r\n*\r\n* Generates a platform-specific on duplicate key update string from the supplied data\r\n*\r\n* @author Jeric T &lt;jeric@badjoerichards.com&gt; based off (Chris Miller &lt;chrismill03@hotmail.com&gt;)\r\n* @since 3.0.0\r\n* @access public\r\n* @param string the table name\r\n* @param array the update\/insert data\r\n* @return string\r\n*\/\r\nfunction _duplicate_insert($table, $values)\r\n{\r\n $updatestr = array();\r\n $keystr = array();\r\n $valstr = array();\r\n \r\n foreach($values as $key =&gt; $val)\r\n {\r\n $updatestr[] = $key.&quot; = &quot;.$val;\r\n $keystr[] = $key;\r\n $valstr[] = $val;\r\n }\r\n \r\n $sql = &quot;INSERT INTO &quot;.$this-&gt;_escape_str($table).&quot; (&quot;.implode(', ',$keystr).&quot;) &quot;;\r\n $sql .= &quot;VALUES (&quot;.implode(', ',$valstr).&quot;) &quot;;\r\n $sql .= &quot;ON DUPLICATE KEY UPDATE &quot;.implode(', ',$updatestr);\r\n \r\n return $sql;\r\n} \r\n\r\n \/\/ --------------------------------------------------------------------\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Then add this function into CI\\system\\database\\DB_query_builder.php<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n\/**\r\n* On Duplicate Key Update\r\n*\r\n* Compiles an on duplicate key update string and runs the query\r\n* \r\n* @author Jeric T &lt;jeric@badjoerichards.com&gt; based off (Chris Miller &lt;chrismill03@hotmail.com&gt;)\r\n* @since 3.0.0\r\n* @access public\r\n* @param string the table to retrieve the results from\r\n* @param array an associative array of update value\r\n* @return object\r\n*\/\r\n\r\nfunction on_duplicate($table = '', $set = NULL )\r\n{\r\n if ( ! is_null($set))\r\n {\r\n $this-&gt;set($set);\r\n }\r\n\r\n if (count($this-&gt;qb_set) == 0)\r\n {\r\n if ($this-&gt;db_debug)\r\n {\r\n return $this-&gt;display_error('db_must_use_set');\r\n }\r\n return FALSE;\r\n }\r\n\r\n if ($table == '')\r\n {\r\n if ( ! isset($this-&gt;qb_from[0]))\r\n {\r\n if ($this-&gt;db_debug)\r\n {\r\n return $this-&gt;display_error('db_must_set_table');\r\n }\r\n return FALSE;\r\n }\r\n \r\n $table = $this-&gt;qb_from[0];\r\n }\r\n \r\n \r\n $sql = $this-&gt;_duplicate_insert($this-&gt;protect_identifiers($table), $this-&gt;qb_set );\r\n \r\n $this-&gt;_reset_write();\r\n return $this-&gt;query($sql);\r\n}\r\n\r\n\/\/ --------------------------------------------------------------------\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Example usage:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n$data = array(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0'id'\u00a0\u00a0\u00a0\u00a0=&gt; 10,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0'title' =&gt; $title,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0'name'\u00a0\u00a0=&gt; $name,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0'date'\u00a0\u00a0=&gt; $date\r\n\u00a0\u00a0\u00a0\u00a0\u00a0);\r\n\r\n$this-&gt;db-&gt;on_duplicate('mytable', $data);\r\n\r\n\/\/ INSERT INTO mytable (id, title, name, date)\r\n\/\/ VALUES ({$id}, {$title}, {$name}, {$date})\r\n\/\/ ON DUPLICATE KEY UPDATE id={$id}, title={$title}, name={$name}, date={$date};\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This hack in CodeIgniter 3 does the following: If a primary or unique key in the database is the same as one of the insert values then it updates if not then it inserts. &nbsp; Add this function into\u00a0mysqli_driver.php in CI\\system\\database\\drivers\\mysqli. &nbsp; Then add this function into CI\\system\\database\\DB_query_builder.php &nbsp; &nbsp; Example usage: &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to add On Duplicate Key Insert into Query Builder in CodeIgniter 3? - Developer Hack<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to add On Duplicate Key Insert into Query Builder in CodeIgniter 3? - Developer Hack\" \/>\n<meta property=\"og:description\" content=\"This hack in CodeIgniter 3 does the following: If a primary or unique key in the database is the same as one of the insert values then it updates if not then it inserts. &nbsp; Add this function into\u00a0mysqli_driver.php in CIsystemdatabasedriversmysqli. &nbsp; Then add this function into CIsystemdatabaseDB_query_builder.php &nbsp; &nbsp; Example usage: &nbsp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/\" \/>\n<meta property=\"og:site_name\" content=\"Developer Hack\" \/>\n<meta property=\"article:published_time\" content=\"2017-03-04T11:45:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-03-09T17:01:04+00:00\" \/>\n<meta name=\"author\" content=\"Bad Joe\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bad Joe\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#website\",\"url\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/\",\"name\":\"Developer Hack\",\"description\":\"Hacks, tips, tricks and best practices for developers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/\",\"url\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/\",\"name\":\"How to add On Duplicate Key Insert into Query Builder in CodeIgniter 3? - Developer Hack\",\"isPartOf\":{\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#website\"},\"datePublished\":\"2017-03-04T11:45:46+00:00\",\"dateModified\":\"2017-03-09T17:01:04+00:00\",\"author\":{\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/01df012e94dd976ac49e50cdbf0e38d6\"},\"breadcrumb\":{\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to add On Duplicate Key Insert into Query Builder in CodeIgniter 3?\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/01df012e94dd976ac49e50cdbf0e38d6\",\"name\":\"Bad Joe\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5d413dbb6a01d82a56f2ceb6a82e8704?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5d413dbb6a01d82a56f2ceb6a82e8704?s=96&d=mm&r=g\",\"caption\":\"Bad Joe\"},\"url\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to add On Duplicate Key Insert into Query Builder in CodeIgniter 3? - Developer Hack","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/","og_locale":"en_US","og_type":"article","og_title":"How to add On Duplicate Key Insert into Query Builder in CodeIgniter 3? - Developer Hack","og_description":"This hack in CodeIgniter 3 does the following: If a primary or unique key in the database is the same as one of the insert values then it updates if not then it inserts. &nbsp; Add this function into\u00a0mysqli_driver.php in CIsystemdatabasedriversmysqli. &nbsp; Then add this function into CIsystemdatabaseDB_query_builder.php &nbsp; &nbsp; Example usage: &nbsp;","og_url":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/","og_site_name":"Developer Hack","article_published_time":"2017-03-04T11:45:46+00:00","article_modified_time":"2017-03-09T17:01:04+00:00","author":"Bad Joe","twitter_card":"summary","twitter_misc":{"Written by":"Bad Joe","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#website","url":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/","name":"Developer Hack","description":"Hacks, tips, tricks and best practices for developers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/","url":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/","name":"How to add On Duplicate Key Insert into Query Builder in CodeIgniter 3? - Developer Hack","isPartOf":{"@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#website"},"datePublished":"2017-03-04T11:45:46+00:00","dateModified":"2017-03-09T17:01:04+00:00","author":{"@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/01df012e94dd976ac49e50cdbf0e38d6"},"breadcrumb":{"@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/add-duplicate-key-insert-query-builder-codeigniter-3\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/"},{"@type":"ListItem","position":2,"name":"How to add On Duplicate Key Insert into Query Builder in CodeIgniter 3?"}]},{"@type":"Person","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/01df012e94dd976ac49e50cdbf0e38d6","name":"Bad Joe","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5d413dbb6a01d82a56f2ceb6a82e8704?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5d413dbb6a01d82a56f2ceb6a82e8704?s=96&d=mm&r=g","caption":"Bad Joe"},"url":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/posts\/66"}],"collection":[{"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/comments?post=66"}],"version-history":[{"count":8,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/posts\/66\/revisions\/72"}],"wp:attachment":[{"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/media?parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/categories?post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/tags?post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}