Welcome Guest, Not a member yet? Register   Sign In
Database class - catch insert error

Hi all,

When I use
$this->db->insert('mytable', $mydata)
to insert data into a mysql table, sometimes I get an error like
'A Database Error Occurred ... Error Number: 1062 ... Duplicate entry 'zeff' for key 1'
, because I don't use an autoincrement primary key but just a unique varchar field.
I have read that 'write' actions only return true or false, but isn't there a manner to intercept the 1062 error code and show the user a nicely styled paged saying 'the record already exists'?

Many thanks in advance!


No solution yet ...

So I choose the following workaround: (maybe it can help others :-))

$sql ="SELECT * WHERE `uid` ='" . $uid . "'";
where the uid field is my primary key field.
Now with the following code, I handle duplicate key problems:
    if($this->db->insert('accounts', $data)){
        $this->data["content"]  = "<div class='ok'>Record successfully added...</div>";
        $this->data["content"]  = "<div class='error'>Insert of new record failed!</div>";
    $this->data["content"] = "<div class='error'>Record already exists.</div>";
I do not use a model here, since I just use simple native active record functions...

Theme © iAndrew 2016 - Forum software by © MyBB