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

后端 未结 7 1748
我在风中等你
我在风中等你 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:36

    This works for me might work for someone

    In your controller, create a method similar to this:

    function rolekey_exists('table','field','value')
    {
      $this->roles_model->role_exists($key);
    }
    

    And in your model that handles roles, add something like this:

    function role_exists($table,$field,$value)
    {
        $this->db->where($field,$value);
        $query = $this->db->get($table);
        if (!empty($query->result_array())){
            return 1;
        }
        else{
            return 0;
        }
    }
    

提交回复
热议问题