Hello everyone, welcome to this Spoken Tutorial, which is more of an update tutorial and not a full length video.
Some one has pointed out to me that in my register script, I need some kind of check to note if the user has been registered or not by the username that they specify.
Let us go back to our form which is here. Here you can type your fullname. You can choose a username and a password.
I have had these values here before. Let us get rid of them for now.
But, what we want is, when we are choosing the username...
For example, lets say I'm registering with the username "alex". In the database we can see here that username "alex" already exists.
So what we will do is check the existence of the username.
If the username already exists, we are not going to let the user register because we don't want a double username.
If I were to register here, let me put the password in and choose the username as "alex". The username "alex" is already in the database.
Let's just change this for namesake and click register.
I have been successfully registered.
Let us look inside our database. We can see that we have two usernames with alex.
Now this causes problems while logging in.
The 1st occurrence of name, this one here will be logged in. And this one will be ignored.
So this person really will never be able to login into the database.
So let's just delete this.
You need to create some kind of check to see if the username already exists.
This is incredibly easy. There are more that one method for doing it.
But I am going for the simplest and probably the most effective way which is going to work.
The first thing I want to do is, take my code to connect to my database.
Selecting my database. I want to take this up to just where the submit button is checked.
So, it is just connecting to the database. I am inside here.
Then, under here I can start my code to check my username.
Now you can't put your check anywhere. For simplicity I am just going to put it here and kill the rest of the script.
If the username has been found, I can put it in anywhere.
Take care when you are using a full length page in your website, the die function will cut off the rest of the code. So I don't recommend using this.
I recommend casing the checks that you already have inside the next statement and not really to kill the script.
But you will get the general idea here on how to work on what we are trying to do.
We need to just type a query that specifies taking a record with a particular username.
So I will say "namecheck query" here. I will call the variable "namecheck" and this will be a mysql query.
I will select "username" for simplicity. This is not going to select all the data.
So I am selecting username from users,
since that's our table name here.
I am going to say where username is equal to... If we look up here username of the person that submits the form is in the variable name "username".
So we can just come down here and type "username" now.
Now if we choose the name "alex", this would select every record in the database that has the username "alex" and we can see there's one at the moment.
Now if I were to specify in this case, with only one record...
If I were to specify the username as "Dale", for example, no records will be returned.
So therefore, the username won't exists, if no records would be returned. So we need a function to check how many records are returned.
You can do this by creating a count variable. Its "mysql num rows".
It just returns the amount of records or rows that are contained within your query which is called "namecheck".
So lets just test this. I am going to echo out count and then kill the script.
The rest of the code does not execute.
Lets go back to register and I'll type my fullname as "alex".
Fullname, then choose a username. I am going to choose "Dale".
The password won't be checked so we could skip that.
But I will just put them there for the sake of it and click Register.
We see that we have got a value of zero that is returned.
That's because "Dale" is not actually in the data base as a username.
However if I change this to "alex", that will be a small "a".
We have got some... strip tags. The way to deal with case sensitivity as well, is.... so this is another pointer....
When we are taking the username into account what we are going to say "string to lower" here, just to make sure that this will always convert to lowercase.
Next we are going to... let me find it.... Click Register.
We can see that the value of one is returned. .
So the check that we were looking for here is - if this variable we are echoing out, is not equal to zero,..then we need to tell the user that the username is already registered.
So here we will create a simple if statement and our block.
Then we can say, if our count doesn't equal zero, meaning there is a record present under this condition where the username is already specified...
...then we can just kill the script and say "Username already taken" or any other message. Coming back here, lets refresh.
We can choose "alex". Let me type in a password and click register.
You can see that we have got "Username already taken" error.
If I was to type "Dale" and choose a new name and password and click register, we can see that it has been successfully registered into the database because the username does not exists.
So I will leave it at that. You can see that we have got our registered user in.
Add a "str to lower" function, which is a really useful thing to keep everything simple.
Or you can just use a "str to lower" function in your if statement.
However, to keep it simple I would recommend that you convert all usernames into lowercase.
You need to incorporate that into a login script as well. You need to convert whatever the user is typing into the login box to lowercase.
I encourage you to play around with it. That's a good way to find some errors.
Try them but if you do need any help, please email me. Make sure you subscribe for updates.
Thanks for watching. This is Aravind dubbing for the Spoken Tutorial project.