在codeigniter中检查mysql数据库中的预存记录

时间:2012-08-02 09:37:29

标签: codeigniter

我正在检查我的数据库中已经存在的国家名称我使用了回调但它不会发现任何错误

$this->form_validation->set_rules('country_name', 'Country Name', 
       'trim|required|xss_clean|callback_check_exist');

这是我的控制器功能

function check_exist($country_name)
    {
         $this->countrymodel->country_exists($country_name);

    }

这是我的模特

function country_exists($key)
        {
            $this->db->where('country_name',$key);
            $this->db->from($this->dbname);
            $query = $this->db->get();
            if ($query->num_rows()> 0){
                return true;
            }
            else{
                return false;
            }
        }

1 个答案:

答案 0 :(得分:0)

回调函数应该返回一个值并设置一条消息:

function check_exist($country_name)
{
     if (!$this->countrymodel->country_exists($country_name)) {
         $this->form_validation->set_message('check_exist', 'an error message');
         return FALSE;
     }
     else {
         return TRUE;
     }
}
相关问题