2016-03-29 101 views
-2

我正在嘗試創建edit.php,以便用戶可以更改密碼並更改他們的信息,如電子郵件,用戶名。更新帳戶信息PDO/PHP

用戶名是會話用戶登錄,所以如果用戶名更改,我需要更新會話與新的用戶名。它不起作用。我不知道爲什麼。 謝謝你的幫助。這是代碼edit.php

<?php 
require('includes/config.php'); 
if(!$user->is_logged_in()){ header('Location: login.php'); } 
?> 
<?php 
class info {  
} 
$username= $_SESSION['username']; 
$sql = 'SELECT * FROM users WHERE username = :username'; 
$query= $db->prepare($sql); 
$query->execute(array(

      ':username' => $username, 
)); 
$query->setFetchMode (PDO::FETCH_CLASS, 'info'); 
while($r= $query->fetch(PDO::FETCH_OBJ)) { 
    $email = $r->email; 
    $namefull = $r->namefull; 
    $usertype = $r->usertype; 
    $password = $r->password; 
    } 
if(isset($_POST['submit'])){ 
    if(strlen($_POST['username']) < 3){ 
     $error[] = 'Username deve avere almeno 4 lettere.'; 
    } else { 
     $stmt = $db->prepare('SELECT username FROM users WHERE username = :username'); 
     $stmt->execute(array(':username' => $_POST['username'])); 
     $row = $stmt->fetch(PDO::FETCH_ASSOC); 
     if(!empty($row['username'])){ 
      $error[] = 'Username già in uso.'; 
     } 
    } 
    if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ 
     $error[] = 'Inserisci una email valida'; 
    } else { 
     $stmt = $db->prepare('SELECT email FROM users WHERE email = :email'); 
     $stmt->execute(array(':email' => $_POST['email'])); 
     $row = $stmt->fetch(PDO::FETCH_ASSOC); 
     if(!empty($row['email'])){ 
      $error[] = 'Email già in uso.'; 
     } 
    } 
    if(!isset($error)){ 
     try { 
      $stmt = $db->prepare("UPDATE users SET (username,email,usertype) VALUES (:username, :email,:usertype) WHERE username='".$username."'"); 
      $stmt->execute(array(
       ':username' => $_POST['username'], 
       ':email' => $_POST['email'], 
       ':usertype' => $_POST['usertype'], 
      )); 
      header('Location: edit.php?action=joined'); 
      exit; 
     } catch(PDOException $e) { 
      $error[] = $e->getMessage(); 
     } 

     } 
} 
?> 

這對於重置密碼的操作形式,我創建編輯reset.php

<?php 
if(isset($_POST['submit'])){ 
    if(strlen($_POST['password']) < 3){ 
     $error[] = 'Password deve contenere almeno 4 lettere.'; 
    } 

    if(strlen($_POST['passwordConfirm']) < 3){ 
     $error[] = 'Conferma password deve contenere almeno 4 lettere.'; 
    } 

    if($_POST['password'] != $_POST['passwordConfirm']){ 
     $error[] = 'Le password non sono uguali.'; 
    } 


    //if no errors have been created carry on 
    if(!isset($error)){ 

     //hash the password 
     $hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT); 

     //create the activasion code 
     $activasion = md5(uniqid(rand(),true)); 

     try { 

      //insert into database with a prepared statement 
      $stmt = $db->prepare('INSERT INTO users (username,password,email,active) VALUES (:username, :password, :email, :active)'); 
      $stmt->execute(array(
       ':username' => $_POST['username'], 
       ':password' => $hashedpassword, 
       ':email' => $_POST['email'], 
       ':active' => $activasion 
      )); 
      $id = $db->lastInsertId('id'); 

      //send email 
      $to = $_POST['email']; 
      $subject = "Cambio password"; 
      $body = "<p>La tua password è stata cambiata!</p> 
      <p>Amministrazione Dixard</p>"; 

      $mail = new Mail(); 
      $mail->setFrom(SITEEMAIL); 
      $mail->addAddress($to); 
      $mail->subject($subject); 
      $mail->body($body); 
      $mail->send(); 

      //redirect to index page 
      header('Location: register.php?action=joined'); 
      exit; 

     //else catch the exception and show the error. 
     } catch(PDOException $e) { 
      $error[] = $e->getMessage(); 
     } 

     //aggiornare la sessione con la nuova password 




    }  

} 

?> 

這裏是兩種形式(更改電子郵件,用戶名,用戶類型和更改密碼):

用戶名 「> Tipologia佔 「> 郵箱地址 」> 薩爾瓦
   <h2 class="strong-header large-header">Change Password</h2> 
       <form role="form" action="edit-reset.php" method="post" novalidate> 
        <div class="form-group"> 
         <label for="password">Password corrente</label> 
         <input type="password" placeholder="Password" name="password_corrent" id="password" class="form-control" required> 
        </div> 

        <div class="form-group"> 
         <label for="password">Nuova Password</label> 
         <input type="password" placeholder="Nuova Password" name="password" id="password" class="form-control" required> 
        </div> 
        <div class="form-group"> 
         <label for="password-repeat">Conferma Nuova password</label> 
         <input type="password" name="passwordConfirm" id="passwordConfirm"class="form-control" placeholder="Conferma Nuova Password" required> 
        </div> 

        <button type="submit" class="btn btn-primary">Cambia Password</button> 
       </form> 

回答

0

當您在edit.php腳本要檢查如果用戶名或電子郵件已被使用,你不排除在該id當前用戶。因此,每次他想更改其中的一個時,用戶將不得不更改他的用戶名電子郵件。

此外,在更新數據庫中的數據以保持用戶登錄後(假設用戶名已被修改),您不會更新$_SESSION['username']

+0

是的,我需要更新會話與新的用戶名數據。但問題是用戶名和電子郵件不會改變。數據庫數據不變 \t \t $ stmt = $ db-> prepare('SELECT username FROM users WHERE username =:username \t \t AND NOT EXISTS(SELECT username FROM users WHERE id = $ id)'); (數組(':username'=> $ _POST ['username']));這個數組是一個數組,這個數組是一個數組。 \t \t $ row = $ stmt-> fetch(PDO :: FETCH_ASSOC); (!空($行[ '用戶名')) \t \t如果{ \t \t \t $錯誤[] = '用戶名存在。'; \t \t} \t} –

+0

1.我認爲您的更新請求存在問題;你需要看看這個:http://dev.mysql.com/doc/refman/5.7/en/update.html。你的請求應該更像這樣:'UPDATE users SET username =:username,email =:email,usertype =:usertype WHERE id =:id;' 2.在你以前的驗證中,用一個簡單的請求'SELECT id FROM users WHERE username =:username;'並且在你檢查用戶名是否已經被使用的時候在你的條件中添加這個id:'SELECT username FROM users WHERE username =:username AND id!=:id; – Schnapse