Codeigniter - 仅当数据库中不存在电子邮件时才更新电子邮件

时间:2015-03-05 00:27:34

标签: php database codeigniter

我的用户有一个更新页面,可以编辑他们的姓名,电子邮件和其他信息。

到目前为止,他们可以编辑所有内容。包括他们的电子邮件。他们可以输入已存在于数据库中的电子邮件而不会出现任何问题。

我已尝试添加此表单验证规则

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

但这没有用,因为如果他们点击保存按钮,即使他们不想更改他们的电子邮件,也会要求用户输入另一封电子邮件。

我只想这样做,以便当他们点击保存按钮时,只有在用户更改了电子邮件时才更新电子邮件并在保存之前检查数据库中是否存在该电子邮件。

我试过这样做但没有运气。

我正在玩的代码:

$first_name = $this->input->post('first_name');
    $last_name = $this->input->post('last_name');
    $email = $this->input->post('email');
    $uid = $this->session->userdata('uid');

    //$query = $this->db->get('dayone_entries');
    $query = $this->db->query('SELECT uid, email FROM users');


    $sql = "UPDATE users SET first_name = '{$first_name}', last_name = '{$last_name}', email = '{$email}' WHERE uid = $uid LIMIT 1";
    $this->db->query($sql);

    if ($this->db->affected_rows() === 1) {

        return true;
    } else {
        return false;
    }

3 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

$this->form_validation->set_rules('email', 'lang:email', 'trim|required|valid_email|callback__is_unique_email[email]');

并且回调函数应该如下所示:

public  function _is_unique_email($value, $field){
    $result = $this->db->where('uid !=', $this->session->userdata('uid'))
                    ->where($field, $value)
                    ->get('users')
                    ->row_array();

    if ($result) {
        $this->form_validation->set_message('_is_unique_email', $this->lang->line('_is_unique_'));
        return false;
    }

    return true;
}

答案 1 :(得分:1)

您的Javascript文件:

$("#form_id").validate({
        rules: {
            email: {
                required: true,
                email: true,
                remote: {
                    type: "post",
                    url: "pathtocontroller/controller.php/checkEmail",
                 }
            }
        },
        messages: {
            email: {
                required: "Please enter Email!",
                email: "Please enter valid Email!",
                remote: "Email already not available!"
            }
        }

您的控制器文件:

function checkEmail() {
        $userArr = $this->input->post();
        $id = $this->session->userdata('id');//if you have stored id within session else pass it within remote function
        if (isset($userArr["email"])) {
            if ($id != '') {
                $ext_cond = "id !='" . $id . "'";
            }
            echo $this->your_model_name->getUserValidation('your_email_field_name', $userArr['email'], $ext_cond);
            exit;
        }
        exit;
    }

你的模特:

public function getUserValidation($usertype = '', $value = '', $cond = '') {

        if ($usertype != '' && $value != '') {
            $this->db->select($usertype);
            $this->db->from($this->main_table);
            if ($cond != '') {
                $this->db->where($cond);
            }

            if (is_array($usertype)) {
                foreach ($usertype as $key => $type_value) {
                    $this->db->where($type_value, $value[$key]);
                }
            } else {
                $this->db->where($usertype, $value);
            }

            $user_data = $this->db->get()->result_array();
//            echo $this->db->last_query();exit;
            if (is_array($user_data) && count($user_data) > 0) {
                return "false";
            } else {
                return "true";
            }
        } else {
            return "false";
        }
    }

答案 2 :(得分:0)

使用验证库:http://docs.jquery.com/Plugins/Validation/Methods/remote

你的javascript:

$("#yourFormId").validate({
        rules: {
            email: {
                required: true,
                email: true,
                remote: {
                    url: "checkmail.php",
                    type: "post"
                 }
            }
        },
        messages: {
            email: {
                required: "Please Enter Email!",
                email: "This is not a valid email!",
                remote: "Email already in use!"
            }
        }
    });

checkmail.php:

<?php
$registeredEmails = array('test1@test.com', 'test2@test.com', 'test3@test.com');

$requestedEmail  = $_POST['email'];

if( in_array($requestedEmail, $registeredEmails) ){
    echo 'false';
}
else{
    echo 'true';
}
?>

如果你使用codeigniter意味着使用checkmail.php作为控制器函数.. 您可以将$ registeredEmails数组值作为$ registeredEmails [] =“您的查询结果传递给for循环”。