【发布时间】:2021-02-22 12:26:04
【问题描述】:
目前我做一个检查功能来知道电子邮件已经存在或不存在于数据库中。如果存在则错误,如果不更新数据。但是如果现有用户编辑他们的数据,例如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);
}
````````````````
【问题讨论】:
-
请阅读INSERT ON DUPLICATE KEY UPDATE,这是基础知识!
-
@Vickel 指出,谢谢。我会读这个:)
标签: php mysql codeigniter