如何在Codeigniter中使用插入或替换 [英] How to uses insert or replace in codeigniter

查看:68
本文介绍了如何在Codeigniter中使用插入或替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public function save($data, $id)
    {
      $this->db->insert OR REPLACE($this->table, $data);
      return $this->db->insert_id();
    }

如果不存在的数据将被插入,否则将替换存在的数据

if the data does no exist will be insert else the data exist will be replace

推荐答案

如果您使用的是CodeIgniter v3.0.x,则只需 REPLACE 上的MySQL文档)

If you are using CodeIgniter v3.0.x then all you need is $this->db->replace() as it compiles and executes a REPLACE statement. (MySQL documentation on REPLACE)

诀窍是在$data数组中包含表的键字段(问题中的$id).然后,您只需要将一个参数传递给save().

The trick is to include your table's key field ($id in your question) in the $data array. Then you only need to pass one param to save().

 public function save($data)
  {
    $this->db->replace($this->table, $data);
    return $this->db->insert_id();
  }

我承认我不确定insert_id()在调用update()之后是否会返回任何内容.

I confess that I am not sure that insert_id() will return anything after update() is called.

这篇关于如何在Codeigniter中使用插入或替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆