CodeIgniter - Checking to see if a value already exists in the database

后端 未结 7 1743
我在风中等你
我在风中等你 2020-12-24 13:25

Im running CodeIgniter for a project of mine ... Im fairly new to this, i have the standard form validation checks for my form, except im unsure of how to check if the value

相关标签:
7条回答
  • 2020-12-24 13:54

    Yes, if you want to check whether a value already exist in the database, it's correct to add is_unique in the validation side. So, you don't need to create a function in your controller to do it.

    $this->form_validation->set_rules('rolename', 'Role Name', 'trim|required|xss_clean|is_unique[table_name.table_field]');
    
    $this->form_validation->set_rules('rolekey', 'Role Key', 'trim|required|xss_clean|is_unique[table_name.table_field]');
    

    Then, you will see an error message in the input area. For example: The username field must contain a unique value.

    I have tested it in Codeigniter 3.0.0.

    0 讨论(0)
提交回复
热议问题