2016-05-16 89 views
1

我對某些主題的打印有問題。這個問題是我無法更新我的用戶信息的原因。雖然這個代碼是我認爲正確的。無法使用php打印支票

這是我在editprofile頁面上的代碼。

include_once ("classes/Db.class.php"); 
include_once ("classes/config.class.php"); 
include_once ("classes/user.class.php"); 

session_start(); 
if (!isset($_SESSION['loggedIn'])) { 
    echo("not set"); 
    header("Location:index.php"); 
} 
if (!empty($_POST['update'])) { 
    echo "test 2"; 
    // todo: 1 form input velden ophalen 
    try { 
     $u = new User(); 
     $u->Username = $_POST['form-username']; 
     $u->Email = $_POST['form-email']; 
     $u->Password = $_POST['form-password']; 
     $u->Passwordconfirmation = $_POST['form-passwordconf']; 
     $u->Update($_SESSION['loggedIn']); 
     $u->profileImg($_SESSION['loggedIn']); 
     $succes = "Je gegevens zijn aangepast"; 
    } catch (exception $e) { 
     $succes = $e->getMessage(); 
    } 
} 

這裏是我的user.class.php中的代碼。我想與之交談的具體功能是更新功能。

<?php 
/** 
* Created by PhpStorm. 
* User: erhanlammar 
* Date: 23/04/16 
* Time: 10:13 
*/ 

include_once("Db.class.php"); 

class User{ 

// todo: 1 private variabelen aanmaken voor firstname, lastname, ... 
private $_db; 
private $m_sUsername; 
private $m_sFirstname; 
private $m_sLastname; 
private $m_sEmail; 
private $m_sPassword; 
private $m_sPasswordconfirmation; 

private $m_sProfileimage; 
//private $m_susersid; 

// todo: 2 getters & setters! 

public function __set($p_sProperty, $p_vValue){ 
    switch($p_sProperty){ 
     case "Username": 
      if(!empty($p_vValue)){ 
       $this->m_sUsername = $p_vValue; 
       break; 
      }else{ 
       //opvangen van leeg veld username. 
       throw new exception("vergeet geen username in te vullen"); 
      } 
     case "Firstname": 
      if(!empty($p_vValue)){ 
       $this->m_sFirstname = $p_vValue; 
       break; 
      }else{ 
       //opvangen van leeg veld firstname. 
       throw new exception("Uw voornaam hebben we echt wel nodig"); 
      } 
     case "Lastname": 
      if(!empty($p_vValue)){ 
       $this->m_sLastname = $p_vValue; 
       break; 
      }else{ 
       //opvangen van leeg veld lastname. 
       throw new exception("Heeft u geen achternaam?"); 
      } 
     case "Email": 
      if(!empty($p_vValue)){ 
       $this->m_sEmail = $p_vValue; 
       break; 
      }else{ 
       //opvangen van leeg veld email. 
       throw new exception("Wij hebben uw email nodig om u op de hoogte te houden"); 
      } 
     case "Password": 
      if(!empty($p_vValue)){ 
       $this->m_sPassword = $p_vValue; 
       break; 
      }else{ 
       //opvangen van leeg veld firstname. 
       throw new exception("Zonder wachtwoord geen login"); 
      } 
     case "Passwordconfirmation": 
      if(!empty($p_vValue)){ 
       $this->m_sPasswordconfirmation = $p_vValue; 
       break; 
      }else{ 
       //opvangen van leeg veld firstname. 
       throw new exception("Zonder wachtwoord geen login"); 
      } 
     case "Profileimage": 
       $this->m_sProfileimage = $p_vValue; 
       break; 
    } 
} 
public function __get($p_sProperty){ 
    switch($p_sProperty){ 
     case "Username": 
      return $this->m_sUsername; 
      break; 
     case "Firstname": 
      return $this->m_sFirstname; 
      break; 
     case "Lastname": 
      return $this->m_sLastname; 
      break; 
     case "Email": 
      return $this->m_sEmail; 
      break; 
     case "Password": 
      return $this->m_sPassword; 
      break; 
     case "Passwordconfirmation": 
      return $this->m_sPasswordconfirmation; 
      break; 
     case "Profileimage": 
      return $this->m_sProfileimage; 
      break; 
     } 
} 

private function checkPasswordConfirmation(){ 
    if($this->m_sPassword == $this->m_sPasswordconfirmation){ 
     return true; 
    }else{ 
     throw new exception("wachtwoorden komen niet overeen"); 
    } 
} 


public function signup(){ 
    if(!$this->checkEmail()){ 
     throw new exception("Dit emailadres bestaat al neem een ander of ga naar login"); 
    } 
    if(!$this->checkUsername()){ 
     throw new exception("De username die u gekozen heeft bestaat al!!"); 
    } 
    if(!$this->checkPasswordConfirmation()){ 
     throw new exception("De registratie is niet correct verlopen. Check alles nog eens"); 
    } 
    $conn = new PDO("mysql:host=localhost;dbname=IMDstagram", "root",""); 
    $options= ['cost' => 12]; 
    $this->m_sPassword = password_hash($this->m_sPassword, PASSWORD_DEFAULT, $options); 
    $statement = $conn->prepare("INSERT INTO users(
     username, 
     firstname, 
     lastname, 
     email, 
     password 
    ) 
     VALUES(
     :username, 
     :firstname, 
     :lastname, 
     :email, 
     :password 
    ) 
     "); 
    $statement->bindValue(":username", $this->m_sUsername); 
    $statement->bindValue(":firstname", $this->m_sFirstname); 
    $statement->bindValue(":lastname", $this->m_sLastname); 
    $statement->bindValue(":email", $this->m_sEmail); 
    $statement->bindValue(":password", $this->m_sPassword); 
    return $statement->execute(); 

} 

public function checkEmail(){ 

    $PDO = Db::getInstance(); 
    $stmt = $PDO->prepare("SELECT * FROM users WHERE email= :email"); 
    $stmt->bindValue(":email", $this->m_sEmail, PDO::PARAM_STR); 
    $stmt->execute(); 

    if($stmt->rowCount() > 0){ 
     return false; 
     throw new exception("") ; 
    } 
    else{ 

     return true; 

    } 
} 
public function checkUsername(){ 

    $PDO = Db::getInstance(); 
    $stmt = $PDO->prepare("SELECT * FROM users WHERE username= :username"); 
    $stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR); 
    $stmt->execute(); 

    if($stmt->rowCount() > 0){ 
     return false; 
     throw new exception("") ; 
    } 
    else{ 
     return true; 

    } 
} 

public function loggingIn(){ 
    if(!empty($this->m_sUsername) && !empty($this->m_sPassword)){ 
     $PDO = Db::getInstance(); 
     $stmt = $PDO->prepare("SELECT * FROM users WHERE username = :username"); 
     $stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR); 
     $stmt->execute(); 

     if($stmt->rowCount() > 0){ 
      $result = $stmt->fetch(PDO::FETCH_ASSOC); 
      $password = $this->m_sPassword; 
      $hash = $result['password']; 

      if(password_verify($password, $hash)){ 
       session_start(); 
       $_SESSION["loggedIn"] = $result['usersid']; 
       $_SESSION["loggedIn"] = $result ['username']; 
       session_write_close(); 
       return true; 
      }else{ 
       return false; 
      } 
     } 
    } 
} 

public function Update($userid){ 

    $PDO = Db::getInstance(); 

    if(!empty($this->m_sUsername)){ 

     $stmt = $PDO->prepare("UPDATE users SET username= :username WHERE usersid = :usersid"); 
     $stmt->bindValue(":usersid", $userid, PDO::PARAM_INT);//update velden velden met where m_sUserid = Userid 
     $stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR); 
     $stmt->execute(); 
     echo("username"); 

    } 

    if (!empty($this->m_sEmail)){ 

     if(!$this->checkEmail()){ 
      throw new exception("De update is niet correct verlopen. Check alles nog eens"); 
     } 

     $stmt = $PDO->prepare("UPDATE users SET email= :email WHERE usersid = :usersid"); 
     $stmt->bindValue(":usersid", $userid, PDO::PARAM_INT);//update username met " " " 
     $stmt->bindValue(":email", $this->m_sEmail, PDO::PARAM_STR); //aleen email 
     $stmt->execute(); 
     echo("email"); 

    } 
    if (!empty($this->m_sPassword)){ 

     if(!$this->checkPasswordConfirmation()){ 
      throw new exception("de update lukt niet, passwoorden komen niet overeen."); 
     } 

     $stmt = $PDO->prepare("UPDATE users Set password = :password WHERE usersid = :usersid"); 
     $stmt->bindValue(":usersid", $userid, PDO::PARAM_INT);//update password met " " " 
     $stmt->bindValue(":password", $this->m_sPassword, PDO::PARAM_STR); //aleen u password 
     $stmt->execute(); 

    } 
} 
} 
+0

1)檢查下列實例屬性'$ m_sUsername','$ m_sEmail'和'$ m_sPassword'是否存在並設置與否。你已經設置了不同的實例屬性'$ u-> Username = ...','$ u-> Email = ...'等等。2)重構你的* update()*方法,而不是三個* if只有一個* if *塊來更新所有三個字段。 3)不要將密碼存儲爲簡單易讀的文本,在插入前務必使用[salted password hashing](https://crackstation.net/hashing-security.htm)。 –

+0

他們都設置和存在。密碼也被散列。 –

+0

你在哪裏設置了所有這些實例屬性,「$ m_sUsername」,「$ m_sEmail」和「$ m_sPassword」,以及你在哪裏哈希密碼?我沒有在任何地方看到*代碼*。 –

回答

0

不是更新分別在三個ifusernameemailpassword字段,僅使用一個if塊來更新所有三個字段。

所以你update()方法應該是這樣的:

public function Update($userid){ 

    $PDO = Db::getInstance(); 
    if(!empty($this->m_sUsername) && !empty($this->m_sEmail) && !empty($this->m_sPassword) && !empty($this->m_sPasswordconfirmation)){ 
     if($this->m_sPassword == $this->m_sPasswordconfirmation){ 
      $options= ['cost' => 12]; 
      $this->m_sPassword = password_hash($this->m_sPassword, PASSWORD_DEFAULT, $options); 

      $stmt = $PDO->prepare("UPDATE users SET username= :username, email = :email, password = :password WHERE usersid = :usersid"); 
      $stmt->bindValue(":usersid", $userid, PDO::PARAM_INT); 
      $stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR); 
      $stmt->bindValue(":email", $this->m_sEmail, PDO::PARAM_STR); 
      $stmt->bindValue(":password", $this->m_sPassword, PDO::PARAM_STR); 
      if($stmt->execute()){ 
       // success 
       echo "succes"; 
      }else{ 
       // error 
       echo "failure"; 
      } 
     }else{ 
      // Mismatch password 
      echo "mismatch password"; 
     } 
    }else{ 
     // some values are not set 
     echo "some values are not set"; 
    } 
} 
+0

謝謝,該函數的工作原理是現在的更新不會發生在我的數據庫 –

+0

@Lammar_E檢查「$ userid'在update()方法內的狀態。在'update()'方法內部'echo $ userid;'看看你得到了什麼。 –

+0

當我在userid上進行回顯時,在頁面上回顯該用戶名的用戶名我得到了這個輸出=> Jacky(這是用戶標識符)je aanpassingen zijn doorgevoerd –