2013-03-14 38 views
-3

我正在使用一個註冊系統的網站。在userCP中,您可以更改密碼。我爲它製作了這個腳本,但它不起作用。有人能幫助我嗎?當我更改密碼時,它不會給出錯誤,但它不會更新它。PHP更新密碼腳本不起作用

PHP代碼:

<?php 
    if (isset($_POST['updatePassBtn'])) 
    { 
     $cpassword = $_POST['cpassword']; 
     $npassword = $_POST['npassword']; 
     $rpassword = $_POST['rpassword']; 
     if (!empty($cpassword) && !empty($npassword) && !empty($rpassword)) 
     { 
      if ($npassword == $rpassword) 
      { 
       if (mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '".$_SESSION['username']."' AND `password` = '".SHA1($cpassword)."'"))) 
       { 
        mysql_query("UPDATE `users` SET `password` = '".SHA1($npassword)."' WHERE `username` = '".$_SESSION['username']."' AND `ID` = '".$_SESSION['id']."'"); 
        echo '<div class="nNote nSuccess hideit"><p><strong style="color:green;">SUCCESS: </strong>Password Has Been Updated</p></div>'; 
       } 
       else 
       { 
        echo '<div class="nNote nFailure hideit"><p><strong style="color:red;">FAILURE: </strong>Current Password is incorrect.</p></div>'; 
       } 
      } 
      else 
      { 
       echo '<div class="nNote nFailure hideit"><p><strong style="color:red;">FAILURE: </strong>New Passwords Did Not Match.</p></div>'; 
      } 
     } 
     else 
     { 
      echo '<div class="nNote nFailure hideit"><p><strong style="color:red;">FAILURE: </strong>Please fill in all fields</p></div>'; 
     } 
    } 
?> 

我的形式:

<form action="" class="form" method="POST"> 
    <fieldset> 
     <label></label> 
     <input name="cpassword" type="text" value="Current Password" onfocus="this.value = (this.value=='Current Password')? '' : this.value;" onblur="if(this.value == ''){this.value='Current Password';}"/> 
    </fieldset> 
    <fieldset> 
     <label></label> 
     <input name="npassword" type="text" value="New Password" onfocus="this.value = (this.value=='New Password')? '' : this.value;" onblur="if(this.value == ''){this.value='New Password';}"/> 
    </fieldset> 
    <fieldset> 
     <label></label> 
     <input name="rpassword" type="text" value="Repeat Password" onfocus="this.value = (this.value=='Repeat Password')? '' : this.value;" onblur="if(this.value == ''){this.value='Repeat Password';}"/> 
     <input type="submit" value="Update" name="updatePassBtn"/> 
    </fieldset> 
</form> 
+0

也許是JavaScript標記?沒有看到任何PHP或SQL。啊,更新後好一點。 – ficuscr 2013-03-14 18:46:49

+1

**它輸出的是什麼?**你有一個'echo'遍佈整個地方,所以它必須輸出* something *。 – 2013-03-14 18:47:44

+0

它只是輸出,它是成功的,所以我猜問題是在SQL中。 – wouterdz 2013-03-14 18:48:56

回答

2

你數行數它們與用戶名和密碼相匹配,但是當你更新時你也有條件必須匹配$ _SESSION ['id']。如果你的會話不包含正確的'id',那麼你的更新可能不匹配任何行。

在報告更新成功之前,您應該檢查mysql_affected_rows()

您還應該檢查mysql函數是否返回成功(如@RocketHazmat在註釋中所示)。錯誤時,許多人返回false

+0

也許增加,這個代碼是可怕的廢棄可能會受益OP :) – christopher 2013-03-14 18:54:32

+0

我刪除了ID'檢查',它現在的作品!謝謝:) – wouterdz 2013-03-14 19:00:09

+0

@ChrisCooney,這是真的,mysql_ *函數已被棄用,但許多人仍然必須維護一個現有的應用程序與舊功能編碼。我也可以評論變量到SQL字符串中的危險插值,但這不是問題的要點。 – 2013-03-14 19:24:21

-1
mysql_query("UPDATE `users` SET `password` = '".SHA1($npassword)."' WHERE `username` = '".$_SESSION['username']."' AND `ID` = '".$_SESSION['id']."'"); 

您已經請求mysql_query成立,但我沒有看到你執行它

+3

['mysql_query()'](http://php.net/manual/en/function.mysql-query.php)執行查詢。它沒有像MySQLi/PDO那樣的準備好的語句。 – 2013-03-14 18:53:01

+0

它的確如此,因爲我沒有使用變量和mysql_query()執行它 – wouterdz 2013-03-14 18:55:28