How to check if id already exists - codeigniter

前端 未结 7 1399
独厮守ぢ
独厮守ぢ 2021-02-09 06:56

i am trying to check if an id in the database already exists and if it does not then only insert that id and not the other ones that exist

I have tried to do a where sta

7条回答
  •  误落风尘
    2021-02-09 07:28

    For Checking An Id Or any column value exist not in database CI have a validation rule for it.

    See it live here: Validation Rule

    Rule: is_unique

    Returns FALSE if the form element is not unique to the table and field name in the parameter. Note: This rule requires Query Builder to be enabled in order to work.

    Example: is_unique[table.field]

    $this->form_validation->set_rules(
            'username', 'Username',
            'required|min_length[5]|max_length[12]|is_unique[users.username]',
            array(
                    'required'      => 'You have not provided %s.',
                    'is_unique'     => 'This %s already exists.'
            )
    );
    

    For More Advance use of validation, You can add all validation Setting Rules Using an Array.

            $this->form_validation->set_rules(
                'username', 'Username',
                'required|min_length[5]|max_length[12]|is_unique[users.username]',
                array(
                        'required'      => 'You have not provided %s.',
                        'is_unique'     => 'This %s already exists.'
                )
        );
    
    
        $config = array(
            'your_rule_name' => array(
                    array(
                            'username', 'Username',
                            'required|min_length[5]|max_length[12]|is_unique[users.username]',
                            array(
                                    'required'      => 'You have not provided %s.',
                                    'is_unique'     => 'This %s already exists.'
                            )
                    )
            ),        
            array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'required'
            )
    );
    
    $this->form_validation->set_rules($config);
    

提交回复
热议问题