Fscore/objects/Users.php
/**
     * Atualizar um utilizador na base de dados
     * @return boolean
     */
Atualizar um utilizador na base de dados
@return boolean
Atualizar um utilizador na base de dados @return boolean
public function update() {
        $password_set = !empty($this->password) ? ", password = :password" : "";
        $query = "UPDATE " . $this->table_name . "
            SET
                username = :username,
                email = :email,
                photo_user = :photo_user
                {$password_set}
            WHERE id = :id";
        $stmt = $this->conn->prepare($query);
        $this->username = filter_var($this->username,FILTER_SANITIZE_STRING);
        $this->email = filter_var($this->email,FILTER_SANITIZE_EMAIL);
        $this->password = filter_var($this->password,FILTER_SANITIZE_STRING);
        $this->photo_user = filter_var($this->photo_user,FILTER_SANITIZE_STRING);
        $stmt->bindValue(':username', $this->username);
        $stmt->bindValue(':email', $this->email);
        $stmt->bindValue(':photo_user', $this->photo_user);
        if (!empty($this->password)) {
            $password_hash = password_hash($this->password, PASSWORD_DEFAULT);
            $stmt->bindValue(':password', $password_hash);
        }
        $stmt->bindValue(':id', $this->id);
        if ($stmt->execute()) {
            return true;
        }
        return false;
    }
public function update ( ) { $ password_set = ! empty ( $ this -> password ) ? " , password = :password " : " " ; $ query = " UPDATE  " . $ this -> table_name . " 
            SET
                username = :username,
                email = :email,
                photo_user = :photo_user
                 { $ password_set } 
            WHERE id = :id " ; $ stmt = $ this -> conn -> prepare ( $ query ) ; $ this -> username = filter_var ( $ this -> username , FILTER_SANITIZE_STRING ) ; $ this -> email = filter_var ( $ this -> email , FILTER_SANITIZE_EMAIL ) ; $ this -> password = filter_var ( $ this -> password , FILTER_SANITIZE_STRING ) ; $ this -> photo_user = filter_var ( $ this -> photo_user , FILTER_SANITIZE_STRING ) ; $ stmt -> bindValue ( ':username' , $ this -> username ) ; $ stmt -> bindValue ( ':email' , $ this -> email ) ; $ stmt -> bindValue ( ':photo_user' , $ this -> photo_user ) ; if ( ! empty ( $ this -> password ) ) { $ password_hash = password_hash ( $ this -> password , PASSWORD_DEFAULT ) ; $ stmt -> bindValue ( ':password' , $ password_hash ) ; } $ stmt -> bindValue ( ':id' , $ this -> id ) ; if ( $ stmt -> execute ( ) ) { return true ; } return false ; }
Atualizar um utilizador na base de dados
 @return boolean
Atualizar um utilizador na base de dados @return boolean
// if password needs to be updated // if no posted password, do not update the password // prepare the query // sanitize // bind the values from the form // hash the password before saving to database // unique ID of record to be edited // execute the query