PHP-and-MySQL/C4/User-Registration-Part-6/English-timed

From Script | Spoken-Tutorial
Revision as of 17:12, 2 September 2014 by Gaurav (Talk | contribs)

Jump to: navigation, search
Time Narration
00:00 Hello everyone, welcome to this Spoken Tutorial, which is more of an update tutorial and not a full length video.
00:08 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.
00:19 Let us go back to our form which is here. Here you can type your fullname. You can choose a username and a password.
00:28 I have had these values here before. Let us get rid of them for now.
00:33 But, what we want is, when we are choosing the username...
00:37 For example, lets say I'm registering with the username "alex". In the database we can see here that username "alex" already exists.
00:47 So what we will do is check the existence of the username.
00:50 If the username already exists, we are not going to let the user register because we don't want a double username.
01:01 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.
01:13 Let's just change this for namesake and click register.
01:20 I have been successfully registered.
01:23 Let us look inside our database. We can see that we have two usernames with alex.
01:28 Now this causes problems while logging in.
01:31 The 1st occurrence of name, this one here will be logged in. And this one will be ignored.
01:39 So this person really will never be able to login into the database.
01:44 So let's just delete this.
01:48 You need to create some kind of check to see if the username already exists.
01:53 This is incredibly easy. There are more that one method for doing it.
01:59 But I am going for the simplest and probably the most effective way which is going to work.
02:05 The first thing I want to do is, take my code to connect to my database.
02:12 Selecting my database. I want to take this up to just where the submit button is checked.


02:20 So, it is just connecting to the database. I am inside here.
02:26 Then, under here I can start my code to check my username.


02:31 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.
02:39 If the username has been found, I can put it in anywhere.
02:44 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.
02:53 I recommend casing the checks that you already have inside the next statement and not really to kill the script.
03:00 But you will get the general idea here on how to work on what we are trying to do.
03:06 We need to just type a query that specifies taking a record with a particular username.
03:12 So I will say "namecheck query" here. I will call the variable "namecheck" and this will be a mysql query.
03:21 I will select "username" for simplicity. This is not going to select all the data.
03:27 So I am selecting username from users,
03:35 since that's our table name here.
03:39 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".
03:50 So we can just come down here and type "username" now.
03:55 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.
04:09 Now if I were to specify in this case, with only one record...
04:15 If I were to specify the username as "Dale", for example, no records will be returned.
04:20 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.
04:29 You can do this by creating a count variable. Its "mysql num rows".
04:36 It just returns the amount of records or rows that are contained within your query which is called "namecheck".
04:47 So lets just test this. I am going to echo out count and then kill the script.
04:53 The rest of the code does not execute.
04:57 Lets go back to register and I'll type my fullname as "alex".
05:03 Fullname, then choose a username. I am going to choose "Dale".
05:10 The password won't be checked so we could skip that.
05:16 But I will just put them there for the sake of it and click Register.
05:24 We see that we have got a value of zero that is returned.
05:28 That's because "Dale" is not actually in the data base as a username.
05:32 However if I change this to "alex", that will be a small "a".
05:39 We have got some... strip tags. The way to deal with case sensitivity as well, is.... so this is another pointer....
05:49 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.
06:01 Next we are going to... let me find it.... Click Register.
06:08 We can see that the value of one is returned. .
06:12 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.
06:25 So here we will create a simple if statement and our block.
06:29 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...
06:40 ...then we can just kill the script and say "Username already taken" or any other message. Coming back here, lets refresh.
06:50 We can choose "alex". Let me type in a password and click register.
06:56 You can see that we have got "Username already taken" error.
07:00 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.
07:15 So I will leave it at that. You can see that we have got our registered user in.
07:22 Add a "str to lower" function, which is a really useful thing to keep everything simple.
07:29 Or you can just use a "str to lower" function in your if statement.
07:32 However, to keep it simple I would recommend that you convert all usernames into lowercase.
07:39 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.
07:48 I encourage you to play around with it. That's a good way to find some errors.
07:53 Try them but if you do need any help, please email me. Make sure you subscribe for updates.
07:58 Thanks for watching. This is Aravind dubbing for the Spoken Tutorial project.

Contributors and Content Editors

Gaurav, Gyan, PoojaMoolya, Pratik kamble, Sandhya.np14