【问题标题】:How to check multiple values to be unique in CodeIgniter如何在 CodeIgniter 中检查多个值是否唯一
【发布时间】:2022-01-21 16:48:53
【问题描述】:

我有一个用于注册用户的 html 表单。我想在 codeIgniter 中应用一个条件,即如果有人编写了数据库中已经存在的 2 个输入字段(名字和姓氏),则不允许插入数据。 喜欢我会写

$this->form_validation->set_rules('firstname', 'First Name', 'required|is_unique[student.firstname]');
$this->form_validation->set_rules('lastname', 'Last Name', 'required|is_unique[student.lastname]');

如何同时申请?就像两者都应该是唯一的,但一个已经存在。我想要“组合独特价值”之类的东西

示例:

名字:阿里

姓:莫尤丁

如果有人在两个字段中再次写相同的内容,那么我不想在数据库中插入数据,但如果有人写如下内容:

名字:阿里

姓:伊姆兰

那么我应该接受用户。

【问题讨论】:

标签: php codeigniter


【解决方案1】:

您可以在验证中使用回调来做到这一点

$this->form_validation->set_rules('firstname', 'First Name', 'required|callback_check_user');// call callback function

$this->form_validation->set_rules('lastname', 'Last Name', 'required');

检查名字和姓氏的组合

function check_user() {
    $first_name = $this->input->post('firstname');// get first name
    $last_name = $this->input->post('lastname');// get last name
    $this->db->select('user_id');
    $this->db->from('student');
    $this->db->where('firstname', $first_name);
    $this->db->where('lastname', $last_name);
    $query = $this->db->get();
    $num = $query->num_rows();
    if ($num > 0) {
        return FALSE;
    } else {
        return TRUE;
    }
}

【讨论】:

【解决方案2】:

一个简单的解决方案是添加自定义验证方法。

如果您还没有,请在您的 application/libraries 目录中创建一个名为 MY_Form_validation.php(区分大小写)的文件,其中包含以下内容:

class MY_Form_validation extends CI_Form_validation
{

    public function __construct($rules = array())
    {
        parent::__construct($rules);
    }
}

然后在那个文件中你可以有类似的东西:

    public function unique_user_name()
    {

        $firstname = $this->CI->input->post('firstname');
        $lastname = $this->CI->input->post('lastname');

        $check = $this->CI->db->get_where('users', array('firstname' => $firstname, 'lastname' => $lastname), 1);

        if ($check->num_rows() > 0) {

            $this->set_message('unique_user_name', 'This name already exists in our database');

            return FALSE;
        }

        return TRUE;
    }

最后你的规则会是这样的:

$this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|unique_user_name');

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    也许你对我的自定义 is_unique 函数感兴趣。 在这里。

    您可以通过两种方式使用它:

    1. is_unique[table_name.field_to_check.id_field_name.id_field_value] //<-- unique 1 field only
    
    
    2. is_unique[table_name.field_to_check.id_field_name != 'id_field_value' and anotherIdName='theValue'] //<-- custom where 
    

    只需将此代码保存在一个文件中,将其命名为 MY_Form_Validation.php,并将其放在库目录下。然后你可以把那些 is_unique

    //is_unique[table_name.field_to_check.id_field_name.id_field_value] <-- unique 1 field only
    //is_unique[table_name.field_to_check.id_field_name != 'id_field_value' and anotherIdName='theValue'] <-- custom where 
    class MY_Form_validation extends CI_Form_validation {
        public function is_unique($str, $field)
        {
            $hasil = true;
    
            try {
                //hanya validasi jika ada isinya. jika tidak ada isinya, tidak usah divalidasi.
                //karena kalau memang tidak boleh kosong, bisa kasih validasi required
                if ($str) {
                    $x = substr_count($field, '.');
                    if(strpos($field, "=") > 0) {
                        list($table, $field, $where) = explode('.', $field);
    //                    list($table, $field, $id_field, $id_val, $where) = explode('.', $field);
    
                        $is_unique = 0;
    
                        if ($where) {
                            $logos = "select * from $table where $field =? and $where ";
                        } else {
                            $logos = "select * from $table where $field =?  ";
                        }
    
                        $data = array($str);
                        $qq = $this->CI->db->query($logos, $data);
    
                        if (is_log_query()) {
                            $logos = $this->CI->db->last_query();
                            log_to_file($logos);
                        }
    
                        $row = $qq->row();
                        if ($row) {
                            if ($row->id) {
    //                            if ($row->$id_field == $id_val) {
    //                                $is_unique = 1; //berarti jika edit miliknya sendiri
    //                            } else {
    //                                //berarti id milik record lain
    //                            }
                            } else {
                                //berarti sudah ada di xuser_confirmed
                            }
                        } else {
                            $is_unique = 1; //belum ada sama sekali
                        }
    
                        $hasil = (bool)$is_unique;
    
                    }
                    else {
                        if ($x >= 3) {
                            list($table, $field, $id_field, $id_val) = explode('.', $field);
    
                            $is_unique = 0;
    
                            if ($id_field && $id_val) {
                                $logos = "select * from $table where $field =? and $id_field != '$id_val' ";
                            } else {
                                $logos = "select * from $table where $field =?  ";
                            }
    
                            $data = array($str);
                            $qq = $this->CI->db->query($logos, $data);
    
                            if (is_log_query()) {
                                $logos = $this->CI->db->last_query();
                                log_to_file($logos);
                            }
    
                            $row = $qq->row();
                            if ($row) {
                                if ($row->id) {
                                    if ($row->$id_field == $id_val) {
                                        $is_unique = 1; //berarti jika edit miliknya sendiri
                                    } else {
                                        //berarti id milik record lain
                                    }
                                } else {
                                    //berarti sudah ada di xuser_confirmed
                                }
                            } else {
                                $is_unique = 1; //belum ada sama sekali
                            }
    
                            $hasil = (bool)$is_unique;
    
    
                        } 
                        else if ($x == 1) {
                            list($table, $field) = explode('.', $field);
                            $query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
                            $hasil = $query->num_rows() === 0;
                        }
                    }
                }
    
            }catch (Exception $e) {
                die($e->getTraceAsString());
            }
            return $hasil;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多