Welcome to the 2nd part of our “Change Password” tutorial. In the last one, we learnt how to check if our forms were submitted.
We’ve got our data values in here.
Please remember that inside our database, our passwords are encrypted.
So, as soon as these fields are coming in, I will encrypt them into an md 5 hash.
Make sure you put the brackets.
What I have highlighted here is our parameter.
So, here we will have our md5 encrypted passwords.
We will need to check this field to see whether they exist or not.
At the moment when we submit our form, we see that nothing really happens.
First I will say “check password against db” and then we have to connect to our database.
We have already connected to the database in several of these pages- like the Login page.
You can put this into a separate file, say, “include” and” include connect .php” with your one time login script in, so that you don’t have to keep typing it.
But for our tutorial's sake, I will keep typing it over and over again because this is a good way to learn.
We type here - "connect = mysql_connect".
And we will be connecting to our local host database, with my username as root and my password as nothing, I am going to select my database.
So that is “phplogin” which here. Lets go there and you can see it here.
Our table is "users", which we can use later on.
Next we will create a query to get the passwords.
So I will type “ query get” which is equal to mysql.......... "mysql query" and here we will type "SELECT password" - We need to ascertain the password from the database "users".
You can see here. This is the "users" table.
Then we type “Where username is equal to user”. This is our session variable holding our user’s user name.
So, what we are doing is we are selecting our password hash from this table where the username is equal to the session name, and that is equal to “Alex”.
So, that should be a successful query. And you can type at the end “ or die "Query didn’t work”" - some error message.
You can be a bit imaginative with these error messages and type what you like.
Same here. You can say “or die”. You can add your own error message in here but to save time, I am not going to right now.
Now, we will use this slightly differently, before we use the "while" function to loop through every record in the data base.
I was informed about this method through a comment someone posted. I will say "row = mysql_fetch_associative". And that‘s "query get"
We will set “old password db” which is a new variable name . Don’t mistake this with the old password that has been submitted.
Our old password inside the database will be equal to our row.
Remember this creates an array.
So this value is” password”, because inside our database, this is “password” here. You need to use the labels.
So from here on we can check our passwords.
Checking our old passwords and our new passwords is just a simple “IF” statement.
Lets type - if the old password is equal to the old password inside the database.
These are both md5 hashes because we converted them into an md5 hash earlier.
So, if they are equal then we’ll run a block of code, otherwise we’ll kill the page and say ” Old password doesn’t match!”.
So here, assuming we’ve got through the first stage of our validation,- we checked the old password with to old password in the database - now we need to our two new passwords.
Now this is just as simple as typing “if new password is equal to repeat new password”, then we can write a block of code, otherwise we can just kill the page and say “ New passwords don’t match!”.
So here this is “success” and then we’ll say “change password in database”.
So now what I’ll do is echo out “success” and I’ll go back to my page.
I’ll type my password wrong on purpose. So I’ll just type this.
My new password I’ll type as "abc" and then clicking on “change password” we get the message "Old password doesn’t match!".
If I type "abc" as my old password, which it is, and "123" as my new password and random letters in the next, we should get.....Oh "Old password doesn’t match!"
Lets go back and check the code. Old password......... row - password............ query get........
What we can do here to debug is just say “echo old password db” with a break on the end, and just say echo old password with another break.
What we can do now is, run the script again, so old password equals "abc", new password equals "123" and then random letters.
okay so let’s compare these. They both look the same to me, so we can see that we’ve got a problem here.
Again lets check the code. Checking for the spellings.
Ok I just found out the problem. If I go back to my database here, we see that I had added in this value myself and I had created this space at the end of this - you can see it highlighted in blue - I’ll just get rid of that quickly and I’ll come back to my page.
I’ll login again as usual and quickly change my password, I’ll put my old password in correctly and random text for my two new passwords.
You can see that my two new passwords don’t match.
We've echoed this out already, so now we can delete this.
So assuming my passwords do match, let me echo this success message.
So let’s just delete these. I put them for debugging.
I’ll just type in my old password, my new passwords 123 and 123, click change password, and we’ve got success.
So I apologise for that last slip-up there.
So in the 3rd part of this tutorial, we will continue with updating the user's password and just making sure everything’s working properly.
Thanks for watching. This is Joshwa Mathew dubbing for the Spoken Tutorial Project.