我应该如何更新电子邮件?

时间:2016-03-25 05:04:36

标签: php codeigniter

这是我更新电子邮件的验证..

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

回叫功能是

public function _unique_email($email) {
if ($this->crud_model->fetch_user_by_email_user_id($email,$this->session->userdata('user_id'))) {
    return true;
} else {
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
        return true;
        return false;
    }
}

如果用户在配置文件设置中更改了电子邮件,则应更新电子邮件,但如果用户未进行任何更改,则电子邮件应为旧电子邮件,如果他更改了电子邮件,则应通过检查天气更新电子邮件是唯一的在数据库中还是没有?

2 个答案:

答案 0 :(得分:0)

    function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('email', 'Email', 'required|callback_email_check');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('form');
        }
        else
        {

            if($this->update($email)){
                 $this->load->view('formsuccess');
            }
        }
     }

答案 1 :(得分:0)

I haven't used codeIgniter for years, So I don't exactly know how things are there. But I'll try to answer your question on a general context and try to express that using the same code in your question with minor changes.

When You want to check if the user changed his email or not you need to have his old email from database first or in a hidden field of the form, then compare the email field value with the old one will make you know if he changed it.

Second you need to check if the email have duplicates on the database, remember you need to do this only if the user changed his email address, so just run a query with the database. if there are no existing email update the user email in database else display an error and quit.



public function is_unique_email($email) {
$old_email = $this->crud_model->fetch_user_email($this->session->userdata('user_id')); 
        if ($old_email == $email) {
            return true;
        } else {
                $duplicates = $this->crud_model->fetch_user_by_email($email);
              if(count($duplicates) > 0)
              {
                 return false;
              }
              else
              {
                return true;
              }
            }
        }

最终代码与此类似,我很抱歉,我无法为您提供准确的代码,但我认为您可以使用此方法。

  

我建议你转向一些好的框架,比如symfony,laravel或者   Zend公司。从早期的PHP开始,Codeigniter并没有太大的改进   做了很多。