本文介绍了如何检查用户名是否已经存在于codeigniter中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站在codeigniter。我不是那么专家使用framework.Hera我必须检查电子邮件是否已经存在database.I已编码所需的功能,但我收到一个错误,当提交表单。
我的代码是:
在控制器中我有以下规则。

I am working on a site in codeigniter.I am not so expert using framework.Hera 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.My code is:In controller i have made the following rule.

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

我提交表单时,错误。

遇到PHP错误

严重性:通知

消息:未定义的补偿:1

Message: Undefined offset: 1

文件名:libraries / Form_validation.php

Filename: libraries/Form_validation.php

发生数据库错误

错误号码:1064

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

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

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

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

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

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

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

Line Number: 330Anyone help me please.Thanks in advance.

推荐答案

您的问题在set_rules行。这里使用 is_unique 回调函数。你必须使用任何人。如果使用call_back函数检查重复数据;不需要使用is_unique。 对于这个错误你得到那个错误只要从那里删除 is_unique

Your problem is in the set_rules line. Here you use both is_unique and a callback function. 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 error Just remove the is_unique from there.

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

尝试一下,让我知道。

这篇关于如何检查用户名是否已经存在于codeigniter中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:39