简体   繁体   English

如何检查用户名是否已经在codeigniter中

[英]how to check if username already exists in codeigniter

I am working on a site in codeigniter.I am not so expert using framework.Here I have to check if email already exists in database.I have coded the required functionality but I am getting an error when submit the form. 我在codeigniter中的一个网站上工作。我不是使用框架的专家。在这里,我必须检查数据库中是否已经存在电子邮件。我已经编码了所需的功能,但是提交表单时遇到错误。 In controller i have made the following rule. 在控制器中,我制定了以下规则。

My code is: 我的代码是:

$this->form_validation->set_rules(
    'email', 'Email', 'trim|required|valid_email|is_unique|callback_isEmailExist'
);
public function isEmailExist($email) {
    $this->load->library('form_validation');
    $this->load->model('user');
    $is_exist = $this->user->isEmailExist($email);

    if ($is_exist) {
        $this->form_validation->set_message(
            'isEmailExist', 'Email address is already exist.'
        );    
        return false;
    } else {
        return true;
    }
}

in model user the function is : 在模型用户中,功能为:

function isEmailExist($email) {
    $this->db->select('id');
    $this->db->where('email', $email);
    $query = $this->db->get('users');

    if ($query->num_rows() > 0) {
        return true;
    } else {
        return false;
    }
}

When I submit the form, I am getting the following errors. 提交表单时,出现以下错误。

A PHP Error was encountered 遇到PHP错误

Severity: Notice 严重程度:注意

Message: Undefined offset: 1 消息:未定义的偏移量:1

Filename: libraries/Form_validation.php 文件名:libraries / Form_validation.php

Line Number: 953 行号:953

A Database Error Occurred 发生数据库错误

Error Number: 1064 错误号:1064

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ` = 'muraddnw@gmail.com' LIMIT 1' at line 2 检查与您的MySQL服务器版本相对应的手册,以在第2行的'WHERE`='muraddnw@gmail.com'LIMIT 1'附近使用正确的语法

SELECT * WHERE ` = 'muraddnw@gmail.com' LIMIT 1 SELECT * WHERE`='muraddnw@gmail.com'LIMIT 1

Filename: C:\\xampp\\htdocs\\aunction\\system\\database\\DB_driver.php 文件名:C:\\ xampp \\ htdocs \\ aunction \\ system \\ database \\ DB_driver.php

Line Number: 330 Anyone help me please. 行号:330任何人都可以帮助我。 Thanks in advance. 提前致谢。

Your problem is in the set_rules line. 您的问题出在set_rules行中。 Here you use both is_unique and a callback function. 在这里,您同时使用is_uniquecallback函数。 You have to use anyone of that. 您必须使用其中的任何一个。 If you use a call_back function to check duplicate data; 如果您使用call_back函数检查重复数据; doesn't need to use is_unique. 不需要使用is_unique。 For this wrong you get that error Just remove the is_unique from there. For this wrong you get that error只需从那里删除is_unique

$this->form_validation->set_rules('email','Email','trim|required|valid_email|callback_isEmailExist'); // removed is_unique

Try this out and let me know. 试试看,让我知道。

$this->form_validation->set_rules('username', 'userName', 'required|callback_exists_username');


#uniqueness of username
    function exists_username($str)
    {
        $record_id = $this->input->post('record_id');
        $condition = array('user_id !='=>$record_id,'username'=>$str);
        $value =GetAllRecord('user_master',$condition,$is_single=true);
        if (count($value) == 0)
        {
            return TRUE;
        }
        else
        {
            $this->form_validation->set_message('exists_username', 'username already exists!');
            return FALSE;
        }
    }

Just add is_unique[tablename.fieldname] rules to the form validation. 只需将is_unique[tablename.fieldname]规则添加到表单验证即可。 for Example: 例如:

 array('field' => 'email', 'label' => 'Email', 'rules' => 'trim|required|valid_email|is_unique[users.email]'));

Your Model is like 你的模特就像

$this->db->select('id');
$this->db->where('email', $email);

please tell the mysql from which table you want to get the record. 请告诉mysql您要从哪个表获取记录。

$this->db->from('tablename');

it would be like 就像

$this->db->select('id');
$this->db->from('tablename');
$this->db->where('email', $email);

You have an error in the mysql query:- 您在mysql查询中出错:-

SELECT * WHERE ` = 'muraddnw@gmail.com' LIMIT 1

Your code has not specified which table to query from. 您的代码未指定要查询的表。 So use following:- 所以使用以下:

$this->db->select('id');
$this->db->from('tablename');
$this->db->where('email', $email);

Hope this helps. 希望这可以帮助。

no need to a make a special function, this is enough 不需要做一个特殊的功能,这就足够了

$this->form_validation->set_rules(
    'email', 'Email', 'trim|required|valid_email|is_unique'
);

Use is_unique[table.field] add table name and field. 使用is_unique[table.field]添加表名和字段。

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

您可以使用form_validator函数is_unique[table_name.field_name]检查字段是否唯一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM