Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem lies here - </p> <pre><code>if(!$result) </code></pre> <p>When a user enters a wrong username, the query searches for that user in the database, but won't find one. So the result will contain empty dataset, but the query is still valid, since you can query a database and return empty datasets. So your <code>!$result</code> check will always evaluate to true unless a DB error occurs.</p> <p>Instead of just checking the $result, you should do the following - </p> <pre><code>if($newpassword==$repeatnewpassword) { // User's provided new password and repeatpassword matches, so keep going forward, // query the database. $result = mysql_query("SELECT password FROM $tbl_name WHERE username='$username'"); if($result) { // Database query successful. Now check if that username exists in the database. if(mysql_num_rows($result) &lt;= 0) { // user has provided wrong username, take action accordingly } else { // Username found, now check for old password match $row = mysql_fetch_array($result); if($password==$row['password']) { // User's old password matches with DB. So, update password and // forward him to confirmation page } else { // User's old password doesn't match with db. Show appropriate message } } } else { // Some DB error occurred. Handle it appropriately. } } else { // User's new and repeat password don't match, so take action accordingly } </code></pre> <p><strong>P.S.</strong></p> <p>Your site is vulnerable to <a href="http://en.wikipedia.org/wiki/SQL_Injection" rel="nofollow">SQL Injection</a> attack. You should at least sanitize your input as follows - </p> <pre><code>$username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $newpassword = mysql_real_escape_string($_POST['newpassword']); $repeatnewpassword = mysql_real_escape_string($_POST['repeatnewpassword']); </code></pre> <p>To know more, go here: <a href="http://php.net/manual/en/function.mysql-real-escape-string.php" rel="nofollow">mysql_real_escape_string() manual</a>.</p> <p>Also storing passwords in database in plain old text format is another bad idea. Even you should not be able to see the passwords that your site's user provide. Use <a href="http://php.net/manual/en/function.md5.php" rel="nofollow">md5()</a> function to <a href="http://en.wikipedia.org/wiki/MD5" rel="nofollow">encrypt</a> passwords and then store it in the database.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload