Search code examples
phpsqlcodeignitercodeigniter-4

Get true/false codeigniter insert function


every time I use insert function in my model, the return always true, here's my code

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

call code

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

even the execution failed (cause of same primary key already exists), code always return "success", how to fix it?

Thanks


Solution

  • check if inserted successfuly

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