Welcome Guest, Not a member yet? Register   Sign In
Mysql/ database question
#1

[eluser]megabyte[/eluser]
I'm building an form that inserts the info intot he db.

I have a "unique" column.

Is there a way to check if the value already exists when writing the insert, or do you have to check first then choose to insert or not.

I'm just wonderign if there is a short cut.

As we speak, if I try to do an insert and the value already exists then it throws an error. Maybe there is a way to capture error codes?
#2

[eluser]Unknown[/eluser]
I would do an update first and if $this->db->affecte_rows() == 0 then do an insert.
#3

[eluser]Seppo[/eluser]
A correct way to validate a field as unique is using validation callbacks.
Take a look at Validation, under the title "Callbacks: Your own Validation Functions"
#4

[eluser]megabyte[/eluser]
Thanks Seppo. That makes total sense. I do use callbacks, but never thought to use one for that.




Theme © iAndrew 2016 - Forum software by © MyBB