 Right, so hey guys and welcome back to another PHP tutorial if you guys remember in the last tutorial We created a website that would allow us to basically Enter you use the name and a login password and then redirect us to a session page This was only done using fixed variables and not really the way it should be done by linking it to a database So in today's tutorial, we're going to be carrying on with linking it to a database so that we have a register interface as well So first of all what you want to do is go to ZAMP and turn on the Apache and SQL servers Once you're done with that you want to go ahead and click on the admin for the SQL because that's where we're going to be managing the database for this system create a new database. I'm just going to call this Trial system and then we click on create and Then in this database, we're going to create different tables that are going to store the information that we need So the first table we're going to store users. So the different users that are going to be registered on the server So type in the username. I mean the name of the table called users and then click on go Once we're done with that, we're going to create the different fields that are going to be stored in the table So what we want to be stored is an ID that will automatically be incremented for every username that is selected So the type is going to be integer and go over to the AI auto increment and then click on Take it up and then click on go. So next what we're going to be saving is the username and Password and that's about it So we'll be saving ID username and password in a real-life login system Obviously, you'll be saving more details about this and also like Hashing the password so no one's able to crack it and stuff But today I'm just going to keep it really simple because this tutorial is really intended for beginners So what we're going to do is change this from Integer type to either text or work here. We can do it whichever one you like I prefer text personally and then you can set a length if you like as well So the username I'm going to set a length of 50 and the password Okay, so once we're done with that, we're going to click on save and Then as you see right here, we have a trial system database inside with one table and it's called users And we have ID username and password So what we want to do first of all is go over to our code from last time I if you haven't watched the last tutorial I really recommend you do because otherwise this wouldn't make sense links going to be in the description So in the last tutorial as you know, we created a login page So in this one, we're going to create a register page as well within the same system So this bit right here is the form that allows us to log in into the system While we're also gonna what we're going to do is create another button down here I'm going to do input type equals summit name equals Register start And then the value is going to be equal to register obviously Okay We're just going to do a VR Okay, so now if you go over to localhost My folder is called login and register So If I go over there as you see right here As you see right here We have another button called register and we have a button called login Both of them don't have any actions assigned to them because I've taken off the code which I had from last tutorial So when we click on this essentially what it should do is take this information Check if a user already exists with this name in our database If he does it shouldn't allow that username to be used again And if he doesn't it should register the user So that is what should happen and if user clicks on login It should just log in and log them into the account checking if the account exists obviously So what we would want to do first of all is do the if asset statement. So we're going to do if this is a Post so if the event of a post happens and of the value that is submitted equals Register start Then what we're going to do is we're going to grab the username username and Also, we're going to grab the password. So we're just going to copy this over again and Paste it right here So what these two lines of code are going to do is They're going to grab the username and password Which is going to be posted from these input boxes once the login button is pressed and it's going to save them into the username and password variable, so we're just going to Tab this as register so that it's going to be different to the login variable. We're going to create later Once that's done. What you want to do is start a MySQL connection. So MySQL connect localhost Because that's where the server's running localhost and User is going to be the root root user and the password is none Once you're done with the connection what you want to do next is Select the database so MySQL select database The database name was system trial. Okay I'm just going to check that again. Yeah, it's trial system. Actually, so just the other way around Okay, now that we've connected to the database and we've selected the right database We're going to write a query. So we're going to do MySQL dot in query Query and in the query what we're going to do is we're going to select star from table of users so star means everything select everything from users where username Which is this we're referencing to this right now where username equals The variable username that we've saved up here. So we're saying select any Information that where from the table users where the username the field right here is equal to the username that we just submitted Okay, and then we're going to call this variable Request Once we're done with that we're going to do MySQL num rows request right and Then we're going to say results So what we've just done is we've created another variable called result and what this variable is going to do is it's going to grab the amount of rows That the use that for this request So let's say there is one username which equals the username. We've just entered it's going to return one if there's none It's going to return none if there's two obviously it's going to return two so what we're essentially doing is checking whether the given username which is entered Into the field here already exists within the table So that it doesn't get overwritten So we're going to write an if condition based on that actually we're just going to test this first so echo result Let's do a quick refresh on the page and we'll do register and define variable in usernames that on 26 Oh, so it was a mistake right there. So it's red username not just username refresh again and As we see right here when we click on register, there's zero users registered with that name. So if we go ahead and insert a Record manually into the system insert and The username is going to be this one right here. Just copy that over here Username passwords. Oh my god. It doesn't want to copy for some reason There you go. I'm possible. It's going to be one two three And the ID should automatically do its job So once we're done with that, we're going to go back to users as you see right here We now have the username admin at isaacota UK and a password So now when we try to click on register It should Return one because obviously there's one record in the database with that username So now based on this concept, we're going to write an if condition in the code So instead of echoing result what we're going to do is we're going to say if if the Results is less than one So if it's obviously zero Then we're going to say We're going to write a query that's going to insert the username and password into the database so that the user will have an account So we're going to do my ask you our query Insert into Users values Reg username We need single quotes as well. I think right comma reg password and We're just going to do a blank one for the ID so that it just ignores it automatically Okay, it needs to be in the same order otherwise takes my get messed up So once we're done with that query, you're just going to close that and then we're going to write the L statement. So if Or if obviously by any chance There isn't any so if the result is on zero that means there's no record I Mean if it's if it's one or greater than one obviously there is a record. So we're just going to say echo account with this username Already exists Okay Time for a quick check I'm going to refresh account with this username already exists and now we're going to try to sign up using a different account name Just click on that again. Not going to change anything. So if we try to log in using this which hasn't been registered yet it refreshes and Now if we go to I just want to get rid of this one now, so I'm just going to go into In Cognitive and try accessing it really quickly Log in and register Okay, that's perfect. I'm just going to close this down Because it keeps Pressing in the password. That's why it's a bit annoying. So now what we're going to do is go and check so as you see right here since we didn't have a User based on this username So Johan Godino it automatically Incremented the user into the system and if we try logging in with the same username again, so Johan Godino You try that the click on register it should say you account with this username already exists So if you do a new username like this It's just gonna create the account and if we go back and refresh as you see right here. It's created the account So when it does create the account, we just need to say echo account has been Created right So now that that's sorted What we want to do next is we're going to go on to the login sessions, so we're gonna say Else if this set is a dollar post Log in log in start I think it is yeah Logging starts If that's the case then we're gonna grab the username and password again I'm just gonna copy and paste this because there's no point of typing it again, and then just take up the red Okay, and then we move on to making an SQL connection so we can just copy this again Okay, this as well So we're literally just repeating the procedure here This is quite a short form of code because we're not using any security Measures and stuff just so that it's easy for you guys to understand So what we're doing here again is we're taking a request. We're making a request So we're gonna call this login request instead and then we're gonna call this the login result And change that to login. I mean you can keep the same variable names Well, I just prefer to change them up So we're gonna say select from users where username equals username equals Change this to the username variable and We also need to add a statement and say and password equals The password variable now we're verifying the password as well So now the login result if it equals one That means that the login has succeeded. If not, that means the password's wrong with this So we're gonna do a concept based on that. So we say if Login results is Greater than One Let's say if it's less than one, which means it has failed then we say echo Incorrect username or password Right and else that means the password is correct. We're just gonna do A redirect so we're gonna use a header tag to redirect the user. I mean a meta So I'm just gonna paste that in so what this is gonna do is redirect the user to the correct page So username Just gonna get rid of this So it's gonna redirect the user to something called session PHP, which was another blank type of file we just created last time in the last tutorial and We're gonna pass the information about the username being obviously username onto the next page too So let us just save this and quickly rerun it I'm just gonna run this again refresh account with this username already guess so okay. We're gonna type in We're gonna create a new legit account, so join one two three and The password is going to be one two three register account has been created. Okay Johan one two three and I'm gonna put the wrong password Login incorrect username or password. Okay, and Johan one two three and then one two three and As we see right here the system is working perfectly fine right now. So based on obviously the database and the Linking PHP code it's verifying whether the username or password is already existing the system and then allowing you to register and Also later on allowing you to log in simply using the simple script that we've created within this tutorial Obviously, it's missing a lot of security concepts like Using MD5 and stuff to hash the passwords But we can look into that in a future tutorial as well as obviously styling issues Which haven't been included in this because I just wanted to keep it as basic and simple for you guys as possible Anyway, guys, hope I was able to help you during this tutorial I look forward to helping you out even further if you type it type your problems in the comment sections or any Errors that you have the next tutorial is going to be about styling this website a bit more to make it look most more suitable and Similar to a login site and register site So stay tuned for that as well as the other tutorials that are gonna follow about PHP Anyway guys, thanks for watching. Please make sure to like comment subscribe and I'll see you in the next one. Peace out