Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter's is_unique always saying value already exists

I'm using CodeIgniter's form validation, and I've spent a lot of time trying to fix this, with no luck. I have this field:

<input type="text" name="user" id="user" length="20" placeholder="Username">

And I'm using this to validate:

$this->form_validation->set_rules('user', 'Username', 'trim|required|min_length[3]|max_length[20]|alpha_dash|is_unique[users.user]');

My db has a table users and user is a field in it, so I don't know what I'm doing wrong, or what the problem is. The table is empty (but I've also tried with it having records) and in phpmyadmin the "unique" icon is selected. I know the db connection is working fine, because if I remove that rule and enter otherwise valid data and submit the form, then the user is added to the database.

Unless is_unique uses another db configuration file that I haven't configured? I don't really know. It's kind of frustrating and I'm thinking that I may as well just drop the use of a framework...

Your help would be great! Thanks.

like image 530
justanelephant Avatar asked Mar 06 '15 05:03

justanelephant


2 Answers

This might/might not help: You seem to be loading the DB after running the form validation. There's also a typo uses.user.

like image 102
Aaria Carter-Weir Avatar answered Oct 06 '22 20:10

Aaria Carter-Weir


In Transact-SQL the word "USER" is a special word. Try surrounding uses.user with a back-ticks like so:

`users.user`
...see if that helps.
like image 33
ChrisA Avatar answered Oct 06 '22 20:10

ChrisA