2012-12-17 86 views
1

即使更新查詢失敗,仍然顯示成功,我無法獲得最後2個迴應。如果任何人有任何關於此代碼的任何建議,以改善任何行,請做!顯示錯誤消息PHP Mysql

<?php 
     if(!empty($_POST['username']) && !empty($_POST['answer'])) { 
      $username = $_POST['username']; 
      $idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'") //check it 
      or die(mysql_error()); 
      $fetched = mysql_fetch_array($idfetch); 
      $id = $fetched['id']; //get users id for checking 
      $answer = $_POST['answer']; 
      $password = (mysql_real_escape_string($_POST['password'])); 
      $confpass = (mysql_real_escape_string($_POST['confpass'])); 
      if ($password != $confpass) { 
       echo ("Passwords do not match, please try again."); 
       exit; 
      } 
      $updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'"); 
      if($updatequery) { 
       echo "<h1>Success</h1>"; 
       echo "<p>Your account password was successfully changed. Please <a href=\"login.php\">click here to login</a>.</p>"; 
      } 
      else { 
       echo "<h1>Error</h1>"; 
       echo "<p>Sorry, but a field was incorrect.</p>"; 
      } 
     } 
?> 

在此先感謝!

+0

請不要在新的應用程序中使用'mysql_query'。如果不使用*完全*這是非常危險的,這是一個巨大的麻煩,儘管我已經看到你至少在這裏嘗試。不過,你逃脫了三個變量中的兩個並引入了一個巨大的注入漏洞。公共互聯網上足夠接近並不夠好。至少你應該使用[PDO](http://bobby-tables.com/php),除非你有一個很好的理由,因爲當使用SQL佔位符時,這些錯誤通常是不存在的。 – tadman

+0

感謝您的評論,我很欣賞它,我會研究這一點。我沒有看到任何東西,但我的研究生病了! – sparkones

+0

感謝大家對我所感謝的所有評論。 – sparkones

回答

2
mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'") or die(mysql_error()."update failed"); 

,並使用

mysql_affected_rows() 

Returns the number of affected rows on success, and -1 if the last query failed. 
0

使用

if(mysql_num_rows($updatequery) > 0) { 
    // success 
} else { 
    // error 
} 

$updatequery永遠是真(非NULL),直到有您的查詢錯誤

+1

不會** [mysql_affected_rows()](http://php.net/manual/en/function.mysql-affected-rows.php)**是正確的功能嗎? –

+0

@DamienPirsy是正確的。從手冊:['mysql_affected_rows()'](http://php.net/manual/en/function.mysql-affected-rows.php) - 獲取最後一次INSERT,UPDATE,REPLACE或DELETE查詢。 ['mysql_num_rows()'](http://php.net/manual/en/function.mysql-num-rows.php) - 檢索結果集中的行數。該命令僅對像SELECT或SHOW這樣的返回實際結果集的語句有效。要檢索受INSERT,UPDATE,REPLACE或DELETE查詢影響的行數,請使用mysql_affected_rows()。 – Sean

0

使用或模具(mysql_error()),因爲它會顯示MySQL錯誤如果您的查詢出現錯誤。

$updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'") or die(mysql_error());

1

使用嘗試捕捉,並試圖讓錯誤啓用錯誤在PHP報告還

<?php 
     error_reporting(E_ALL); 
     ini_set('display_errors','On'); 
     if(!empty($_POST['username']) && !empty($_POST['answer'])) { 
     $username = $_POST['username']; 
     $idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'") //check it 
     or die(mysql_error()); 
     $fetched = mysql_fetch_array($idfetch); 
     $id = $fetched['id']; //get users id for checking 
     $answer = $_POST['answer']; 
     $password = (mysql_real_escape_string($_POST['password'])); 
     $confpass = (mysql_real_escape_string($_POST['confpass'])); 
     if ($password != $confpass) { 
     echo ("Passwords do not match, please try again."); 
     exit;} 

     try{ 
     $updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'"); 
     if($updatequery) { 
     echo "<h1>Success</h1>"; 
     echo "<p>Your account password was successfully changed. Please <a href=\"login.php\">click here to login</a>.</p>"; } 
     else { 
     echo "<h1>Error</h1>"; 
     echo "<p>Sorry, but a field was incorrect.</p>"; 
     } 

     }catch(Exception $e){ 
      print_R($e); 
     } 
     } 
0

試試這個:

$idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'"); 
if(!idfetch){ 
    die(mysql_error()); 
} 

執行相同的所有其他查詢過。

0

嘗試此操作,首先將行計數值計數爲1然後繼續登錄過程。

<?php 
    if(!empty($_POST['username']) && !empty($_POST['answer'])) { 
     $username = $_POST['username']; 
     $idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'") //check it 
     or die(mysql_error()); 
     $fetched = mysql_fetch_array($idfetch); 

     $count= mysql_num_rows($idfetch); 

     if($count>0){ 
     $id = $fetched['id']; //get users id for checking 
     $answer = $_POST['answer']; 
     $password = (mysql_real_escape_string($_POST['password'])); 
     $confpass = (mysql_real_escape_string($_POST['confpass'])); 
     if ($password != $confpass) { 
      echo ("Passwords do not match, please try again."); 
      exit; 
     } 

     $updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'"); 

      if($updatequery) { 
      echo "<h1>Success</h1>"; 
      echo "<p>Your account password was successfully changed. Please <a href=\"login.php\">click here to login</a>.</p>"; 
     } 
      else { 
      echo "<h1>Error</h1>"; 
      echo "<p>Sorry, but a field was incorrect.</p>"; 
      } 
    } } ?>