首页 > 解决方案 > 获取真/假codeigniter插入功能

问题描述

每次我在模型中使用插入函数时,返回总是正确的,这是我的代码

public function insert_text($data)
{
    return $this->db->table($this->table)->insert($data);
}

呼叫代码

if ($this->text->insert_text($data)) {
    echo "success";
} else {
    echo "failed";
}

即使执行失败(由于相同的主键已经存在),代码总是返回“成功”,如何解决?

谢谢

标签: phpsqlcodeignitercodeigniter-4

解决方案


检查是否插入成功

if ($this->db->insert_id()) {
    echo "success";
} else {
    echo "failed";
}

推荐阅读