简体   繁体   中英

How to check if a value already exists in the database with codeigniter

I found this line: $this->form_validation->set_rules(); But I have no idea where to use it. it seems complicated and everywhere its mentioned its about 'username' and 'password' fields. I have a variable called 'utr' which I am POSTing to the same page with ajax submit, now I want to to check if it is a unique value that is submitted on form submit and not already in my database. The table name is 'orders' and the column name is 'utr'. Thank you.

You can check any value which is already exist in database try this code:

utr is the name which is send by form and is_unique validate table.coulmn in database.

$this->form_validation->set_rules(
        'utr', 'UTR Title',
        'required|is_unique[orders.utr]',
        array(
                'required'      => 'You have not provided %s.',
                'is_unique'     => 'This %s already exists.'
        )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM