This is the 3rd part of my “change password” tutorial. In this part, we’re going to change the password in the database.
We’ve already connected to our database up here.
We’re already connected here so there’s no need to reconnect, since that command has already been reissued.
I’ll create a new query called “query change” and that will be equal to “mysql query" function.
Now, this is a new bit of code. So I will scroll down so that you can see it easily.
This is “UPDATE”. So I’m going to say “UPDATE users” - which, you can see is our table - so to update our "users" table.
I will say “SET password equal to new password”
making sure I use inverted commas here.
Then I’ll say WHERE username is equal to the "user" variable that I’ve got on my page currently.
Now this equates
to what we have in this column here.
So since we’ve already processed our php session,
that is equal to "Alex".
This piece of code is basically saying “update the table, change the password to the new password entered by the user. – this is the password they want.
and change this “where” to Alex,
as this is equal to Alex.
So, this password will be changed because this username is equal to Alex.
So this starts with 900 and as soon as we change it, we can refresh this and check that it actually has been changed.
So I’ll just add a few more things
Let’s just put this back up here.
And I’ll kill the page and say “die” and then say “Your password has been changed”.
Then I’ll put a link in saying “return” and that’s going to return to the main page.
And that is “index.php”.
Before we kill the page, I’m going to destroy the session.
So “session destroy”.
The reason being, once the user has changed their password, this link will take them back to the main page, and this will destroy the session.
So they’ll need to login again using the new password.
So if we test this, remember in here, my current password is "abc" whose md5 hash starts with 900.
And if I go back here, write my old password- "abc", my new password "123" and click “change password”. , we see that all the validation has been checked, our password has been changed,, and we get this message to return back to the main page.
Now if I try to go back to the member page, you will see that you must be logged in. Our session has been destroyed, as we used our “session destroy” function here.
And also, when I login again and type "abc" as my password which is my old password, we get an “Incorrect password” message.
If I try "123", you’re in, and evidence of this is shown in here.
Let’s just go back and click “browse”. Lets scroll down and we can see that the password has been changed from 900 to 202.
Therefore this is a completely new hash and a completely new password.
So everything’s working fine. You can see that it is quite simple to do this.
All you need to do is learn your "sql" queries properly. I have tutorials on that, too.
And you need to think logically about how to check your old password, and your two new passwords,
Obviously when we did our registration, we had a limit for how big the password should be.
I’ll leave that to you to enter another check to see if the password should be bigger than 6 characters or no bigger than 25 characters.
So really there are a lot of checks you can do, but this is the basic skeleton for changing the password in php using a mysql database.
Hope you enjoyed this. If you have any comments or questions please let me know. Also subscribe to video updates.
Thanks for watching! This is Harini dubbing for the Spoken Tutorial Project.