PHP MySQL - 在 PHP 中验证电子邮件
PHP MySQL - Validate email in PHP
目前我正在执行检查功能以了解电子邮件是否已存在于数据库中。如果存在则错误,如果不存在则更新数据。但是,如果现有用户编辑他们的数据,例如 weight
。然后它会出错,因为它检测到电子邮件已经存在。但是用户只编辑 weight
数据,而不是 email
。任何人都可以帮助我如何解决这个问题或有任何建议来完成这一部分。我正在使用 CodeIgniter 框架。
这是我的控制器
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);
}
这是我的模型(验证电子邮件功能和更新数据)
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);
}
````````````````
您必须 user_id 有电子邮件。
试试这个。
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
}
}
目前我正在执行检查功能以了解电子邮件是否已存在于数据库中。如果存在则错误,如果不存在则更新数据。但是,如果现有用户编辑他们的数据,例如 weight
。然后它会出错,因为它检测到电子邮件已经存在。但是用户只编辑 weight
数据,而不是 email
。任何人都可以帮助我如何解决这个问题或有任何建议来完成这一部分。我正在使用 CodeIgniter 框架。
这是我的控制器
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);
}
这是我的模型(验证电子邮件功能和更新数据)
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);
}
````````````````
您必须 user_id 有电子邮件。
试试这个。
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
}
}