如何检查具有CodeIgniter的db中已存在的电子邮件

时间:2014-12-04 13:03:08

标签: php codeigniter

我在CodeIgniter中做应用程序,我不知道如何在CodeIgniter中比较电子邮件和db,请任何人帮助我。

控制器

public function signup()
{
    $this->load->view('signup');

}
public function savedata(){

$this->load->library('form_validation');        
$this->form_validation->set_rules('firstname', 'firstname', 'required'); 
$this->form_validation->set_rules('lastname', 'lastname', 'required'); 


if ($this->form_validation->run() == TRUE) // Only add new option if it is unique
{       
    $result = $this->account_model->insert();
    if($result > 0)
    {
    $data['message'] = "Added successfully";    

    $this->load->view('signup',$data);
    }
    else
    {
    $this->index(); 
    }
}
else
{
    $this->load->view('signup');
}
}

如何发送电子邮件以及是否存在电子邮件。

6 个答案:

答案 0 :(得分:8)

假设您有user表和email列。因此,您必须在表单验证中添加此行

$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[users.email]');

注意 并且需要在email列中添加唯一索引

检查Documentation

答案 1 :(得分:5)

添加规则:

$this->form_validation->set_rules('email', 'Email', 'callback_rolekey_exists');

在同一个控制器中:

function rolekey_exists($key) {
  $this->roles_model->mail_exists($key);
}

在模型中,

function mail_exists($key)
{
    $this->db->where('email',$key);
    $query = $this->db->get('users');
    if ($query->num_rows() > 0){
        return true;
    }
    else{
        return false;
    }
}

Reference

答案 2 :(得分:0)

 //in controller    
public function usernameExist($username)
{
    $user = $this->Model-> usernameExist($username);

    if ($user) {
        $this->form_validation->set_message(
            'usernameExist',
            'Username  is already exist.'
        );
        return false;
    } else {
        return true;
    }
}

答案 3 :(得分:0)

function username_Exist($key)
{
    $this->db->where('username', $key);
    $query = $this->db->get('signup');
    if ($query->num_rows() > 0) {
        return true;
    } else {
        return false;
    }
}

//在模型中输入

答案 4 :(得分:0)

 $this->form_validation->set_rules('username', 'User', 'trim|required|callback_username_Exist');
        //type in controller in form validation

答案 5 :(得分:0)

最佳方式

$this->form_validation->set_rules(
            'email', 'Email', 'valid_email|required|is_unique[usr_user.user_email]',
            array('is_unique' => 'This %s already exists.')
        );