2

我将 User Cake 用于用户管理系统,但我正在努力解决一个问题,我曾在他们的网站上问过这个问题,但我找不到任何人来帮助我。

我需要的只是让用户能够更新他们的信息。前任。名字,电话,电子邮件......电子邮件字段随着该功能的出现而正确更新。

我添加的字段没有更新。有人可以给我一些关于我缺少什么的提示吗?

这是我尝试查看电子邮件字段的内容。我有名字字段。

函数.php

   //Update a user's email
  function updateEmail($id, $email)
  {
global $mysqli,$db_table_prefix;
$stmt = $mysqli->prepare("UPDATE ".$db_table_prefix."users
    SET 
    email = ?
    WHERE
    id = ?");
$stmt->bind_param("si", $email, $id);
$result = $stmt->execute();
$stmt->close(); 
return $result;
    }


   //Update a user's first name. This is what isn't working. 
   function updateFirstname($id, $firstname)
   {
global $mysqli,$db_table_prefix;
$stmt = $mysqli->prepare("UPDATE ".$db_table_prefix."users
    SET 
    firstname = ?
    WHERE
    id = ?");
$stmt->bind_param("si", $firstname, $id);
$result = $stmt->execute();
$stmt->close(); 
return $result;
    }

这是class.user.php

 class loggedInUser {
public $email = NULL;
public $hash_pw = NULL;
public $user_id = NULL;
public $firstname = NULL;


   //Update a users email
public function updateEmail($email)
{
    global $mysqli,$db_table_prefix;
    $this->email = $email;
    $stmt = $mysqli->prepare("UPDATE ".$db_table_prefix."users
        SET 
        email = ?
        WHERE
        id = ?");
    $stmt->bind_param("si", $email, $this->user_id);
    $stmt->execute();
    $stmt->close(); 
}

//Update a users first name
public function updateFirstname($firstname)
{
    global $mysqli,$db_table_prefix;
    $this->firstname = $firstname;
    $stmt = $mysqli->prepare("UPDATE ".$db_table_prefix."users
        SET 
        firstname = ?
        WHERE
        id = ?");
    $stmt->bind_param("si", $firstname, $this->user_id);
    $stmt->execute();
    $stmt->close(); 
}
    }

user_settings.php 我可以在其中更改字段并点击更新按钮。如果我更改电子邮件并点击更新,则电子邮件会更新,但是当我更改名字并点击更新时,我会得到

nothing to update

    //Prevent the user visiting the logged in page if he is not logged in
    if(!isUserLoggedIn()) { header("Location: login.php"); die(); }

   if(!empty($_POST))
    {
$errors = array();
$successes = array();
$password = $_POST["password"];
$password_new = $_POST["passwordc"];
$password_confirm = $_POST["passwordcheck"];

$errors = array();
$email = $_POST["email"];
$firstname = $_POST["firstname"];


//Perform some validation
//Feel free to edit / change as required

//Confirm the hashes match before updating a users password
$entered_pass = generateHash($password,$loggedInUser->hash_pw);

if (trim($password) == ""){
    $errors[] = lang("ACCOUNT_SPECIFY_PASSWORD");
}
else if($entered_pass != $loggedInUser->hash_pw)
{
    //No match
    $errors[] = lang("ACCOUNT_PASSWORD_INVALID");
}   
if($email != $loggedInUser->email)
{
    if(trim($email) == "")
    {
        $errors[] = lang("ACCOUNT_SPECIFY_EMAIL");
    }
    else if(!isValidEmail($email))
    {
        $errors[] = lang("ACCOUNT_INVALID_EMAIL");
    }
    else if(emailExists($email))
    {
        $errors[] = lang("ACCOUNT_EMAIL_IN_USE", array($email));    
    }

    //End data validation
    if(count($errors) == 0)
    {
        $loggedInUser->updateEmail($email);
        $loggedInUser->updateFirstname($firstname);
        $successes[] = lang("ACCOUNT_EMAIL_UPDATED");
    }
}

if ($password_new != "" OR $password_confirm != "")
{
    if(trim($password_new) == "")
    {
        $errors[] = lang("ACCOUNT_SPECIFY_NEW_PASSWORD");
    }
    else if(trim($password_confirm) == "")
    {
        $errors[] = lang("ACCOUNT_SPECIFY_CONFIRM_PASSWORD");
    }
    else if(minMaxRange(8,50,$password_new))
    {   
        $errors[] = lang("ACCOUNT_NEW_PASSWORD_LENGTH",array(8,50));
    }
    else if($password_new != $password_confirm)
    {
        $errors[] = lang("ACCOUNT_PASS_MISMATCH");
    }

    //End data validation
    if(count($errors) == 0)
    {
        //Also prevent updating if someone attempts to update with the same password
        $entered_pass_new = generateHash($password_new,$loggedInUser->hash_pw);

        if($entered_pass_new == $loggedInUser->hash_pw)
        {
            //Don't update, this fool is trying to update with the same password ¬¬
            $errors[] = lang("ACCOUNT_PASSWORD_NOTHING_TO_UPDATE");
        }
        else
        {
            //This function will create the new hash and update the hash_pw property.
            $loggedInUser->updatePassword($password_new);
            $successes[] = lang("ACCOUNT_PASSWORD_UPDATED");
        }
    }
}
if(count($errors) == 0 AND count($successes) == 0){
    $errors[] = lang("NOTHING_TO_UPDATE");
}
    }
4

1 回答 1

0
if($email != $loggedInUser->email)
{
    if(trim($email) == "")
    {
        $errors[] = lang("ACCOUNT_SPECIFY_EMAIL");
    }
    else if(!isValidEmail($email))
    {
        $errors[] = lang("ACCOUNT_INVALID_EMAIL");
    }
    else if(emailExists($email))
    {
        $errors[] = lang("ACCOUNT_EMAIL_IN_USE", array($email));    
    }

    //End data validation
    if(count($errors) == 0)
    {
        $loggedInUser->updateEmail($email);
        $successes[] = lang("ACCOUNT_EMAIL_UPDATED");
    }
}

将此函数克隆为

if($firstname != $loggedInUser->firstname) blah blah

从上面的函数中删除这一行,将其移到新函数中:

loggedInUser->updateFirstname($firstname);

只需克隆函数,就像您在上面所做的那样。更改错误消息并添加函数来验证名称,它会有所不同,需要更多的工作。

于 2013-09-01T20:33:43.187 回答