sj005 Posted February 21, 2011 Share Posted February 21, 2011 Hey all. I am new to this forum and to PHP as a whole. I though I would try to make a login system using php and mysql. The login and register forms work great but I cannot seem to fully figure out how to let a user change their password. My code partially works. When the user types the correct old password and when the two new password forms confirm, the password changes and the database is updated and the user is taken to a page that tells him that his password was successfully changed. However, the problem is when the old password he types is different than the one in the database, the page that states password successfully changed also appears but the password is not changed in the database. The problem is thus with the SELECT statement. Can anyone please help me find whats wrong. It has been tormenting for a few hours now. Thank you in advance. . I used md5 encryption for the passwords. Here is the section of code that comes after the script makes sure that none of the forms are empty and that the passwords confirm . //Create SELECT query to verify that the old password is correct $qry="SELECT * FROM members WHERE login='" . $_SESSION['SESS_USERNAME'] . "' AND passwd='".md5($_POST['opassword'])."'"; $result = mysql_query($qry); if($result) { //Create UPDATE query to replace old password with new password $updatepasswd="Update members set passwd='".md5($_POST['npassword'])."' where login='" . $_SESSION['SESS_USERNAME'] . "' AND passwd='".md5($_POST['opassword'])."'"; $update = mysql_query($updatepasswd); //Check whether the query was successful or not if($update) { header("location: changepasswordsuccess.php"); exit(); } else { die("Query failed"); } } else { header("location: passwordchange-failed.php"); } Link to comment https://forums.phpfreaks.com/topic/228400-help-in-php-mysql-password-change-code/ Share on other sites More sharing options...
chaseman Posted February 21, 2011 Share Posted February 21, 2011 You should validate before you even do the query, like this: if ($oldpassword != $dboldpassword){ echo "incorrect password"; } $oldpassword is the password the user entered into the input field and $dboldpassword is the password in the database the validation checks if the entered PW matches with the PW in the database, if NOT then it will print out an error. Hope that was what you were asking for. And please use code tags next time. EDIT: Here's a good video tutorial on register and login system which also explains how to implement a change password function: Link to comment https://forums.phpfreaks.com/topic/228400-help-in-php-mysql-password-change-code/#findComment-1177708 Share on other sites More sharing options...
Veteah Posted February 21, 2011 Share Posted February 21, 2011 Also, just to point out, you're using $result incorrectly. All it does is tell you whether the query failed or not, not if it returned any rows like it looks like you're trying to see. For that you should use mysql_num_rows(), but yeah, you should do password verification before you start making queries. Link to comment https://forums.phpfreaks.com/topic/228400-help-in-php-mysql-password-change-code/#findComment-1177710 Share on other sites More sharing options...
Pikachu2000 Posted February 21, 2011 Share Posted February 21, 2011 Just because an UPDATE query completes successfully, doesn't mean it actually changed anything. You'd need to use mysql_affected_rows to determine if any changes were made to the record. Link to comment https://forums.phpfreaks.com/topic/228400-help-in-php-mysql-password-change-code/#findComment-1177737 Share on other sites More sharing options...
sj005 Posted February 21, 2011 Author Share Posted February 21, 2011 Thanks to all I got it working. It was something really simple, what veteah pointed out. I replaced $result with mysql_num_rows($result)!=0 and made some other similar small adjustments. I guess i am still in the progress of understanding the different functions of php and mysql. Link to comment https://forums.phpfreaks.com/topic/228400-help-in-php-mysql-password-change-code/#findComment-1177753 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.