Codeigniter - 仅当数据库中不存在电子邮件时才更新电子邮件 [英] Codeigniter - Update email only if the email doesn't exist in the database

查看:88
本文介绍了Codeigniter - 仅当数据库中不存在电子邮件时才更新电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更新页面,供我的用户修改他们的姓名,电子邮件和其他信息。

I have an update page for my users where they can edit their name, email and other info.

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

So far, they can edit everything. Including their email. They can enter an email that already exists in the database without any issue.

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

I have tried adding this form validation rule

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

但这没有帮助,因为它会要求用户输入另一个电子邮件,如果他们单击保存按钮,甚至如果他们不想更改他们的电子邮件。

But that doesn't help because it will ask the user to enter another email if they click the save button, even if they don't want to change their email.

我只是想使它,以便当他们单击保存按钮,只有更新电子邮件,如果用户已更改

I just want to make it so that when they click the save button, only update the email if the user has changed it AND check that the email doesn't exist in the database before saving.

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

I have tried doing this but no luck.

我玩的代码:

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


推荐答案

p>

Your Javascript file:

$("#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";
        }
    }

这篇关于Codeigniter - 仅当数据库中不存在电子邮件时才更新电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆