Home > Database > Mysql Tutorial > How to Implement ON DUPLICATE KEY UPDATE in CodeIgniter Models?

How to Implement ON DUPLICATE KEY UPDATE in CodeIgniter Models?

Patricia Arquette
Release: 2024-10-29 10:59:02
Original
538 people have browsed it

How to Implement ON DUPLICATE KEY UPDATE in CodeIgniter Models?

Applying ON DUPLICATE KEY UPDATE in CodeIgniter Models

Q: I'm working with a CodeIgniter model and want to insert data into the database. How can I implement the ON DUPLICATE KEY UPDATE statement, which I used in my original SQL queries, within the ActiveRecord model?

A: CodeIgniter provides a workaround to add the "ON DUPLICATE" statement to your queries without modifying its core files. Here's how:

<code class="php">$sql = $this->db->insert_string('table', $data) . ' ON DUPLICATE KEY UPDATE duplicate=LAST_INSERT_ID(duplicate)';
$this->db->query($sql);
$id = $this->db->insert_id();</code>
Copy after login

By executing this custom SQL statement, you can insert data into the database and update the duplicate counter if the key already exists.

The above is the detailed content of How to Implement ON DUPLICATE KEY UPDATE in CodeIgniter Models?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template