0

currently I do a checking function to know email already exist or not in db. If exist then error, if not update data. But if existing user edit their data such as weight. Then it will give error because it detect email already exist. But the user only edit the weight data, not the email. can anyone help me how to solve this problem or have any recommendation to do this part. I'm using a CodeIgniter framework.

This is my controller

if (!empty($this->input->post()))
        {
            $data["weight"] = $this->input->post("weight");
            $data["height"] = $this->input->post("height");
            $data["username"] = $this->input->post("username");
            $data["email"] = $this->input->post("email");
            
            if (strlen($this->input->post("username")) < 6)
            {
                $result = $this->Global_model->GenerateOutputMsgArray("0", "Username should be at least 6 alphanumerics, please try again.");
            }
            elseif (!$this->Profile_model->ValidateEmail($this->input->post()))
            {
                $result = $this->Global_model->GenerateOutputMsgArray("0", "Email has been taken, please try another.");    
            } else {
                $result["status"] == "1";
                
                $this->Profile_model->UpdateProfile($data["weight"], $data["height"], $data["username"], $data["email"]);
                
                $result = $this->Global_model->GenerateOutputMsgArray("1", "Your profile has been updated.", $data);
            }

this is my model (validate email function and update data)

public function ValidateEmail($post) 
    {
        $stat = "0";
        $msg = "";
        $data = array();
        $output = array();
        $query = $this->db->get_where("user", array("email" => $post["email"]));
        $result = $query->row();
        $result = (array)($result);
        
        return ($result) ? false : true;
    } 


    function UpdateProfile($weight, $height,$username, $email)
    {
    $data= array(
            "weight" => $weight,
            "height" => $height,
            "username" => $username,
            "email" => $email
        );
        
        $this->db->where("user_id", $this->session->userdata("user_id"));
        $this->db->update("user", $data);
    }
````````````````
2
  • please read INSERT ON DUPLICATE KEY UPDATE, this is fundamental knowledge! Commented Feb 22, 2021 at 14:23
  • @Vickel noted, thanks. i'll read this :) Commented Feb 23, 2021 at 3:00

1 Answer 1

2

You must be having user_id with email.

Try with this one.

public function ValidateEmail($post) 
{
    $stat = "0";
    $msg = "";
    $data = array();
    $output = array();
    $query = $this->db->get_where("user", array("email" => $post["email"]));
    if($query->num_rows() > 0){
        $result = $query->row();
        if($result->user_id == $this->session->userdata("user_id")){
            return false; // force doesn't exist
        }else{
            return true; // exists
        }
    }else{
        return false; // as there is no row returned. it will return as doesn't exist
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.