php 如何检查用户名是否已存在于codeigniter中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18498986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:39:42  来源:igfitidea点击:

how to check if username already exists in codeigniter

phpcodeigniter

提问by user2702406

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. In controller i have made the following rule.

我正在使用 codeigniter 在一个网站上工作。我在使用框架方面不是很专业。在这里我必须检查电子邮件是否已经存在于数据库中。我已经对所需的功能进行了编码,但是在提交表单时出现错误。在控制器中,我制定了以下规则。

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

文件名:库/Form_validation.php

Line Number: 953

行号:953

A Database Error Occurred

发生数据库错误

Error Number: 1064

错误编号:1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ` = '[email protected]' LIMIT 1' at line 2

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 2 行的“WHERE ` = '[email protected]' LIMIT 1' 附近使用的正确语法

SELECT * WHERE ` = '[email protected]' LIMIT 1

SELECT * WHERE ` = '[email protected]' 限制 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. Thanks in advance.

行号:330 请任何人帮助我。提前致谢。

采纳答案by Sharif

Your problem is in the set_rules line. Here you use both is_uniqueand a callbackfunction. You have to use anyone of that. If you use a call_back function to check duplicate data; doesn't need to use is_unique. For this wrong you get that errorJust remove the is_uniquefrom there.

您的问题出在 set_rules 行中。在这里您可以同时使用is_unique和 一个callback函数。你必须使用任何一个。如果使用 call_back 函数检查重复数据;不需要使用 is_unique。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.

试试这个,让我知道。

回答by Nikunj Dhimar

$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;
        }
    }

回答by bikram kc

Just add is_unique[tablename.fieldname]rules to the form validation. for Example:

只需is_unique[tablename.fieldname]向表单验证添加规则即可。例如:

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

回答by Kiran Shrestha

you can check if a field is unique or not by using form_validator function is_unique[table_name.field_name]

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

回答by Sizzling Code

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');

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

it would be like

它会像

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

回答by abhinsit

You have an error in the mysql query:-

您在 mysql 查询中有错误:-

SELECT * WHERE ` = '[email protected]' 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.

希望这可以帮助。

回答by Dave Driesmans

no need to a make a special function, this is enough

不需要做一个特殊的功能,这就够了

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

回答by Avin Varghese

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'
);