PHP MYSQL - Login System - Admin Dashboard | Telmo Sampaio | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

PHP MYSQL - Login System - Admin Dashboard

teacher avatar Telmo Sampaio

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

9 Lessons (3h 60m)
    • 1. Introduction

      2:31
    • 2. MySQL Connection with PDO

      25:59
    • 3. Adding Security - Sanitise and Validate user data

      37:56
    • 4. Register Users with Hashed Passwords

      22:48
    • 5. Login & Logout users with Sessions

      35:15
    • 6. Edit User Profile

      18:06
    • 7. Creating Blog Posts

      40:49
    • 8. Creating Admin Dashboard

      32:49
    • 9. Deleting Users

      23:41
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

253

Students

--

Projects

About This Class

In this course you are going to build a complete Login and Register system using PHP and MYSQL as a database.

We are going to build an Admin Dashboard where you can update the users details, and also be able to delete users. We will create also a blog section where Admins or moderators can post blog posts, but guests can only see them!

And finally don't worry I will cover every single detail so you guys can easily follow along, even if you have little experience, I will add the full code in the end of the course!

By taking this course you will learn:

Learning outcome:

  • Create a Complete Login and Registration System

  • Adding Security by Sanitising and Validating Data against malicious scripts

  • Connecting to MYSQL database with PDO

  • Learning how to Hash user passwords

  • Login and Logout with Sessions

  • and much more...

Meet Your Teacher

I'm Telmo Sampaio, a full stack web developer from Manchester, UK. I love to share what I know through my training courses, teaching and speaking on my youtube channel. Any questions get in touch with me, I would love to meet you!

See full profile

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: All right, guys, welcome to these PHP with my SQL course. And let me just give you a quick overview about what we're gonna learn in this course, we're gonna learn how to do a log in and registration system. So in here, I'm going to show you the database. I'm gonna show you how you can connect Ph. B with a my SQL database. In here. We have a couple of our users that are in our database with their email, their password. We're gonna learn how toe hash passwords to make sure that our replication is completely secure. And we have in here the roles of the moderators You can log in as a user or is a nod mean. And depending on what kind of user you want, you're going to see different things. So if I go in here and just logging as Carl, for example, this is one of my user's gonna do a Gmail password. 123456 Log in. Now. I'm logged in this, Karl, This is my home page. I can see the blocks and I can update my details in here if I want to. If I'm gonna put car Lee, I'm gonna put update if I see these volleys now in here, I should see Carly now updated. I can also look out. Okay, Now I could log in, for example is and not mean. So I'm gonna put in here and not mean I want to drink for 56 This is my admission, the as mean as thes page that he can change the role of the users from moderator two guests . I can delete the users if I want to. Look, I can delete this one. For example, the late now is gone. If I refresh my database now, this is Ghana's. Well, now one last thing I want to show you in here is that if you go into your block, I can actually create a new post if I may not mean Okay. So I can go in here title. I'm gonna put post. Let me just delete actually one of these boats, so I'm going to go to my block. I'm gonna delay to these post three imposed too. OK, I'm going to refresh the page. They are all gone. I'm going to create the new post post to and supposed to body. I'm going to create this. And now look, it's in here. And if I refresh my databases well is all here. All right, so we're gonna learn these and so much more. So I hope you are excited for this course, and I'll see you inside. 2. MySQL Connection with PDO: What's going on? Guys, welcome back to another video today. We're gonna build a PHP project that will allow you to register and log in users. And we will also learn how to connect with my SQL database. How can you insert data inside of the database? How can you hash in creeped passwords? How can you just put some special content that will be on Lee available toe have means or moderators? I'm going to show you exactly right now what we're gonna build So in here, this is our main page. That is just something in years until more academies. Just because it's me building it and I haven't hear Welcome, guest. So when someone goes to your website, you see these message? Welcome, guest. Please log in or register toe. Unlock all the content on the top. You guys see that we have in here some novel ings, one that stays home. This is where we are at the moment one that says register and we can actually register someone and another one that says log in. If you already have someone who is logged in or or there's already a logging, they can do it so Let's go in here and just register someone I'm gonna put in here. User name. I'm gonna put John okay? And I'm gonna put the email john at email dot com. Oops. I need to put us in here. So, john at gmail dot com. And I'm gonna put 123456 for my password, and I'm gonna put in here register. Okay? Now that I did this, I'm I'm redirected to the home page. I can do log in and let me do john at gmail dot com I just created and I'm gonna do 123456 if I breast log in. Look, now, I seen here on the top this connected successfully, which I'm going to remove. This is just to test that our connection to the database is working. And now look what size in here. Welcome, John. It says your role is a guest. So whenever you create someone by default their role, you will be a guest. And and it says in here this is a super secret content on Lee for logged in people. So I know that this person right now is locked in. We are using sessions in here. But if you guys want, you can also use cookies later on. So look at our nav wings on the top. We got home, which is these at the moment we got our profile, which we can change. Okay, I can change the name of this person and also the email. Okay. And I can access the blog's. Remember, guys, if you were not logged in, you don't have access to these blog's. And you also have the log out. I'm just show you in here the database if I'm gonna refresh the page. Look, there is a new value in here for John and John at email and also the password, which is completely encrypted. And then in here the role of this person. I'm gonna show you how to build all of these. Let me just go back in here now. I'm just gonna log out. Look, this is the block for someone who is a guest. I'm just gonna log out this time I'm gonna logged in as someone who is a moderator. So I'm gonna put in here, Telmo, moderator, and then I'm gonna put the password to log in with these user. This is something that I created before. So from the log in look, I have home which say's welcome Telmo the moderator. Your role is moderator. I can go into my profile to change the name. Look, instead of having Telmo the moderator, I'm just gonna put Telmo moderator. I'm just get rid of these. I'm gonna click update details, Okay. When I click, update details. Now, if I go to my home Look. Welcome. Tell move. Moderator is not Tell me d moderator anymore. And if I go into my database and I refresh Look, now I have on the name Telmo moderated on Lee. Okay, one more thing. Now, if I go into my blawg look at the top because I'm a moderated, I can create a new post. Look, I can click in here and I go into the title and I'm going to create for a simple post three and in here I'm gonna put this is the post three for the blah, blah, blah, and I'm going to create post. And look, I just added the complete new post in here. Look, if I refresh the page is still there. We are these into the database. Finally, we have one more user, which is the admin. So let me just do the log out when a dual logging again this time is gonna be admitting at email dot com And I'm going to start with the 123456 on going into a log in. And look, when I have someone who is a nod mean, logged in on the home page, I can see all the users that are on my database. So I have Telmo guest. I have Telmo moderator and I have John. I can if I want, change the rules of these users. So I know that Telmo guests at the moment is only a guest. I know that Telmo moderator is a moderator. Look, it comes by default in there. And I know that John is a guest. A swell. If I want, I can change the role of John to be a moderator. Look, I'm going to click. Moderator update is user. I refresh the page. Look, it still stays as a moderator. If I go into my database, our refresh the page. Look, John, which is my last user in here now has a role of moderator at the nephew Gaskin. Seen year next to my cursor. Finally. If I want, I can delete the user. Look, clicking here. Warning. Are you sure you want to delete these user? Let's click Delete a user. And now look, it's gone. I don't have that Use it anymore on my database. Gonna refresh the page. And look, that user is gone from my database. Alright, guys. So this is the project that we're gonna be building today. Okay, I'm going to guide you from everything. Like you don't know anything, so don't worry about that now. The first thing that you guys need in order to start building this project with PHP is downloading some kind of a local server. The one that I'm gonna be using right now. It's called Zampa. Okay, so if you guys put on download Zampa, they should be the page. Look, if you are using, um, windows, you can download this one for windows. If you are actually using Mark okay for Mark. I actually think that this one would be better, So I'm gonna do downloads. Um, Mum, I think that's the name Mum, Pop. Okay, so if you guys, if you guys are using, um, Max. Okay, I would download this one month. You don't need to use the pro version. The free version is just fine. Okay, So what I'm gonna be doing right now is thes first video. I'm just going to show you how you can go through. And so I'm gonna set up just like the home page, and I'm going to set up the connection to the database, and then we're gonna keep going on with the other videos. I don't want to make them too long. Okay? So because I know you guys don't have too much time to watch all of these, so let's get started. Um, I need First of all, let me go back in here. If you guys install these amp, this is what I'm gonna be using. Okay, Once you install it, you need to go into Let me just show it If you're going to your disk, See, For example, if you haven't installed in their program files Zampa Okay, HD dogs. So this is the place where you need to create a folder for your project. So in here, I'm just gonna put um please log in project. I'm creating a new folder completely empty in year. And now from here, This is where I'm going to start creating my project. So, look, I'm on my visuals to your coat. I'm gonna open the folder. Okay? I'm gonna open these logging project that is completely empty at the moment. And now I can start writing down some stuff. First of all, you need to start your server, guys. Okay? Your server. Let me just go at the moment. I'm on C program files. Zampa HD docks. I want to go up one level if you guys keep going down. Look, you should have these three executed Bols, You have exams. Start Zam stop and zam control dot Exe. Okay, so these executable I want you to double click on this one. That's a example. Control X So once you open it, I'm just stop these. You should have something similar like these. You have one in here. That's a Apache and one that says my SQL. I want you to click on both of these ones. You can even clicking here on these other means to start as another mean if you have some kind of, ah, privileges problem. But you only need to start is to the Apache and the my SQL. Both of them should turn green. Okay, now, Okay. Inside of my project in here. I'm just going to create a new file called index dot PHP. Okay. You guys need to create files with the extension of dot PHP to make sure that is working. So right now, I'm going to start with this, okay? Whenever you're going to start writing down some peach because you need to start with this kind of tag. Okay? This is how you opening PHP. And this is how you cause it. Whatever you write down in between, these tax is gonna be considered s PHP. So I'm just gonna doing here at the moment, Echo, and I'm going to say my first beach be project. OK, Don't forget the semi colon in the end. And now I'm just gonna put a regular. Um, it looks like this is not working. HTML. I'm just gonna create something else in year. About about html. Just rename these. I just want to grab, like a regular HTML five template. Okay, so I'm just gonna copy these stuff Okay, so I created the new about the HTML so I can grab these template from a regular HTML. Just don't save this. I'm gonna face these in here, and that's it. Okay. Now, toe, open this file. This index dot PHP We should see on the page on Lee these echo. So if you go back on yours EMP folder that we were just in there you go inside of HD docks . What was the name of these folder that we just created? It's called Log in Dash Project. Okay, So if you go back to your browser now, what I'm gonna do is local host, and then I'm gonna put forth slash was a PHP dash log in. I don't remember. Let me see. I mean, opened these now it was logging project. Okay, let me doing here logging project. And look, I'm gonna do some zoom so you guys can see better my first speech be project. Okay, so these part is all working, So it means that we can actually see the code that is coming from this file. So you guys need to put on the top local most four slash the name of your folder that you created. And obviously because we are using the index dot PHP, you should put in here on the browser as well index dot PHP. But but because this is the default file for all the projects, look is going to read anyway. OK, so this is it. Our file is working. Guys, make sure that your server is running. If your server is not running, you can't just, like, run your project locally. So once this is done, let's just now get with our connection to the database. Let me just get rid of these about dot html. I don't need these at the moment. Eso What I'm gonna do is I'm gonna create in here a new folder. Let me see. What did I call this fall that I call this conflict. Okay, so I'm gonna click these buttons to create a new folder. I'm gonna create these conflict. This is and now a new file inside of it called db dot BHP. I think that's it. Yeah. Now let's create our connection to the database. So the first thing I'm gonna do in here is to create the variable in PHP. You just start with the dollar sign and then the name of the variable first thing I'm gonna do is I'm gonna start in here with the host, Okay? The host, because we are creating these project locally, is going to be called the local host. Okay, These needs to be in between single quotes. Okay? Don't forget the semicolon. Indy ends after. Okay? I'm gonna need a user. Okay? I'm gonna create the new variable called User and these user by the fault. Okay, guys, when you are using something like thes something like exempt, it comes with a user by default called route. OK, you can google it to do whatever you want. It comes by default with a user called route. It also comes. I mean, put in here with the passwords. The password that comes by default exam is also route. Okay, if you guys are going to be using No, Sorry. The password that comes by default on on Zampa is empty is like nothing so you can put this kind of Ah, empty string. Finally, we need to put in here a database name which I'm going to show you in a second. So in here, I'm gonna put DB name, for example, and I'm going to call these, um, logging, logging project. Think this is fine logging project. So now, after I created all of these, I need to set up my database. Name. Source. So how I'm gonna do it, I'm gonna do the s n. Okay. This is a short for just gonna put in years. So you guys can copied after, can you? Some kind of comments, if you would like to. Okay, set the Ascend database source name. He goes to ds, and and now I'm going to do in here. I'm just copy this. I have my SQL. The host is gonna be the host. Appear. Look, I'm using these dots. These dots means that I'm connecting Strings is called the concatenation. So I'm putting in here the my SQL A host is gonna be the host that I have been here on the top and my database name is gonna be whatever I put down in here. We are going to create our database in a second. OK, guys, So we need these. Let me just see if I am missing something. So I'm grating my database name, source and then I need to do a try catch block. So I'm gonna do a try. Catch, Okay. And in these catch, I'm gonna put some kind of errors. I'm going to show you in a second What you need to put now what we want to try. First of all, we are going to use PDO to connect to our they the basis, like the most modern way, the object orientated way to connect to a database with the PHP and my SQL. So I'm just going to copy this, and I'm gonna put it in here so you guys can see I have created a PDO instance. I have PDO equals two new PDO. You know, this is to create our connection. First of all, I'm gonna pass my database source name that we just created in here on the top. The second parameter I'm passing in here is the user of Mayes emp. And finally, is the password of Mayes. EMP. Okay, After I created this, I need to set up two attributes, so I'm gonna put in here these two attributes. So the first attribute that we're setting up for our PDO connection east to use the object orientated way toe grab after the values I'm going to show you after, Why is it so important? And then this one, these attributes is to set the errors toe toe exception mode so we can actually see the errors better in our screen. So don't worry too much about these right now. I'm gonna explain further up along. So if there is some error with our connection, by the way them let me just put in here and Neco Echo connected successfully. So using echo is the way of PHP toe actually print something on your page. So if you say echo, we're gonna print these on the page. If everything goes successfully okay on our connection, then if the recent sort of errors what we want to do is I'm just going to copy it in here, I'm gonna put these echo connection failed, and then I'm gonna put the actual error that comes from the connection error. So these is Don't forget now. We started our PHP tag on the top, and I'm just going to close it down here to close it. I'm going to do this. So here it is, our connection to the database right now, we need to connect these connection in here on the top with our index dot PHP. But we also need to create our database because we still haven't done it. So what we can do with this is going in here to our browser. You guys can go on the top. Let me just reset the zoom in here if you guys go to Okay, local host, four slash beach be my admin. Okay, Local host. I'm gonna put in here on the screen, maybe Alaskan. See, look, local host four slash speech be my admin. So now I can remove this. It should bring you these PHP my admin interface that you can actually use to interact with the database. So right now there is in here on the left side all the databases that I created from before , and I'm going to create a new database in here if you go in here on two new. Okay. Clicking here on new and you're going to select the database name? What was the database name? We just gave it in here. We said logging project. So let's call it like that lugging project, okay? No, I can just go in here and do create. That's it. You don't have to do anything else, Okay? Now, when you create a database is gonna ask you, Do you want to create some kind of table? The table is going to be used now for for us to put some values for our users. So I'm going to call the stable users, and I want to see stable toe. Have, um how many tables I wanna have an i d for the user and name, email, password, and use a role. So I'm gonna put five in here Gate five columns. I'm gonna press go now in here on the first column I want to put I d. The type of it is gonna be an integer because an I d should be an indigent. And I'm going to select in here on the right side under these ai that stays out of increment. I'm going to click on it and select it and just say Go. I'm not liking anything else. I just clicked in here on the way I look, I'm going to do one more time. Click on the AI in here, everything is fine. I'm just pressing Go. What these means is when you're going to create a new user because the idea is going to be out of increment. The first time that you create an user, the user is gonna have a man idea of one. The 2nd 1 is going to have an idea of 234 And so one second thing I'm gonna put in here is gonna be, um is gonna be the name. Okay, so it's gonna let me just he wants, um It should be fine. I'm gonna put to name I'm gonna put in here. The type of these is gonna be a var char. So the bar chart is just gonna be a variable of characters, okay? And I'm going to give it a maximum length of 255 characters. So whoever comes in here to my website and tries to register a name, I don't want the name Toby bigger than 255 characters. That's it. That's the only thing that you need to worry about. Now I'm gonna using here an email. An email is also going to be a bar chart is gonna be a variable of characters to 55. Same thing. Then I'm gonna have a password. The password is also going to be a bar chart, a variable of characters to 55. And finally, I'm gonna put in here a role. So this is gonna be the user role. The user role is also going to be a bar chart to 55. And I'm gonna put in here the default as you asking, seeing these parts, I'm gonna put as defined. So whenever I'm going to register a user for the first time, I want that the default value that comes is guest. Okay, so if all of these is fine, I'm just going to go in here and press safe. Okay? Now, if I click in here on my users, Look, I can check in here on the structure. I haven't I d for the first column name, email, password and roll. Okay, let's see if our connection is working fine. So far. So what we can dio is on my PHP logging project. I'm going to go and gonna save these. Save this file on my index dot PHP on the top. I'm just gonna dio require Let me just see if I have 18 years, so I can just copy it out for you. So there is no mistakes at all. So where is it? This is what I'm gonna do. I'm going to do a require. So what is required? This is in these index dot PHP file that we are just starting right now. I'm going to require the file. That is where I'm using these dot Slash To look on the current directory. Where is my index dot PHP? This is where we are writing down our code right now. I want to look into the dot ford slash I want to go into the conflict folder. Okay. Inside of the conflict fall that I'm gonna do adat four slash to go inside of it. And I want to grab these db dot PHP. So I'm going to save these. And now if I load my page in here on the top, I should expect to see a connection successful. Let's see if it's working or not and look connected successfully. So let me just put in here on the top before my, um, echo. Like I said, Echo is just to print out something on the page. I'm gonna put in here a break tack. And now when a refresh look my first speech be project, I do the break back. This is an HTML elements to go to the next line and then I have connected successfully coming from my connection to the database. If for some reason I have something wrong on my database connection in year, let's say that I put, for example, a user of rooty for example. I know that this is not correct because there is no user rooty on my exempt. Okay, there is no user in year called ROOTY. If I refresh my page look, connection failed access The night for rooty at local host is going to give you an error message fight. Put the details correctly again. Look like I said, guys, the host, by default when you are working locally, is gonna be local host. If you are working on some kind of server online, you would have to put the I P address of that server in here. I'm going to use a user of route because this is the default user from that from the example and the password is also an empty string by default. Don't put some sort of spaces or anything. Okay, Just empty string. And then in here is the database name that we just created right now. So, basically, guys, that's it Toward his video. We already learned what we're gonna be building during this project. We learn how to connect to a database, we learn how to create a table and put some values in there. And yeah, that's it for these video guys. I hope you enjoyed it. And I hope you are excited to continue this project. I'll see you in the next video. 3. Adding Security - Sanitise and Validate user data: What's going on? Guys, welcome back to another video today. We're going to build the register page so we will allow people to register in your website . So the first thing that I want to do is just like styling here beats the front page. I'm going to do something quickly in here. But you guys can also like it any time. Just pause the video or just copy the files that I'm gonna put the link in the description . So now what I'm gonna do is I'm going to go in here into my index dot PHP first of all, and on the top, in here on the side. Actually, I'm just going to start creating some folders. Would it create one folder for CSS And inside of this year says, I'm just going to do a style dot CSS file Just gonna put in here a back ground caller of blue, for example on the body, of course, these needs to be on the body just to test out if everything is working correctly. And now I'm also going to create a file a folder called Js for JavaScript, and inside of it, I'm gonna create the file called Main Dodgy s with a consul. Don't log of running. Let's make sure that these file is working. Okay, so I got these. These okay, now on my index dot PHP on the top, I'm gonna link my CSS, which actually is no working. So I'm just gonna create a random file in here, so I'm just going to call these about dots. H t m l I'm gonna do a regular template so I can just create this thing quickly. So I'm gonna the link CSS and I can just copy these. Okay. On my index dot PHP, I'm gonna put these in here and now I'm just gonna dio so I need to go into dot slash CSS and then the name of my CSS file is styled on CSS. Okay, so I don't need these about dot html anymore. Okay? I can just get rid of it. It was just like to grab these link, so I should have these all Okay, I also want to having here on the bottom script, okay. And I think I need to do these as well. Opening, closing, and now I'm gonna have a source. Okay? The source in here is gonna be dot Slash So we're looking on the same that factory yet. If I'm writing down these on index dot PHP, I'm gonna look on the same directory. I'm gonna go inside of the Js folder and then I want to grab the main door chess file. Okay, so main dot Js I'm going to save these. I want to also install in here or just connect bootstrap. So boots drop. I'm gonna put these on Google. Just go to boots drop. I'm gonna go to get started to get started. I'm going to copy these Lincoln here on the top for the CSS. So let's go in here just before my style that CSS I'm gonna pace this one. So I got this one in here. Now, I need to copy all of these scripts in here for the JavaScript as well. So I'm going to go in here And just before my javascript in here, I can just based these three links. Okay, One more thing. I want to grab a font from Google Fonts, so I'm gonna put Google funds. So where is Google funds? Just in here. I want to use these 14 year called Roboto. You guys can use any funds that you would like. So when you click on it, I'm gonna open the page and selecting Here on the top. Select this front down here. You guys can go and just copy these link. Okay, that is in year on the site. So is link H. Ref wants something. So I'm going in here and just before my style dot CSS I'm gonna put a space when a link, this fund in here and now if I want to use it, I need to copy these parts from here on the bottom, you know, if you guys can see it or not. So I'm just gonna move these in year. Look, when you click, this is the link that I copied. And now I need to use these on my CSS. So let me go to my CSS. Uh, I'm just going to target everything on the page using the asterisk, and I'm gonna do this one. I want everything to have these kinds of funds from my Google funds gonna do a margin. Actually, I don't need to do all of these because I'm just gonna copy After what I have in here on the side, right. Okay. Let me just refresh my page as as you guys can see now All my pages blue. So it means our CSS is working also. If I open the inspect by doing right, click on the page and doing inspect. If you go on the top, this is open the developer tools on the top. You should have been here A console. So let's run these. Our consul is not running at the moment, so there must be something wrong. So on my script source a look I put S c are you shouldn't be SCR should be S r c. Just go back, refresh the page and look, just increase the size of your eyes can see running so all of these is connected Perfect. Okay, One thing that I'm going to do in order to make sure that our code is ah, well dynamic for the all the different pages I'm actually going to create a new folder in here called in Okay, for includes Now inside. I'm going to create the file called Heather Thought html and what I'm gonna do in these heather dot html is on my index dot PHP. I'm gonna grab everything I actually need to put enough. Okay, So I'm gonna put in here enough, Okay? I'm gonna put these First of all, I'm gonna put the nav inside of it. I'm gonna have, like, a NH three, for example. So h three now, I'm gonna put in here Telmo Academy. And after that, I'm just gonna put a UL. Okay, I'm gonna put the UL. We'd some allies inside. So el eye opening in closing and inside, I'm gonna have a treff, so I'm just going to copy what we have in here. Okay, So I'm just gonna copy these. I'm just gonna paste it in here, because basically, this is what I need to put. So as us conceive e, I have three allies with eight ref for the home for index or beach be. This one is going to be for the register page that we're gonna build right now. And we also have these ally to go to the log in page, which we don't have at the moment. But we will build in another video. Now we got all our navigation bar. What I want to do is these code that I'm highlighting. I want these girls to be the same all over the all the pages that I'm going to create. So what I'm gonna do is I'm just going to right click cut these off. And now I'm going to My includes folder my heather dot html. I'm gonna pace this. Okay? Now, if I want these code in any of my pages, what I have to do is let me just check in here to make sure that everything is fine, what I have. So what I'm gonna do is in here. I'm gonna do a PHP required. Okay, So this is the way that we can require this file from here so we can reuse it anywhere that we want. So I'm going to do in here, require I'm gonna do a dot Slash. Like you guys, remember, the dot slash means you're looking on the same directory. I want to go into ink, and then this is the full that I want to grab the heather dot html Heather that html. And now don't forget to put the semi colon. And now, closing the PHP tax like I mentioned before, guys, whenever you want to write down some PHP in here. You just start with these opening PHP tac in closing PHP tag. And what we are doing with this function of requiring he's just like clothing. These code in here into our index dot html. Let's just reload our page and make sure everything is still working. Fine. Look, everything is still working fine at the moment, we need to apply some CSS styles, which I haven't yet on the site. So I'm gonna copy all of these styles, and I'm going to show you. So because this is not a CSS styles class, we are. We want to learn about pH. B in my SQL. So let's not lose too much time with this, so I'm gonna put in here on my styles. I mean, just remove everything in year. I'm just gonna based my a CSS. So what I'm gonna do is I have links on the description if you guys wanna use them. If not, you can copy it. So I'm just going to slowly scroll down so you guys can pause the video and copy all the styles. So we had in here until now. You Well, so Look, I'm going to scroll down a bit. Scroll Slowly, slowly, slowly until the nav you. Well, okay. And that's it. This is all the styles that you guys need. Okay, So I'm just gonna scroll if you want to pause it right Now, look, these nav, you. Well, I'm going to scroll a beat up again. Make sure that you have everything. Okay? Wanna save these and let me now, see? How does these look like at the moment? These all looks good. OK, we have in here enough academy. We got the home register log enough. Ah. And now I want, actually, on my database configuration. I don't want to say anything at the moment on Lee. If I have some error on my connection, I want to see. So let me just comment the sting out for now. Okay? Let me refresh the page. My first BHP project. Where is this one coming from? I think it's on my index dot PHP. Yeah, let me get rid of these stuff on the top as well. I don't need these anymore. Okay? I mean, refresh. This is all good. Now, what I want to do is start applying some. Um, let me also. Look, this is my index dot PHP These stuff up here, all these JavaScript scripts that they will be available in all the pages. Let's just cut these off and create another file for it. So I'm going to go in here and create a footer thought html. Okay, so all these code, they need to be present in all the pages. So I'm just going to right click cut this off and put it in here on footer. Okay, so now I can go in here onto my index dot PHP, do the same thing off, okay? Requiring. This time, I want to require what the footer dot html that I just created. Okay, so I got everything that I want on the top. This is my footer. If you guys remember in here now, I'm just going to create some stuff, so I'm going to create a dif okay. With a class of container, So let's close these up. Okay, so we got in here today for the costs of container. Let me see if I'm just missing something else. I'm just gonna do in here some thieves in here with some bootstrap classes. Okay, so this container is just gonna make sure that everything is centered on the page. Okay, So I'm gonna put in here, Dave. I mean, closed these. And now Okay. What do I have I have in here at cards with the class of card. So this is bootstrap classes. OK, guys, this is just to apply some styles to our our project. I'm going to do a BG light. I'm gonna do it. N margin bottom of three in here. So I got these. And after I have this card, I can have a card. Heather. So I'm gonna do a thief. Okay? I'm gonna close this one as well. I'm gonna put the class of Carl Nash, Heather and inside. I'm just gonna put at the moment Ah, in h five. So I'm gonna put the nature five and in here. I'm just going to say Welcome, guest. Welcome, guest. Now, after the car of Heather, I want to do a card body, so card body. Like I said, this is all some bootstrap classes, and I'm going to show you in a second how they look. Like what? I'm just gonna right down in here. is gonna be this. Okay, so I'm just gonna put in here, please log in or register to unlock all the content. Because when you load the page for the first time, you should be Look, someone who doesn't have access to anything, because look, you are just a guest. So this is it for my index dot html page. Okay, we got everything. Now we need to go to our register page, as you guys can see does not exist at the moment. So let's create It s I'm going to go in here. Look, this is how your folder structure should look at the moment. Let me just see if everything is fine. I think everything is fine. So I'm gonna go in here and just create a new file called Register Thought Ph B. Okay. What I'm gonna do is I'm just gonna copy everything that I have been here on my index dot PHP. Gonna put the same thing in here. Now, let me see what my registered the beach B has. What I want to do is first of all, I have my heather. I have my footer. Okay. I need to start creating some sort of forms, So I got my container. So let me just the leads. Ah, I'm just gonna leave these devi in here, okay? This is going to be with the claws of cards. And then what? Actually, I needed this. So this is fine. I have these. Which is gonna be We've, uh, just register saying I'm gonna put in here, register skin, go all on the same line. I guess so. Register. Let me see. I'm gonna put any other cards. She's gonna be kind of Heather. Okay, so this is kind of Heather, and then I'm gonna have some card of the body. When a copy of this, I'm gonna put the card body. All right, let me the lates. All of this. So inside of my card body, what do I wanna have? Okay. Actually, I'm gonna even rob. So I got my card. Heather, I got my card body, so I'm going to wrap everything in here. Okay, We dave with a class of cards, okay? This is all some boots. Drop classes, guys. Like I said, I'm going to show you in a second how they look like so inside of my card. I wanna have a form. So this is my card body. So I'm gonna put in here a form, Okay, A form First of all needs in action, which I'm going to explain now what it is. So whenever you served me the form, you want to send these data to some sort of file toe handle All these data that you are submitting at the moment, You know what I'm going to do? I'm going to send all these data that we are actually typing down in here to the same file to this one. Okay, so I'm gonna put in here register dot PHP. So whenever I submit this form of thes date, I'm going to send it to these file, which we're gonna grab in year on the top. I will show you in a second. So we got these, and then we need in action. Sorry. A method. So basically, there are a couple of different methods that you can use in orderto send data to the database or to retrieve. If you want to send some data to the database, the best method to use will be the post method. Okay, so this is the method that you use If you want to send something into the database, there are different kinds of methods. Like get for example, if you want to get things from the database. There are others like method, like the leads. If you want to delete stuff from the database that he's put if you wanna update some things from the database. But right now, we're going to use this one post to send things to the database Now, in here, inside of the form. What I'm gonna put is I'm gonna put the div with a class of form group. So let me just copy what I have in here so I can speed things up a bit. Okay? These form group is just like a bootstrap class just to give some different stylings and you will see in a second. How does he look like? So I'm gonna have in here now. A label. Okay, let me put in here a label, so label four. User name. This is gonna be the name of my input that I'm just going to write down at the moment. These that you see in your user name is just the text that you're going to see on the front end. And now I'm actually gonna put my input. So I'm gonna put in here an input input with the type off text. Okay? Because this is the tech is gonna be an important just for you to put some next with the name off. User name. Okay, let me see if I'm missing something else, and I wanna have a class. I think it's form control. OK, so this is another form control. This is another class from Bootstrap. And now what I want to do as well is I got the type. The name the class on and I think I have everything. Yeah, I want to have the user name yet. That's everything. So let's have a look now, how this is looking at the moment, So if I go to my register, Look, I got my register. These parting here on the top. This is my card Heaven. This is my card body. Inside. This is my label user name. And this is my actually inputs where I'm gonna typed on something. So let's copy these code that say's foreign group. Okay. I'm gonna paste it again. This time I'm going to say four. Use that email, which is going to be in here. Look, I'm gonna typed on this is user email. This is what people see on the front end. The text I'm gonna have now an input of the type off email, and I'm gonna have the name, which is gonna be user email. Okay, look, the label for these four needs to match these name. Okay? So the two things they need too much, and now we got the class of form control. This is fine. Let's copy these one more time. This form group, okay? And you put just some spaces in years so you guys can see what's going on. Okay, go. My form. Look, form. Starting in closing, I got three form groups. So one of them is the name user name eso user name, user email of the type email. And now I'm gonna have the password, so I'm gonna put in here four password, for example. I'm gonna call this one password if you guys wanna call it user password. The same thing you can. The type of these is gonna be an input with the type of password. So when you're gonna type down something, you don't see the actual characters. Okay, Let's just have a look. All this looking like Look, you what? They use their name. You got the email, and you got the hopes I didn't update this. This is not user email. This is user password. Okay, let me just see if this is fine. I think this is all good. So far, one thing that we need to put is a button down here, So I'm gonna put the button, okay? And these bottom is gonna be with the name off Limoges scoring here register. So I'm gonna put register. So button these buttons, I'm gonna put it with a class off Bt n TTN primary. This will make your bottle nicely style, like his blue me refresh. Look, register button. So when you click on this button, you wanna put this button as the type off submit. So you can Actually, if you put the type of submitting these button, you're gonna make this bottle toe, actually submit all the data from this form. Now, I'm also gonna do in here a name of register. Okay. I'm gonna put the name on these button So what this means is when you submit all these data , you can actually see if these button was pressed or not to send all these data. Um, so I think that's it. Look, I can't even put one more thing in here in all my inputs, I can put required. So if you put required, okay. You know, all these inputs, it means that you cannot submit the form unless all of these is, um, all of these is in year completely filled up. So look, I'm gonna try to submit. These is going to say please feel in this field, okay? I'm gonna try to do something. Look, please include the nuts, so I'm gonna put at email dot com. And now, if I'm gonna typed on some password, you can't see nothing in here because this is a password I'm gonna do, register and look. Something is still no working because I need to put the nat, and then you can submit the form. Okay, so I guess if I'm not wrong, we got everything in here now, working with this. Now, when we submit these all these data, what we can do is actually just going here on the top. Okay. And in our PHP. So as you guys can see when we submit our for we are submitting to these page to these register so we can grab everything in here on the top. So what I'm gonna do, first of all, is I'm gonna do anyth statement if OK is set. So this is set in here is a function from Beach. Be to make sure if a variable as some kind of value or not. So what we want to do in here is if is set, and I'm going to use in here these dollar sign post, okay? And I'm gonna explain in a second what the's does register. Okay, I'm gonna do in year if it's set. And now I'm gonna put this thing in year. Andi. Okay, let me explain what's going on in here. So whenever you load these page, Okay, you're gonna check this if is set. Like I said he set is a PHP function to check if a variable has some data or not, and you can check in here using these super global variable called the dollar sign. Underscore Post. This is a super global variable off PHP that can grab a variable by their name. So as you guys can see down here, this is my form. Okay, I'm going to see, like everything these form is submitting some data to these file called register dot PHP Vier the method of post. Okay. That's why we are using the super super Global variable of post. So this is checking. Are we sending anything to this page via post? With what name? Look on your button. There is a name of register. So if you are actually submitting this form via these button with the name of register Okay , we will grab all these data. So what I will do, first of all, is just going here, and I'm going to create a new variable called user name. He's gonna be equals $2 sign. Underscored post. Okay, so I'm creating a new variable called user name. And now we're gonna grab the value that you are sending from your form. If you remember, this is your input. Okay? So what are we sending from here? We are sending from here the name look. Name, user name. This is how you grab the value of these input. So I'm gonna put in here these new variable that I just called user name I want to grab from the dollar sign. Underscore post these value from this input with the name of user name. Look, user name. I'm going to copy it and put it in here. Okay, I'm gonna do the same thing in here now for for example, user email is going to be equal to dollar sign. Underscored post. Okay. And I'm just gonna put what is Look, my input in here of the type email and with name. This is the important thing. Guys, Name of user email. Okay, I need to grab these. So I'm starting it in these new variable. And like I said, we are using these dollar sign underscore Post because we are submitting our form through a method of post. If we're using other methods, like get or delete or whatever you would use thes other methods. So what are we missing from our form? We already grabbed the values from the name from the email and from the password. That's what we're missing. So I'm just gonna put in year password. If you want to call it user password. Call it user password. It doesn't matter now. I'm gonna do in year dollar sign underscored post, and I need to grab the post from my password. Look, this is my leave control. This is my input. And now look the name. This is the important part. Guys, The name is password. If the name was something else, you grab whatever is in here. Look, dollar sign in this car. Post password hoops. Okay, now, these is only available when you are coming to this page register page via post, okay? Because when you submit the page in here with a form, you go to these groups, you go to this page in here the same page via posed, so you can Onley make these work when you come to this page via post method. So after I grab all of these values from the submission of my form, I just wanted to do a Neco on the page to make sure that we are grabbing everything correctly. So I'm going to do a Neco. If you guys remember, the echo is just like printing on the page. Something I'm gonna put in here Echo user name and then I'm gonna put a dot Okay, This is two concurrent innate with some strings, so I'm gonna put a space, then I'm gonna concoction eight. These were they use a email. Okay, then I'm just gonna come coordinate with another space. And finally, I'm just gonna have in here my password. Okay? So let me just look, guys is better. He said, instead of you guys being, um, refreshing your page all the time. Just going here on the top, select the U. R l and press. Enter. Now let's sit. Meet our form. I'm going to create the user name like Telmo. I'm gonna do a user email of tell more. It could be my email, this method and user password. I'm just gonna put 123456 Now, look, if you press register, we should see all these values on the top. It means that we are grabbing all these data from our form. Let's see if it's working or not Register and look, I got the name I got to the email and I got the password as well. So this is all great now. There is only one thing in here missing at the moment, which is we need toe actually sanitize and validate all these data that people are putting on our website because sometimes people they can put some militia scripts or anything else , so we need to make sure that we are safe on these things. So what we can do is we should not grab these values just like these, because let me show you a little trick in here. Look, if I click on the URL, remember, guys don't just to refresh on your page, click on your URL and then press enter so you can clear these nicely. So on my user name, instead of putting my name, let's say I'm just gonna put if you guys know some javascript gonna put on alert, and then I'm just gonna put in here. This is a ah virus. I don't know. Just as an example, I'm gonna put on the user email the same thing in the past with another thing. Look, I'm gonna press register, and now look, this is actually coming like this, so this is fine. Okay, so we got you don't want to do these alert like I did, but you could do like this script. And now I'm gonna do alert I'm gonna do This is a my Russ. Okay, I'm gonna call these a script again. I'm gonna put some email. I'm gonna put some password. I'm just copy this. I think this is all working. Fine. Let's were register and look. I submit my form, and now all of the sudden, we are running in here a script. This script at the moment that you are running is just something like, not very problematic for your website. But as you can see, someone could put some scripts in your form and make, like, hack your database or your website. So we need to make sure that we are safe against these things. So what you can do is instead of doing these dollar sign post, what you can do is first of all I'm gonna do in year for my user name, I'm gonna do a filter Var. So look, just gonna do this Calment these out. So I'm gonna do my user name is gonna be equals to feel tha vai Okay, let me just see. So these filter far is just the way that PHP lets you just feel that all these data coming from the user So what do I want to feel that I want to filter these post? Okay, Post user name. This is the data that I wanna sanitize at the moment. And then I'm gonna do a filter. Okay? I'm gonna do Ceni tots on need ties, and then I can do string. Okay. There are many different types of sanitization that you can do, and you can do a quick Google search about thes. So what this does is just like sanitizing if someone put some sort of a string and is going to be like sanitize, So I'm going to show you in a second. Let me just copy this. Actually, I'm gonna go in here. Yes, fine. I can just copy this. I'm gonna put it three times. So this time I'm gonna put user email in here. I'm gonna do in here the post user email, Okay? And I'm gonna dio passwords. Oops, passwords. And now, same thing in year. I'm gonna put these in here. Okay? The first thing in here we are sanitizing a string. But the 2nd 1 in here I wanna sanitize email. Okay? There are many different things that you can sanitize. But this is one of the most like popular. You can sanitize integers for numbers. You can validate the emails and so on. Okay, so, no, we can After we did these kind of sanitization we can if you want to. You can still do another check. You could do something like this. If, for example, check if your user email. Let me just see what we have in here, Okay? So you can do, like, if fielder var okay, and I'm gonna put in here my user email. Then the second thing is, I'm gonna put filter validates email. I mean, just put in here. Okay, So what this does is in here, you sanity, You give the sanitization off the of the text that people are putting, and then you can run the fuel to revive once again these time, for example, for just the user email. Us can do whatever you want in here. And now we're just gonna validate to make sure that this is actually an email. So this in here is going to give you true or false. Is this email valid or not? If this is true. You can do something. Okay. So at the moment, I could just do an echo, for example, echo of, um of the actual use that email. Okay, I'm gonna put the user email I'm going to do again. The same thing this time. I'm even gonna put a script to make sure that you guys see that you can't do the scripts anymore. So I'm gonna put this so in here. What were on a boat? They use their name. Okay. And finally, I just want to put the password. Okay, let's just check these again. If this is working, select your l press enter to refresh. I'm gonna put some scripts in here. Look, this is a virus. I'm gonna put some email, baba blah, and I'm gonna put some password will put register and look what happened this time. We are grabbing our email all goods. If you try to put some sort of a script, he gets all stripped out. Your script, if someone tries to put the script in here, doesn't work. OK, so basically, why is it he's running to times are because I have to echoes? I just need one. So basically, that's it, guys, in year for the foreign validation. I know that I said that we would do the register now, but this video is just taking a bit too long. So right now we just grab all these data and everything. So in the next video, we will actually be able to send these to the database. Right? Guys, I hope you enjoy the Siri's and I'll see you in the next video. 4. Register Users with Hashed Passwords: All right, guys, welcome back in this video, we are actually sending some data into our database. We're gonna or I guys, welcome back. So in this video, we are actually going to register our users on our form. So last video is you guys remember, we were just grabbing all the data from here from this form. Now we're going to send it to the database. So the first thing I'm gonna do is after I checked in here after we grabbed all the values from our A form. After we sanitize everything to make sure everything is fine, I'm gonna check if my user email is ah volleyed user email. So I don't want to do these echo anymore. So what I'm gonna do in here now is I'm gonna start to create the statement. Okay? So I'm gonna doing here s t empty. So this is just like a new variable. I could call it anything, but usually on beach people call it statement. So what we're doing here is statement equals two PDO. Okay, If you guys remember, these PDO is a variable that is coming from our connection. Okay, Now, these PDO what we're gonna do is prepare. And this function in year prepare will allow you to do some sort of a my SQL query. Okay, you guys remember that To connect from PHP to the my SQL database, you need to do some kind of my SQL statements in here. So what I want to do is select all so to do all I'm gonna put these asterisk so select all from users if you guys don't remember users is our table. Let's go back in here to our PHP. My have mean where we see all our data basis, our database is logging project and I have a tables called users. Okay, so I want to grab everything. All the columns for my table users, Okay. And then I'm gonna do where and I'm gonna put in here now where email is gonna be equals to something. The email. If you guys remember. Look, this is my column in here on the top. So I want to see, like, all the columns for my users table where the email is equals to something is equals to what these user email that someone is putting down in here. So instead of putting these value in here straight away. I'm gonna put in here this question mark so we can use positional parameters. If you guys don't know about. This is just a way that beautifui make sure that the people they do don't do SQL injections on your coat. Okay? This is all about security, guys. You can always This is not part of thes course, but you can always do a bit of research on how to prevent SQL injections. And this is one of them. So right now what I'm gonna do after these, after I have this statement I'm going to do S d mt. And I'm gonna do execute. I want to execute to the statement. This is another. And this is another function from BHP itself. So I'm gonna execute my statement in here, And instead of using these question mark in here when you do the execute, you pass an empty array with the values that you want a passing here. Okay, So what do I want a passing year? I want to pass my user email from before, so I'm gonna put in here my user email, and now, after I do this, I can create a new variable called Total Total Users, for example, equals to my statement S t m d. And then I'm gonna do a row count. So basically, look, what I'm gonna do in here is at the moment, I'm just going to do and yet an echo. Okay, Echo total users. So I'm just gonna explain you what's gonna happen in here. Just gonna put in here the break tag, and I'm gonna put this okay, I'm gonna put the break back like thes. So basically, let me explain what's going on in here when I'm in here on my form, I'm submitting all these values from the user name from the user emailing from the password . If you guys remember, we are grabbing it in here on the top with these variables, like I cover before we are sanitizing all the data. Then I'm checking inside of these if statement checking if my user email is valid. Okay. We already sanitized all the data from before, so we are all good. First of all, I'm creating a new variable called the statement. We usually call it like this in PHP. And what we're gonna do is grab the PDO that we just created on our connection and I'm gonna prepare my SQL statement. What is my SQL statement is I'm going to select all the columns from my tables users where the email is going to be equals to something I want to check. First of all, when I'm trying to register someone. If there is already someone on my database with this email that they are just trying to register because probably you have seen this somewhere already when you are trying to register your email somewhere, if if your email is already been registered there, you can't register again. So that's exactly what we are doing. I have this question mark in here because we are using positional parameters from PHP. You don't put the value in here straight away like thes user email. Just gonna show you she's all about security guys, OK, you can do a bit more research into these. If you would like into these positional parameters now, how can I put the value in here? I can just run my statement and we execute. When you do these execute function, you can actually pass an array with the values that you want you will see later on that. If you have more of these question marks, more of these positional parameters you can put inside of these array more values. At the moment, we only have one. These user email is going to replace this question mark that you have in here After you run this, I'm creating a new variable called Total Users. And Total User is going to be equals to your statement that you just run in here from before and we're gonna run these row count. So if that is already someone in the database with these email, it will return. Is one, For example, for these total users, If there is no one on the database with these email that you are trying to register, he's gonna come up s zero. OK, so let's try these out now. I'm going to go in here, put user name Thomas a bio. I'm gonna put these user name is my user name, and I'm gonna press register. Okay, so a two moments we are going in here, Let me just see what's going on. Um, I think I'm on the wrong place. I think I'm on the or wrong place. Let me just seen year. We should be on the HD dogs and I think is ah, logging project. I'm on the wrong. Sorry, guys. I was just doing this one was, like my other project that I just did before these. So I think his logging project let me going to register. I'm gonna do the same thing again. I'm gonna do and look are not sure if you guys can see, so I'll try to make some zoom zero on the top. So it means that you are trying to add this person inside of the database, and there was nothing in there. Okay, so this is my, um nothing still went to our database because we still given up anything. Okay, so let me just reset my zoom in here. Now, what I want to do is after I did these, let me just comment these echo users. I'm just gonna do this if Ok, okay. If my total users is greater than zero, OK? It means that is already someone on the database with this email that you are trying to register. I'm just gonna do an echo, okay? And I'm going to say email uh, already being taken. Okay, so I can just say email already been taken, and I can put just a break tag as well. Okay, so this if my total users variable comes up is more than zero greater than zero. It means that is something else in there else. What we're gonna do is I'm gonna go in here. I'm gonna do my sq pell. Oops. So these SQL variable is just gonna be like this statement you could do straight away the same thing. So maybe maybe we're going to do the same thing straight away. Okay, I'm gonna create another statement. OK, look, guys, I'm using the actual the same name is appear, but this method, okay, because these statement, we just run it once. If you're gonna run another statement now with the same name, you are just gonna overwrite it. So that's all fine. But if you guys prefer to give it another name like statement to is fine as well. Okay, So what I want to do in here is I actually want to do a statement that say's toe insert things into my database. So the statement for that is I'm gonna do insert. By the way, guys, look, all these my SQL queries that we're doing usually is a good practice to write them on capital letters. So, you know, this is a SQL querian here, So I'm gonna do a insert into users, which is our table, OK, inserting to users what I want to insert into my users. Okay, I will insert in here. My user. Uh, sorry. Insert into a user's. This is the columns on my database. So let me check in here on my user stable. I'm gonna go into structure if you guys remember. Look, we have a name, email and password. This is all the things that we have, the names of our columns. So this is what I want to put in here. Insert into users into the name, email and password. Okay. These out of my columns from the database. So what do I want to put inside of my, um, database? I want to put some values which I'm gonna put in here values. Okay, I'm gonna put some values. And what are the values that I wanna put? I want to put these values that we just grabbed from our form so I wanna put user name. OK, look, guys, these needs to be the same sequence. If I'm putting in here on my users table on the name column on the database, I want to put my user name that comes from my form. So it needs to be the same sequence. The second thing I'm gonna put is my user email this one up here. Okay, I'm gonna put it like this. And the 3rd 1 I want to put the password. Okay, I'm gonna put the password in here, so I need to put all these values. Let me see if I'm just forgetting about something in here. So I got my prepared statement. I think everything is fine. So let me just get rid of this statement to like I said, guys, you can use the same name because basically, you are over writing these that isn't here on the top because has already been done. So it doesn't matter if you're going to use the same name again or not. So after you do this, what you gonna do? You gonna do another statement toe? Execute this So to execute these, what we're gonna do is statement execute. And now, look these values in here, like I mentioned before, they should not be put in here directly because of security issues. Toe prevent SQL injections. So what you can do instead of having these values in here, I'm gonna cut them off, and I'm gonna put a question mark. So the first value in here is the user name, So I'm gonna put in here on my execute inside of these. All right, look, that is in a rain year. Guys. Can you see? I'm just putting some spaces so you can see Well, the first volume that question mark is gonna be my user name. The second volume is going to be what? They use an email. So I'm just gonna put another question mark in here End The third value is gonna be my password, which I'm gonna grab. And I'm just gonna put a question mark as well. Okay, so now that you will execute these, these should actually go into your database. Okay? So let's just see if this is working. First of all, so I'm going here, let me refresh my page. And sometimes when you refresh your page, it doesn't work. Well, just click on the U R l on the top and just press enter. Um, I get some error on my line 22. Let me see what we have wrong. I think I forgot the semi colon in here from these eco. Sometimes if you meet some semi colon, it might give you something like this, right? Let me refresh. Lynn, put on some bio. Let me put this email, which is my email. I'm gonna put some password and I think I put 123456 for the password register. Okay? And now it doesn't show you anything at all. But hopefully we send all our data into, um into the database. Let's just refresh. Look, I'm inside of my user stable. And now if you click on the top brows, can you see my my cursor? I have one person with the idea of one which is the first person that we are registering. We are having a name in here of Telmo some pie, which is my name that I just put on my form and we have the email Telmo a some pyre at gmail dot com and we got the password. So all is working Fine. Now, there is one problem in year for security reasons, you should never store on your database and the passwords of your users completely like these without being encrypted without being hashed. So that's what we're gonna do now, By the way, before we do this, let me just show you if I try to register again with these email, okay? Because we just cover these from before. Okay, let me do these When I refreshed my page when I try to registered again with the same email and look what's gonna happen when a press register and on the top look gonna do some zoom so you can see email already been taken off course. You guys can do some sort of other thing down here, which we will do later on. We can even do it now. I guess so. In here. Ah, we can put in here. So instead of doing this Yeah, I'm just gonna doing here if ah, total users equals greater than zero. I'm just going to create a new variable called, um water. Ah, email taken. So these email taken is going to say the same thing. So email already been taken. Okay, so I'm just gonna put thes message in here. I'm going to get rid of these eco for now. So remember, guys, these only gets set up these variable if the total users is greater than zero. OK, so I'm going to go down here and just under the user email, okay? Maybe just ah, that's fine. Under the input. So what is it? My bus word. My user email. So in here, I'm just gonna put a brake pack like this. And now I'm gonna put some peach being here. BHP and I'm gonna put a, um, something saying, like if message, where is it? If email taken, this is the variable. Okay, if this is true, Okay, I'm going to close this. And now I'm gonna put the paragraph, for example, with the message someone adobe hp again. Okay, I'm gonna close my paragraph, and now I'm gonna cause my if statement, So I'm going to do Ph. B. I'm going to close the if statement, and that's it. Now, inside of these PHP, I'm just gonna do echo. And now I'm gonna do echo the email taken message. Okay? You guys can't even style is much better than me after for sure. So I'm gonna just refresh thes. We got some error online. 52. So let me see. I I put Echo, but I didn't put Echo of nothing. I want to put echo of this. Okay, let me just refresh the page again. Um Okay, that is email taken. So is online 51. So instead of if I'm just going to set if is set. Okay, so if is set now, these should cover it. Okay. I'm gonna try Telmo. I'm gonna put some password I'm gonna do register. Look, email already been taken, so it looks of it more professional. Okay, So you can give you can even style these with some I could put in here maybe, like, ah style. And I'm just gonna put, um, background cola reds, and I'm gonna put the color to be Let's find the color. Okay. I'm going to try one more time just to see if this is a little bit better, but register. Look, email already been taken. Let me just look. You can see the error straight away. Cool. Okay. Well, now that we take care of these, you can go in here on the top in. Let's just hash our password. So how do we ash our password? I'm going to go in here on the top and, uh, what's what's going here After I after I sanitized my password. What I'm gonna do is, um I'm gonna call these bus. Sort of hushed passwords, hushed, for example. I'm gonna put these equals two passwords. Passwords hush. Okay, this is a function from PHP, which will hash your password. Okay, so the first thing you wanna put is your passwords. Okay, These variable that we just send it ties and everything. This is what I want a hash. Okay. And then I'm gonna put in here this sort of hashing that I want to put in here. I want to put the default, OK, so as you guys can see, I'm going to give you an example. Imagine that You ever safe at home, okay? You ever safe at home when you can put your keys or your money or something? These safe is obviously like enough safety to for you to have your money or something. Imagine that you put these safe inside of another safe, and that's safe inside of another safe. So now you tell me when is it secure enough? When is it inside of 10. Safe's or what? So is the same thing in here for the password hashing these password default is going to be How many rounds are we gonna hash our password? So the password by default if I'm not wrong, is like 10 rounds. So basically, this is it. Enough a safety for our password. And as you can see, this will work fine in a second. Now these passwords that is already hashed. This is what I want to send to my database. Okay, so down here, when we are doing this statement of execute instead of passing the regular password, I'm gonna pass the password hushed. Okay, Now, all of these should be working. Fine. Let me just refresh my page. I'm going to register a different user, John, and I'm gonna put in here John at email dot com. And I'm gonna put another password. 123456 I'm going to register. Okay. And now if I go into my database, I'm gonna refresh going to do some zoom. Probably you guys can see better as you can see. Now I have John Jonah email and look at the password now all hushed. This is what we were looking for. Okay, So now, even if someone gets access to our database with hopefully not because we are building it's secure enough. Nobody will. Will manage to toe decrypt these things passwords anymore. So that's it for these video guys. We already learned how toe registered a user. How toe make everything secure. So in the next video, we're actually gonna learn how we can log in a user. One more thing I forgot to say, Look, all the users that they register by default, they will have a role of guest. Okay, so, yeah, that's it for the visit. Guys, I hope you like it, and I'll see you in the next one. 5. Login & Logout users with Sessions: All right, guys, welcome back in these really? We're gonna learn how to log in a user. So for that, I'm gonna create in here a new file. So I'm gonna create the new file called Log in dot PHP. And these fire logging dot PHP is gonna be very similar to the 1st 1 in here of the register. So what I'm gonna do is I'm just gonna copy everything from my register. I'm gonna do a command A to select everything, and I'm gonna do a control CR co Come and see. Copy everything. Put it on my logging dot PHP. Okay, Now, let me just go in here on my index. I think I don't have any. Not, but at the moment, let me just see. I don't remember now. So go my log in. Yeah, if you click on the log in is still going to the logging. So in here on my log in page. First of all, when you do a log in, you don't need to pass a user name, okay? You need to pass on Lee. The user email and the user password. And in here it does not say register, but it says logging. Okay, so down here, where is this bottom? We it say's register. I want to do. Look in this is the text that you see on the front end. Then I'm gonna also put Let me see. The name of these button is going to be log in. So when you were going to submit this form, we can just grab these buttons to see if we are submitted or not. These form Look, this time is gonna be also on a method of post, but we want to submit this form to the same file that we're doing at the moment, So I'm gonna put in here log in dot PHP. Okay, Now, this should be all goods. I I want to get rid of these Deve in here with the user name because we don't need it. Okay, When you do a Logan, you don't need nothing of this. Okay? So I'm going to save these, and I think it should be all good. Let's just refresh our page. Look, I'm on logging dot PHP in here on the top. Okay, look, this is this is the nub bar that we did in the beginning guys, if you don't remember, just increase the zoom in here so you can see we got home, We got register and we got log in. This is the log in page. We're just creating right now. Okay, Now let's grab whatever people are typing down in here. Okay? Lets grab it on the top. These coat, like I mentioned guys, we just did the copy of our register dot PHP eso weaken. Just edit these now on the top. First of all, I'm gonna check if is set dollar sign and the score post. This is our super global variable. This time I want to make sure that we are posting these button with the name of log in. Okay, these button with the name of logging. So when you submit this form to these file itself, I want a gravity in here on the top and make sure that this has been posted. That's why we are using these dollar sign underscored boast. If it was something like thes I want to require my connection to my database. Okay? I'm just clean up all the stuff that we don't need. First of all, we are not grabbing any user name anymore. Okay, We are no grabbing any user name anymore because we just have the user email and password. So I have already in here a user email that I'm grabbing from the from the form itself. I'm grabbing the password. I'm also sanitizing it. Okay, now, this time I don't need toe hash the password. I'm going to show you how you can compare one thing with the other. So let's just get rid of these. What I want to do in here is let me just check what I have in here right now, so I'm going to check. I'm gonna do a first of All right, delete this and I'm gonna delete all of these things for now. Maybe not delete everything. I'm just gonna comment and we're going to see what we need to use. Okay. After I get all of these, the first thing I'm gonna do is I'm gonna do, um, s TMT. Okay. A variable statement that we are calling these is going to be equals to the PDO. Okay, we're grabbing these from the our connection to the database. I'm gonna prepare SQL statement, which is going to say select all columns from my table Users. Okay. Where? Okay, the email is gonna be equals to something where the email is going to be. Where? Because imagine that you are trying to log in with an email that does not exist in our database. We need to check this. Okay, So I'm grabbing all the users for my database. Where I'm was, like all the columns from the table users where the email is gonna be equals to whatever you are typing down. Then what I want to do is execute it. So I'm gonna do STM t. Then I'm gonna do a execute to execute these statement. The execute like you guys remember, takes an array like this. And now I'm gonna put the value that should be in here on this pushes positional parameter . So I'm gonna put in year user email, okay? This is what we grabbed in here from the top. If you guys remember, okay? No, after Oops. After I execute this, I'm gonna do in here user, which is a new variable. I created these variable now called User, but could be anything else. Now I'm saying use it equals to my statement and then I'm going to do French. Okay, so now doing these like these at the moment, I can actually get access to these user. If it exists on the database, I can access things like these. Look, user r o I d like these. Look, I can do something like these. User, I d. Okay, I could use to grab these one in here. I could do user name to grab the name of the person like this user name. Okay, I could do use their email, like, thes to grab the user email and so on. Okay. So we could actually do these and let me just show you one thing. We're not going to do these right now. I'm just give you an example on our conflict db dot PHP Do you guys remember when we did these codes? PDO set attributes. And so one we are. Actually, when we leave this coat was to make sure that we can use these object orientated way. You know, when you are using these arrow is just to make sure that we can access it like that. If you didn't do it like that, you would have to access things like this. Look, user zero these user zero. It would be your first column of your database, which is thes i d the user name. It would be something like thes instead of user name. Like thes you would have to do user one. Now you tell me which one is it easier to grab? Is it better to do user Ah, arrow and then named to grab the name from your database or disease user? Or is easier for you to just do user one, You tell me. Okay, so that's why we did that. Let me just get rid of these stuff. So this is what I just show you. Now what I'm gonna dio after I grab these user, if it exists on the database, of course we're gonna do these. I'm gonna do if Okay, I'm gonna do in here if ah is set. Okay, is set user Okay, So you're doing in here if is set user because you need to make sure that these users actually exists. If you are trying toe grab something from the database that does not exist. Uh, this will not work. OK, so I'm gonna do is see if is set. I'm gonna check if passwords dollar sign. Very fight. Okay, This is a a PHP function that is going to check the password that you are putting in at the moment with the password that is hashed on your database. Okay, so this is what this function this this password verify. First of all, you're gonna pass in the password that people are typing down on the log in which we grab up here with these volume password. And the second volume that you pass in here in this password verify is going to be what user? Look, these variable that we just crafting here. And as I told you, you can go on to that user from that email and grab the password from the database like thes. Okay, these will grab you whatever you have in here on your user itself. Okay? Eso Now we got the if password verified, if this is true, Okay, these if statement is going to give you true or false. So if it's true, what we're gonna do is I'm just gonna dio um, echo the past. Word is correct. Okay. Else. Okay. You want to do something like, um echo the log in email or passwords is wrong. Okay, So you don't want to tell the user if the email or the password is wrong, OK? Because if you say it, it will help them hack your system. You don't want that. Okay, so now that this is all done, it should actually log in the user. So let's just check if this is working or not. So I'm gonna go in here on my page. I'm going to go into my registered, registered a complete new user. I'm going to call this one, um, Tom, for example. So this is gonna be tom at email dot com and now I'm gonna put 123456 for the password. I'm when I registered it. Now, we should be all goods. Let's go in here into my local host, my peach. We might have mean I'm going to refresh the page. And look, I got to Tom Tom at email, and I got the password. Now, let's try to log in with these user in here, so I'm gonna do log in. I'm gonna do it, tom at gmail dot com. And I'm gonna do 123456 And when I click log in, it looks like we have a nearer online 11. So what's the problem in here? So let me see. Oh, I see what's wrong in here When we leave our SQL query, we said select all but we for what? The From from users were email because to something Did we do the zone? Our register? Yeah, we got the from Okay, so I don't know. How did I forget these? So let's just in here. Try again. I'm gonna do Tom. 123456 I'm going to log in and look on the top. The password is correct. OK, so we were actually just grew a bit. Will assume we're actually checking with this password in here. And they were matching. Okay, So now if they were actually matching the passwords, what we want to do is you can have two ways of keeping your user logged in on your website . You can either use sessions or you can use cookies. There's some people that they say they prefer sessions. Some people say they prefer cookies. Cookies. They are not so secure. But it doesn't matter. you can use any that you would like. What I'm going to use in here for the scores is gonna be the sessions. So in here, if the password is correct, what I want to do is I want to start in here a new variable called dollar sign. Underscored session. Okay, so these is a super global variable that will allow you to start variable with whatever name you want. So dollar sign in this course session, This is a super global variable. And now I'm going to give it the name. I'm going to give it the name of user I d, for example, and this is gonna be equals to what this is gonna be equals to the ivy of my user that I'm trying to log in. You guys remember on our database the ideas, they are unique. So if you are trying to log in a person with the idea of something, that person is just unique. Okay, so in here, I'm going to start a new variable from sessions called User I D. And I'm gonna put in here user like this, I d. So I'm starting these new variable. If the password is correct. OK, so that's it. Now, in here where it says the log in or email password is incorrect. We can do something very similar to what we did on on, um, on here on the register. So I'm going to copy these. Basically, I'm gonna put these in here, and I'm going to say you said if you will take and I'm going to say logging wrong, okay? And these Logan Wrong. I'm gonna put thes message, okay? I'm going a just comment thes out down here on my form. Now, let me just copy my coat from my register, which with the I think we use the same thing. So yeah, so if iss set and I'm going to do in here what? Log in wrong? I'm going wrong. Logging it doesn't make too much sense. Wrong log in. Okay, this is gonna be my variable that will only exist if the logging is wrong for some reason. So in here, I'm going to do if is set wrong. Log in. I'm gonna do a peach Peter in here with a background color of red, and I'm going to dio the message that they have there on the top the wrong logging guy. So in here. Why the hell did I put these in here? They should be fine. Okay? I don't know. Why did I put these on my registered as well, Guys? Just get rid of it. You don't really need it. So that's it. Let's try it again with the wrong log in this time. And when it trying here with Tom and I'm gonna do 12345 now instead of six. And I'm gonna put seven. I'm gonna put log in and look, the log in email or password is wrong. Now, if I make everything fine, all the Loggins, What I want to do is let me go in here on the top. Eso I'm on the register. I want to go to the log in. So if everything goes well, okay, you start the session variable called User I d. I want to redirect my user from the log in page that we are at the moment to do a redirect . You can use these, Heather. Okay, Now I'm gonna put the location like this, so heaven location. And now I'm gonna put some colon. I'm gonna put the location where I want to send my user at the moment because we are in here in our local host. I need to copy these local hosts. So I'm gonna go to my home. This is where I wanna loads or redirect my users. So I'm going to copy these your l on the top, and I'm gonna baste it in here. Okay, so Hever should redirect us to them, to the home page. One thing that I would like to tell you guys is when you start the session variables, there will be all the time available in your browser until you close it down. OK, so you can navigate through pages and your user will always be logged in with these variable of dollar signs. Session user, I d. So let's try to log in and let's try to see if we are going to the home page or not getting redirected. So let's go to the log in. I'm gonna go in here in Tom. I'm gonna put the password. 123456 I'm gonna do logging. Hopefully we go to the home page, Blufgan, And look, we went re directors to the home page. Cool. So now that this is fine one thing that you can dio is let me go to my index dot PHP on the top on the top in these PHP tag that we have initially just for the requiring, Um, the connection. What I want to do is, first of all, if you want to use sessions toe, grab some sessions that that already set up on your server or not, you need to do this. Session underscored stopped. Okay. In order for you to use sessions and toe grab them. You need tohave this coat on the top. Session start. If you don't have these, you can't grab sessions or anything in here on our log in page is different because we are just creating a new one. But if you want to grab some sessions that already created or something in some pages, you need to start to the session, start on the top. Okay, so you start in your session, then what you gonna do is you're gonna do a NIF statement like this if okay is set. So if you guys remember these east sets function from Beach B is setting if a variable is set or not if he exists. So what is the variable? I'm gonna check if it's set or not. I wanna check my door sign Session underscore session with name off user. I d Hoops user I d small letter. Make sure that you guys got these name the same one, like in here. Look, when you are creating the actual variable. Now, if this is true, if this is set, okay, What I want to do is first of all, require my connection to the database, so I only want toe require my connection to the database if the user is actually logged in . Okay. Now, what I'm gonna do is I'm going to create a new variable called User I D. And this variable called User I d is going to be equals to my dollar sign on this course session. User, I d Okay, these variable that we just created. If you guys remember when we did the log in what should be the value of these variable the dollar aside and the score session user, I d should be Whatever the value of the idea of your user, we're trying to do these with the user Tom. So his i d is three. So they should be the value. Okay, back to my index dot PHP on the top. I just created these new vegetable called User I D. And now I just want to do let me just seen here. I just want to dio, um, echo of this on the top just to make sure that this is working. So I'm just gonna concoction eighth ease with a break back to make sure we can see it. Well, cool. Let's go to our index page. Let's refresh and let me see, um, if he set, Okay, so at the moment, this is not coming up, but, uh, let me just try to log in the user again. So 123456 I'm gonna do a log in. We are coming in here. Let me just see my my So something is actually not working in here. Let me see. What is it? We got the log in, so I got this. Okay, I'm gonna put the same thing in here. I'm requiring my connection. I'm saying user, and I'm then doing this, Okay? So let me just refresh the page Let me see what's going on here. Oh, I see what might be the problem on our logging dot PHP. We also need to have these sessions start on the top. So I think this might be the problem. Let's just try it out and see when have refresh my page. I'm going to go to log in. I'm gonna go, Tom. 123456 Log in and I'm redirected to the index dot PHP with a three on the top, which is these eco. Okay, we are all good so far. So what I'm gonna do right now in here is I'm gonna go in, look through Ah, a statement on my on my database. So if you guys remember s TMT for the statement, I'm going to do where PDO Okay, I can do this because I just required on the top in here. If you guys remember these user i d in here is what? Three? This is what's coming from my session. Variable. Now I'm gonna do a statement. PDO arrow in here. I'm gonna do a prepare. What I'm gonna do in here is select all columns from the user stable where Okay, Where The email. Let me just see. Um not not where the email where the i d is gonna be equals two something. So I want to grab the user from my database. All the columns. So the column for I d name, email, password and so on On Lee for the user Where the i d Easy close to something. What kind of I did what I want. I want it. This one from the user that is logged in. Okay. Now, as usual, I'm gonna do my statement. Execute s TMT. Okay, I'm going to do the exact cute. And what? We're on a passing here in this question. Mark, I want to pass in here and right. Okay. Let me go in here on a passenger wonder right with these volume that I want to put in here with is this'll user. I d Okay, now that I got these I can do, I can create a new variable called User, for example. But you guys could call it something else. Using equals to my statement as TMT, and I'm gonna do a fetch function. Okay. Now, with these user variable, I can access the I D the name the email of these user. So the first thing I'm gonna do in here is Okay, I'm gonna do in here if Okay, let me see. I need to do these still inside of these if statement. Okay. So if the user okay like this, that user role is gonna be equals two. Guest, which it is. If you guys remember, I'm going to say that I have a message, Okay? Saying something like your role is a guest. Okay, which it is at the moment, so I don't want to do anything else. So this is my message now, down here when I have these. Welcome gassed. Um, please log in tow register or whatever. So what I'm gonna do in here is let me see in here. What do I have? I just need to check if the user is logged in or not. So let's do some peach being here before these h five. Welcome. I'm going to do a PHP tag in here, so BHP to start, then I'm going to say if is set user. Okay. So if you guys remember in here from the top, these variable user on Lee gets set only get started if we are actually logged in. So I'm gonna do if is set these variable user. Okay, let me just close these if statement, Okay. I mean, put in here this So if this is set, I wanna have in here. Welcome. So I'm gonna put welcome. And instead of saying, Welcome, guest, I want to do Welcome, BHP. Now I'm gonna do echo user name. This is how you can grab the user name of these user, which, if you guys remember, look name. You should grab the name of Tom, for example. So else. Okay, So if the user is not logged in and these variable user is not set, what do I want to do? I just want to say welcome, guest. So I'm gonna put Ph be closed this and just close my l statement. Now, we put this parting here for them. We put this party in here just for the name. But now, in hearing these message when it say's, please log in to register toe on local the content. I'm just gonna pace these the same thing. Look, let me just undo this. I just copy this part of the codes. When a copy, it command C and paste it in here. Now, where did say these h four. This is gonna be if someone is a guest. Okay, Please log in. Register toe. Unlock all your content. If the user is actually logged in, I want to give it another message, which I have in year. Let me just see. Where is it? I'm gonna copy this, okay? I want to put this in here. This is a super secret content on Lee for logged in people. Okay, lets test if this is working or not at the moment when I refresh the page. Look. Welcome, Tom. This is a super secret content on Lee for logged in people. Okay, but at the moment, guys, look, we still have these home register and log in, OK? We don't want tohave these if the if someone is already logged in. Okay. So what you want to do is in your hever somewhere in here. Where is it? Includes in your heather. You need to check as well. Something like thes. So I'm gonna copy. Where is it? My index of beach be. I'm going to copy this. Let me just see if everything is fine in here. Because this is what we're gonna do. Oh, yeah, is gonna be the same thing on my heather dot html on my heather dot html. I can just put some ph b in here, which will dio like this. So if the user is set, it means the user is logged in. What I wanna have is I'm gonna have index dot PHP and I'm gonna have a I'm gonna have an index dot PHP, which is my home. And then I'm gonna have, um, probably a profile page which will create after this profile, and then I'm gonna put in here, prove file. So this is if the user is logged in, If the user is not logged in, what I wanna have is just the register, okay? I don't have the register and the Logan, so I congratulate of these. Oh, wait a second. I want my home, Toby all the time available. So let me just put these outside of my PHP statement, so I also want to have a log out. Okay, So if the user is logged in, they can access their profile. Oops. They can access that profile, they can also lookouts, which we are going to do in the next video, guys. Okay, so I'm gonna create another file for it called Look out the PHP. So let's see if this is working so far, it's going to refresh my page. Okay, Look at the moment. Because these guy Tom is logged in, can go home profile does not exist and log out does not exist. So they use it at the moment. Is all the time logged in? This is not what we want to want to allow them to, um, to log out. So let's just do it now. Quickly, because this is very, very straightforward. I'm not gonna create one more video just for it is like two minutes. I'm going to create a new, um, file called log out dot PHP. Okay, now, in here, I mean, just seeing here. What I want to do is when I'm gonna go to the log out page, I'm going to start a PHP talk. Close the PHP, doc. Now I'm gonna do a session underscore, start. So we can we we are able to work with sessions, OK? Always need to have these on the top, guys, if you need to. Now I'm gonna do in if statement Okay, if is set. Okay. My door signed. Underscore a session. If you guys remember, the name of our session is user. I d like this. So if he said what I want to do is session underscored, destroy? I want to destroy my sessions. So if all my variable sessions are destroyed, that's it. We are logged out. So after. So when you click to go to this page, log out dot PHP. This is the only coat. So we get in here, we start the sessions. If there is a session with these name of user I d. We are going to destroy it. And after, we're gonna redirect the person to the home page again, how do we do a redirect? You do hever and then you do location. And now, if you guys remember, just copy the u R L up in here on the top. Just paste it in year and that's it. Let's save it. Let's try these out. No, refresh our page. We got welcome, Tom. This is our current user that is logged in. This is a super secret content on Lee for logged in people. It means we are logged in. I'm gonna click on the profile. Doesn't work yet, but if I'm going to click on the log out look. Welcome, guest, please. Logging register toe one local the content. Let's try to register one user quickly. Okay. By the way, when we registered a user, I also want the user to be redirected to the home page. So on our register, after you register the user, I'm just gonna read that act the user as well to the home page. Okay, let's registered another user. I'm going to call these, uh, tell move more the Reiter, which I'm gonna I'm gonna explain in a second because, um, I want to create a moderator after in the in the next video. So I'm gonna put in here Telmo more the Reiter at gmail dot com And let's put email. I always put email. I'm not sure why I put Gmail password. 123456 You guys can put any password that you want register. We are redirected to the home page. Let me refresh my database. Make sure that my Telmo moderator is in here. It is, as you can see, with the role of guest. Now, let's try to log in. I'm gonna go into my log in user email, which is Telma moderator at email dot com I'm gonna put in here user password. 123456 Log in and look were redirected to the home page. We don't see the logging and registered anymore. Look. Welcome, Telmo. Moderator. This is a super its secret content Only four logged in people. And if I do a log out, we are logged out. So that's it, guys. That's it for this video. And I hope you like it. I will see when the next one. 6. Edit User Profile: All right, guys, Welcome back in this video we're gonna learn how to build our profile page where we can change some details about our user. So let's get started with this. The first thing I'm gonna do in here is just create a new file called maybe profile profile dot PHP. And inside of these, I'm probably just gonna go and ah, going here on my register, I think I'm just gonna copy everything in here, and we will see what we can change. Okay, so I'm just gonna see, like, everything from my register, and I'm gonna paste it inside of my profile. Okay, Now, let me just see. What do I want to do? I want to make sure that these partying here is what I want. So I'm gonna have in here container cards I already have in here something that is going to say updates yard details. Then I'm gonna have in here a form that's going to go to profile profile dot PHP with a method of post. Now, one thing that I want to mention in here is whenever you guys are going to be using, like, a real server, that you're gonna put your application, you should use some different methods like put or patch that well, actually update things on your database at the moment because we're just running these on the local server with Samp. Some Bassem kind of problems using this kind of http methods like put and patch. So for now, we're just going to use these Post. Like I said, Guys, if you want to use on a real server, you should use a method of either put or patch, so just going to give you a quick description. The port basically is going to create something new if you don't have it or updated if already exists at the patch is just gonna update a small part of your database So you guys can have a looking to Google. What's the difference between put in parts? But you can basically use any of them for the situation, right? So let's get on with the Post. Like I said, we're going to use the post because examples some problems with the put and the patch, so they should be fine for us to use. Now, in here, I'm going to use these user name and then I need my user email. Okay, So I'm gonna have my user email the password I don't want at the moment, these would be for another project. For now, we just want to update our user name in our user email. And now I'm gonna put in here the butter name is gonna be Have it. And in here, we should say, let me see update the details. So updates details. Okay, Let me just see if this is working. I'm still logged in from the last video. If you guys remember, it's Telmo moderator, which was the last user that we actually, um, just built. So if I go in here on the top and I go to profile now, I can see my profile. But what I actually want to do in here is, um I want to go in here and just, like, put some names I want in here when I go in here to see my actual user name and my user emails so I can actually update it. So let's go back in here. So now let's go to the top and just modify these a bit. So what I want to do now is make sure that we have on the top. If you guys remember from our log in, I need to make sure that I have my session start. Okay? In the beginning. So where is it? I'm gonna put my sessions start on the top. Okay, Now, I'm gonna put this session on the top, and then I'm going. Also gonna do another check in here is if Okay, I'm gonna do is set. And what do we want to check if he sets my dollar sign on the score session with the name off user, I d. So basically in here, we're just checking if the user is already logged in or not. Okay, so if it is, I wanna require my connection to the database, and then I'm going to create a new variable called User. I d Don't forget to put the dollar sign, because in PHP we need to put the dollar sign before our variables use. The 90 is gonna be equals $2 sign underscore session, just like we did in the last video. If you guys remember user, I d. Okay, So I think I forgot to put something in here. Some single quotes Okay, Now I know that these variable that I'm just creating at the moment is gonna be equals. Two, maybe 234 10. Whatever is our user. I d Now, another thing that I can do in here is to check for example. Um, what can I do in here at the moment? I don't want to do nothing of these. I just want to know if the user is logged in. Okay, I'm going to create a new variable called S TMT. Short for statement, and I wanted to do at PDO. Okay, he was Remember PDO I always said these old videos coming from our connection from the database that we did before, and I'm gonna do in here what I'm gonna do a prepared statement and my SQL statement that I want to put in here is gonna be, um, select all columns. That's why I have this starting here from my table users where the i d is going to be equals to question Mark using some positional parameters in year. Now, after these, I'm gonna do a S t m t, and I'm gonna do with eggs acute. Okay, so I'm gonna execute what I'm gonna passing here instead of these question. Mark, If you guys remember, I'm gonna pass these user I d that we just grabbed from before. Okay, Now that I have these, I'm going to create the new variable called User. Okay, equals two s t m t. And now I'm gonna pull STM t, and I'm gonna put these fetch. Okay? Now, I have access if the user is logged in. As you guys remember that we were doing before. If the user is logged in, I actually now have access to all the values of these user. So now in here, on my form on my inputs for the name in for the email I'm gonna do in here something for first, for the name. Okay. Having input in here, I'm gonna put that by default. The value he's gonna be equals two. And I'm gonna put ph be okay When you start the new peach pit, our guys don't forget to close it straight away. So you don't forget. Now, I'm just gonna do echo and I'm gonna the user. And then I want to go in here and put the name you guys don't remember. User name is on our database. Let me just go back in here. I'm gonna go in here. Look, this is the column name. That's why I have access to it. Let's just see if this is working. I'm going to refresh my page and look, Now it's coming. The name of my current user that is logged in on these websites. So we got Telmo moderator. Let's go to my database. I'm just gonna do a bit of zoom so you guys can see Tell Moderator is my last user that I just created. And this is the name. Now I'm going to do the same thing in here on the input for the email. So import require type email name, and I'm gonna put I'm just gonna copy these. Actually, when a copy these value and I'm gonna put on these input from my email this time instead of putting name, I'm gonna put email. Okay? Email is the name of this column in here on the database. Great. I'm gonna save these. And now I'm going to refresh my page. And look, I got tell moderator and I got ah, tell Mom. Tell moderator the name Telma. Moderator the email. Why do I created these user? Because if you guys remember, I want to create after a blawg just for moderators to be ableto post on the block. But these video right now is just about updating the details about any user. So now that we have these, when we actually do some changes in here and we submit this four look with the name of of it, Okay, What I'm gonna do in here on the top is I'm gonna go in here to the top. I'm gonna do in here. First of all, I'm going to check, um, before all of these Ok, I'm gonna do anyth statement saying something like, if and then I'm gonna do door sign post. I'm not gonna do register. Of course I'm going to do and it because if you guys remember, look down here on these, but and I gave it the name of edit, and I know that these four is going to submit to these file and via the method of post, So I'm going to do in here on the top. Look, So after we check if the user is logged in and everything, um, no I'm gonna put in here, um, users that referred for visit in here. I'm a bit lost dollars imposed on the score, and it's OK, so we got this. If the we did this, I want to grab the name. So variable is gonna be equals to name. Okay. And now, obviously, just like we did from before. Guys from the register. If you remember, actually have everything down here, we need to make sure that we do all these sanitizing things, Okay? Actually, I'm gonna Look, I'm gonna grab everything from here, OK? I'm still on my profile. Remember, guys that in the beginning, we just did a copy of everything? Um, I'm just gonna leave everything in here, so let me just delete just going to copy all of this. I'm gonna put it up here, okay? So I'm gonna delete all of these in a second, So I'm just gonna leave these partying here on the top. You guys can always let me just increases a bit. You guys can always just, like, pause the video and copy all of these. So got the user name, which I'm gonna do a filter. Vile. So post user name let me see what we have down here. We got the name, user name okay for these input. So that's what we are doing in here. We are sanitizing the string, and then we're going to do the same thing again for the email. So we are sanitizing everything now, after these. What I want to do is I'm gonna put in here another statement as TMT equals. Like Like you remember guys, we can do these and then after use the same name of the variable because down here, when we start to do these again, were basically just like over writing because this part, when he gets the that's it, we don't need these variable anymore. And after is just getting over written, so it's all good. But if you want, you can create different variables, like esque empty too two and eso won or give it other names. So statement is gonna be equals two b d. O. Okay. And now what I'm gonna do is a prepared SQL statement, and what I want to do in here is update. So this is how you actually updates something in your the database via my SQL so update the table users. Okay, so I want to set the name. This is the column. You remember the column name? Name equals two. Just gonna put these is in here. Short name equals to question mark. Email equals to question Mark. Okay. Where? Okay, as, like, thes let me just check where my I d of the user is going to be equals to something. Okay, So this is my statement. If I'm if I'm a posting in here to this page, okay? We are grabbing the the user name and the user email from the form, which is this that we have in here. Then I'm creating a new statement, which is PDO prepare update the table users. I'm going to set the name toe question mark email to question Mark, where the idea of the user is equal to question Mark. Okay, now we need to execute this, so we're gonna dio ste empty, and I'm gonna do in here just as usual. Execute. So I'm gonna put eggs. Cute. Okay, parenthesis is now. We need to ever know Ray like us, remember? And the rays gonna be with all the values for the name for the email. and for the i. D. So the first volley needs to be the name like like I mentioned before. Guys need to be the same order if I start in here with name. If I start in here after with email and then I d The things in here need to be on the same order. So first is gonna be my user name. Okay, I'm gonna put in here. Second is gonna be my user email that I'm grabbing from the form and finally is going to be the current logged in user I d. Which is up here on the top that we just grab. Okay. And I think that's it. I think that's it. So let me just delete all of these stuff from the code from before. Think, until here. Okay, let me just the lead. Okay, so your PHP parts on the top line just increased the windows so you guys can see if you need to pause the video toe. Copy something that you did forget or something. This is how my PHP on the top looks like. And if I scroll down, these partying here is all the same, like on the register We just took some things out, but yeah, Okay, let's see. Let's see if all of these is fine. So I'm gonna go in here. Tell moderator Tell mother. Right? Let me just refresh the page. Everything is coming up. So line nine with a second. We have something in here if Post. Um, I think we're missing something. Let's see my register. Uh, where is it if is set. Okay, we need to check that. So on my profile, you need to do if is set. Okay? This is what we were missing in here. If is set post at it, we're going to run this. So let me just refresh the page again. No errors this time we got tell moderator. Now I'm gonna just removing here the name from Tell moderator for tell me more and I'm going into the same thing in here for the email I'm gonna put Telmo mods at instead of email. I'm gonna put Gmail. Okay, let's update thes and see what happens. I clicked. Nothing happened. But now if I refresh the page hopes, let me just quick on the u r l on the top so as you guys can see everything is still the same. Look, if I click on the euro and I refresh the page, if I go to my home and then I go toe profile, these is still updated. Let's check now over database. If I refresh the page of the database, look on my database. Now I have Tell me more. Tell me more at gmail dot com and basically, that's it. OK, so we just learned how to basically update the details of a user. Let's just log out. I think they have another use it. Tom, Tom at email dot com So let's try to update them the user name in here of Tom. So I'm gonna do look out first of all. Okay, look out. Now, what I want to do is log in. So I'm going to do Tom, I think. Was it tom at email dot com I'm gonna put the password. All my pastors at the moment is just 123456 You guys can put whatever you want. Log in. Look welcome, Tom. This is a super secret content only for logged in people. Let's go to the law to the profile. Look, user name Tom User name. I told my email dot com. I'm gonna put his name now to be told me and I'm gonna put is female to be told me at email dot com Let's update the details. That's it. It's all done. Now let's go to the database. Let's refresh and look now, I have told me, told me at email dot com. If I go to the home page, go back to the profile. All is fine. Alright, guys. So that's it for this video. We just learned how to update the details of our user or any other content that you would like. And in next video, we're actually going to start with the blog's. So hopefully rise like this video, and I'll see you in the next one. 7. Creating Blog Posts: All right, guys, welcome back. So in these video, we're going to start building our blawg. So let me do the first thing in here that I would like to do is create a new page called blogger dot PHP. So I'm gonna do blawg dot b h b. And inside of year. Um, what I want to do is just start with my, um PHP doc to start. PHP, Doctor. Close and down here. Let me just see what I'm gonna put I'm just gonna require Let me just copy this. I'm gonna copy my heaven. If you guys remember, we also need to use our footer. Okay, This is the same thing. Like they own all the other pages. You guys can go on the top of the profile page and heaven in here on the top. Now, what I'm gonna do in here is created Eve with a class off container. Uh, then inside of the with the class, contain it. I'm going to create another the with the class of cards, and then I'm gonna create another diva with a class of card Heather BG lights, then MB dash three. Okay, this is all, like kind of bootstrap classes just to give a bit of style to our page. Now inside. What else should I have in here? Let me just see This one can just go up here. I'm just gonna put in here post title at the moment. Then I'm gonna put something else now. I'm gonna put in here, Dave, Dave with a class of cart body. Oh, why, These did not work. That it is now inside. I'm just gonna have a paragraph and insides. I'm just gonna put some random checks for now, Lauren, ipsum, blah, blah, blah, blah. Just some random checks so we can see. And I think that's it for now. I think I have the the container. I think I misspelled container on the top. Okay, I got leave with a class of container. I got the cart. I'm pretty sure I'm missing something else. I should have Maybe some kind of row. Maybe I don't need it. Let me just see how this these look like at the moment. So blawg Okay? These blogger at the moment still does not exist in our in our navigation bar. So let's go to the inks. Let's go to the heather and in the heather. I want to make sure that even if the user is logged in or not, we always have a blocked page. Okay, so I'm gonna put in here, is gonna be to blawg dot PHP, and I'm going to call these look, okay, I'm gonna refresh my page. Look, now in the top, I have blogged with one post title and just thes Lauren me Epsom. Imagine this was like a post title, but what I want to do is actually start creating some block posts. First of all, I'm just going to go in here to my, um, database, okay? And I'm going to create a new table just for the bloc's. So let's go in here inside of my logging project, I'm gonna click on new to create a new table. Now, the first volume in here for these column is going to be an I d. So just as usual. And then this is gonna be an indigent. It's fine. And then I'm going to scroll in here to the right side and the life. Find these ai, which means also increment because you just press go in here. You don't need to touch anything else. You just want to make sure that whenever you create a new post is gonna be automatically incremental. The i ds. After I'm gonna have in here something for the title, the title is gonna be a bar chart to 55. And then I'm gonna put in here the body and is also gonna be a bar chart to 55. Okay, this is should be all all good, so I'm gonna press safe. Okay. Ah, table name. I forgot the on the top. You need to have a table name off. Um, I'm going to say block. Okay, let's save these. And now I should have a new table in here called blawg. There is nothing in at the moment. So let's just create, Like to block posts, for example. So on the top, there should be something say insert. Now insert on the I. D. You don't need to do anything else, OK? And now in here on the title, I'm just gonna put post one. And on the body. I'm going to say this is the boast one body and then blonde, blah, blah, blah. Whatever you guys wanna put, I'm gonna do the same thing in here for another post. I'm gonna actually create two posts. So I'm gonna put post to and I'm gonna copy this stuff and I'm gonna put inside. And this is the post to body. Oops. Post to body E. I'm gonna the yea and not another post for my blawg. Okay, Another opposed for my block. I'm gonna press now in here if you just put, like, go. The should be safe now into your database. Okay. Lets clicking our blawg in here on the top where it says brows. As you can see, we have I d want you got to the post one title You've got the body and so on so they should be all straightforward. Now, on my, um Blaga Ph B If you guys remember, we can do something in here on the top. The first thing is I want to make sure like I mentioned in the beginning of the video. Okay, one, the 1st 1 I want to make sure that everyone can see the block posts. But on Lee, the person who is logged in and is a moderator can actually add some new posts. The first thing we're gonna do is grab everything from the database and this plate on our page. So let me go in here on the top and just start with the session start. So I'm going to go to my profile copies because we're just doing the same thing over and over again. Sessions start. Then I'm gonna check if my session I d function is in here. So I'm gonna check if session user id's is set. Okay, to make sure that the user is logged in, then what do I do? I'm gonna copy my connection. Okay. I'm gonna copy my connection, and I'm going to copy the user. I d. So this is gonna be in here. Okay, I'm going to require my connection and then grab the user i d which we are doing all the time. And then I'm going to create a new esti mt. A new statement and these new statement now is going to be a p d. O. Okay. And we're gonna prepare a new SQL statement in here, and I'm going to say select oh, from users where the i d e z goes to question Mark. Okay. We're gonna do the same thing like in here. We just did this. Where was it? Just copy this. And then do they use their fetch? This is the same thing over and over again. If you guys didn't know this already. So we're gonna execute this statement, and then we're gonna grab the user that is actually currently logged in. Okay, Now, we can use these later on when we want to do something. And after these, I'm going to create a new statement. Okay? The statement can be with the same name, guys, because after you grab all of these stuff, you can just, like, overwrite it. That's what I'm telling you all the time. Statement, video, prepare. And now what I want to prepare is select All should be all capitals in here. You can put lower case, but it's just a good practice to have everything like with capital letters select all from Okay, bloke, which is our new table that we just created. So I'm gonna select all the columns from there, and now I'm just gonna do a statement. Execute So esti empty and I'm gonna do exit. Cute. Okay. And after you execute these, I'm going to create a new variable called posts. I could call it anything else, but I'm calling it posts because I'm grabbing all the posts from the database. And now this is gonna be equals to my statement, and I'm gonna do fetch, and I'm gonna do Oh, okay, so s t empty fetch. All so they should be good now that I have actually everything, um, fetched from the database in year on these posts in here on my actual HTML. This is what I'm gonna do. Let me just go in here on the top of these cards. I'm just going to do a Ph. B. Okay, and I'm going to close it. What I want to do is I want to do a for each, Okay, if you guys don't remember before, each is basically a look for a race. Because when you get these variable in here posts is actually an array with all the values that are coming from the database. So we can use these for each and the four each is is gonna grab first of all, your variable with a right which is posts. And then I'm going to say us and I'm gonna put post. So we're gonna do a loop through all the elements inside of the array. And every time we're going to go through the loop, we're going to represent the current post as thes variable called Post. So we started in here the for each, and I'm just gonna put in here the curly braces to start. And when we finish, um, when we finish in here, these cards we're also gonna put in here the okay, the PHP with a closing talk, as you guys can see. Look, and let's just close the PHP. It's just invent these gold so we can see these a bit more organized. Now, inside of these loop, I can actually grab things like instead of having in here post title Aiken dio BHP. And now I'm just gonna do echo. Okay, I'm gonna do echo, and I'm gonna dio post title like this. So I'm gonna copy this on will be exactly the same thing in here for the paragraph. Okay, instead of title now I want the body. Okay. If you guys remember, look, title. This is the column off the titles and body. This is the column of the bodies. So if this is all goods and we go back in here, I'm going to refresh the page. And look. Now I got post one, and this is the post one, blah, blah, blah and post to this is the title. And I have This is the post to body. Yea, another post for my block. So as you guys can see, we can actually now grab everything from our database from the blocks. But what we want right now is to be able to create and a new post. So what we're gonna do for that is I'm gonna go in here. Let me just see. I'm going to go in here and create on my includes okay. On my includes, I'm going to create a new file called New Post Nauts. H T m l. Okay, this could be html. Baikal put just Ph. B. Maybe maybe it would be even better, But this is fine for now. So it's just like an HTML. And what I'm gonna do in here is I'm gonna show in here on the top. I'm going to do bootstrap motile. So if you're going toe woods, drop that he's actually these codes. Four, I think. Is this one? Yes. Something like thes launch them a model. So you want to grab something similar to these to these model? Okay. You see, we're gonna have something similar to this, so I'm just gonna put it in here and now I'm just gonna, um I'm just gonna edit these a beat. So in here, I'm having this. I'm gonna have clothes, and this is going to be create post create post. And what else? I'm gonna have a Deve. I think I wanna have the same thing like I had from before from my profile. So I'm gonna have inside of my mortal body at the moment. There is nothing in here, but I wanna have the same things. Like I have one of my log in. So let me go to my log in, Okay? Let me just see how this just looked like a the moment I'm gonna go into my eye logging. I'm gonna have these form. Okay, I'm gonna copy this. So this is on my log in page. I'm gonna goingto my new post out html when a paste. All of these in now the name of these is going to be create post. Okay, At the moment, I don't even want these bottom Toby displayed on the page. So I'm just gonna put, um, class of the none. So this is just like to display none. This is a bootstrap Kloss. I think I had some stylings in yet. I'm gonna get rid of this, okay? I don't need this. I'm just gonna have in here on imports on the top. So this is the user email. This is the password. I don't need the passwords. Okay. I just need this. I don't need these break talking here as well. Okay, This is gonna be my post body, so I'm gonna put in here. The name is gonna be the post body. Let me see if I'm just missing something. Um, I'm gonna put also up this one instead of being an input, I'm going to create it as a text area. Okay, because the text there is just bigger. So I'm gonna put text area and now in here, I need to close it like this text area. Okay. What's that? The type is going to be text. I don't think we need a type for the text area if I'm wrong. So class, form control. I need an I D. Which I'm going to show you in a second. A 90 off message text. Okay. This could be anything that you guys it could be post Allah. Let's just leave message text. I'm gonna leave it for now. Later on, we can leave it. So in here, I got the label. This one is going to say body. Okay, this one is gonna say body, and these label is four. The post body. Okay, so this is what it looks like at the moment. Now, I also need to create one for, um, the post title. So on my log in, I should have something I think I shouldn't have deleted. So I'm just going to copy these from my log in these label on my new post. Just on the top. I'm gonna have ah label. This should come with a form group as well. Let me just put these around these label. Okay? Put this in here. Put these in here and now label for this is gonna be post title. Great type text name is gonna be post. So I until right. And I'm gonna put, um, don't think I need to put nothing for now. Let's see the label. Four post title. And in here, I'm going to say just title. Okay, so let me see. When I submit this form, I want this form to be submitted to the blogger dot PHP blogger dot PHP. Okay. With a method of boast as well. And I think that should be eat. Let's let's just see how does he look like, let me having to get a new title is well called New Blawg Post, and I think that should be it. Let's let's just see. Let's just see if everything is fine. We're going here. I mean, just refresh. I'm gonna go into my block, okay? I need to, um now that I have these in here, I need to go into my block just after I require my heather. I want to make sure that I require my Where is it? I'm gonna require my, uh, inc new post dot html. Okay. I'm going to require this now. There's one thing in here missing, which is we need to actually create, um, a button a button in here. That when we click is gonna show us something. So these button, where is it on d c? That's just creating here on my blogger dot PHP A new button inside of this container. I'm just going to do in here a bit of space. I'm gonna dio I'm gonna do in here a dif. Okay? I'm gonna do a live with the claws of Roe. Okay? Why is this no working? Okay, Dave Rowe and I'm going to create now Deve with the class of coal as, um, 12 and inside. I'm gonna create a button and these bottle. Now, let me just see what I'm gonna put in here. I'm just gonna put a button with a type Oops, a type of button, And I'm gonna put in here the class of DTN bt n sook says, and then I'm gonna put the data, so go. Okay. Model. Okay, let me just see data toggle model. And I think that should be eat. Let me just see. I'm gonna put in here, create new post. I'm gonna put bottom. Okay. Let's see if this is displaying at the moment. Yeah, it's working. Create a new post. Can you see the button on the top. So I need actually to put some kind of styling in here. So in here, I'm gonna put the style I could go into this year says, but this is just like something so small gonna put the margin top of 50 pixels. Okay, so if I refresh, yeah, now is better by clicking here. Still, nothing is happening. We actually need to create just a small javascript. So my javascript in here? What I'm gonna dio is I'm going to create a if statement, okay, if documents don't get element by i d. And the idea that I want to grab is the bt and delete model. Okay, so if we have these bottom on the page, Okay, let me just see. What is this button? Um, I'm gonna go into my blawg. Go Ph b. Not on my block on my new posts. So let me see. I should have been here. A delete. Where was it? My main dot Yes. I'm just put these two together so I can do this so that by i d So get the element variety , bt and model, which should be on my new post. Let me just see where it is. All right? I don't want to do these exactly. Now what I want to do is grab the document, don't get element by i d. And I want to grab these element in here, which is gonna be the Cree eight post model. So these i d okay, is just what we're doing at the moment is just checking if the's element is on the page and these element is going to be these button down here, which it say's create post. Okay, at the moment, this is still not does not exist. So this is this bottom that we see up in here. Okay, So I'm gonna put in here, eh? I d like this. Okay, I'm gonna put this This is part of my foot turn. So if this is exists, this is gonna be the bottom that we have on our model. I'm going to show you in a second if these exists. OK? What I want to do is do, uh, document not get element by i d. And the idea that I want to grab is that create post model, create post model, and these button does not exist at the moment. So if we go in here, this should be, um these button where? Let me just see where it is. This one that we don't see at the moment. It should be this. Okay, so these button in here, I'm gonna give it a ni de Okay. This is the actual bottom that we should click to open the model, but we are just hiding it at the moment. And then we are using javascript, so I'm grabbing these elements, okay? And I'm gonna put create post model dots are event listener thought aunts, even listener. Okay, let me just see. Create new post. Oh, wait. This should not be in here. We already have it down here. I don't know why I'm just like on duplicating these. So I have these button in here whenever. If it's already on the page, that's what we are doing in here. Okay, we're gonna add an event listener to it. So our event listener of click, okay. And then we're going to run a function. Okay, We're gonna run a function. And now what do we want to do when we click on that? What we want to do is just gonna baste it in here for you. I'm gonna do a document. Get element by i d create post for click. OK, so these create post form, it should be in here, which let me see my create post form. It should be these button. Okay, I d I'm gonna put in your i d off. Create post form. Okay, Now, let me just see if this is working. Um, we're doing all of these. We got our Mendel chairs in here running. Let me just see if this is actually running is running in here. Let me see. Foreign mine Footer is included is also included. I'm going to click in here. Let's just debug these quickly to see what's going on. So if you guys remember, I'm checking if documents that get element by i d. Okay, create post model, which should be on my new post. Create post model. This is the bottom that we see on the page in here. Okay, So if these button exists, what I want to do is I want to grab the document. Don't get element. My i d create post model, which is these same button I want odds an event? Listen, it OFC leak. Okay? And when I click on its I want to run some function. The function that that would have run is to grab these elements. Why, I d we Cheers. Uh, this one in here, this is the bottom. That actually, um, opens the more, though, and not and create post form. Okay, click create those form. Click. So let me just see. I'm gonna put the council about logging here on so that log, same button clicked. Okay, let's just refresh the page. I'm going to click on it and is no working. Okay, so let's debug this. Oh, I see what might be missing in here. Um, if we go into our new posts in here on the top, we have on our model that comes by the fold from boots, drop with a 90 of example model. Okay, I need to make sure that I have down here targets on the button. So I mean, going here on the button, I need to have a data target. So in here on my blogger dot PHP. When I click these books and I have the data total model, I need tohave also a data. It's our goods to be Let me just see it should be example model. So it should be hashtag Example model. Okay, create a new post. Let's refresh. I'm gonna click now. It's working, OK? And now I can create posts or close. So if I click in here, this is all goods, right? Okay, now this is all done. Was a bit messed up in here, as you guys could see, But I'm just gonna go over again to make sure that everyone's understands. And so we created in here First of all, on my blogger dot PHP All these front end interface. Okay, then we created these new button. Okay, These new button is what we seem here at the moment that creates these create new posts we have in here, eh? They to toggle. This is on my blogger dot PHP guys, data total model. We have these data target example model. So what it means is when we click on these button, we should be able tow open our model. Where is our model coming from our model? Look, we are requiring it in here from the stop. Okay? So if this is already. Um, required. So I'm gonna open the in here. What I'm gonna do is I have in here a form. Where is my form? The form is starting in here on the top. It means that we're going to serve meat. All these data from here to the blogger dot PHP via method post. And we have a button down here that this is what we are checking on our main door chairs. We're checking if these bottom exists on the page to make sure that we don't have errors in other pages because we are requiring these JavaScript filing all the pages, basically. So just do not give some errors. We just need to make sure that these button is present on the page. Then what we do is we're gonna add an event listener to these bottom that say's create post model me. Just see, where is it? Create post model, which is this button in here that says create posts. So I'm gonna click in here, create posts. You see, this is the bottom. So I'm gonna add an event listener that if I click on this button, I'm gonna put some stuff in here. Some stuff in here. I'm going to click in here. And what are we going to do when we click in here? I'm gonna grab the button by I d create post form and I'm gonna click on it. This is an automated click. So where is Thes Bottom is on my new post dot html on my form. If you guys noticed that is these botton in year, these bottom needs to be clicked in order to submit the form. So because thes button at the moment is completely he even Look, let me show you. You don't see it in here on the page. You actually need to create toe click on these. Ah, Button Two other event listener to the bottom of these form that is haven in here, and these will actually submit the form. Okay, now that looks like everything of these is fine. What we can do is we can go back to our blogger dot PHP on the top and look, let me just go back in here to my new post dot html. The name of these bottom. This is the button that is submitting. The form is create post. Okay, so on my blogger dot PHP after all of these things in here from the posts. Okay, what I'm gonna do is if is set. And now I'm just gonna put dollar sign on this card post. And what is it that I wanted that create create post button. Okay, so if this he said I want to grab the title, Okay. Now, what I'm gonna do is just like fielding all these data, So to filter it, I'm just gonna grab some codes. Um, that we had from before. So where is it? I'm just gonna copy these and just edit it. So I'm going to place this in year. This is going to be the title. This is going to be the body now. I want a fielder. What dollar sign? Post on my new post. Art HTML. This is my input with the name of Post title. So this is what I want to grab in here. So this is gonna be sanitizing the string. This is sanitizing the string. Now, this one is gonna be for these text area with the name of post Body and let me go to my block. Let me put this in here. Okay. I'm grabbing all these two things. Now. What I want to do is, um I want to go into my statement. Okay, so s t e m t equals two. Um, PDO, prepare the statement. Prepare. And the statement now is gonna be insert into block, which is the name of my table at the moment I wanna put was inside of the block. There is the column of the title and the column of the body. Okay, I wanna insert the values off. Okay, I'm gonna put a question mark. Question Mark. Okay, so I want to put these in there. Now. What I'm gonna do after is I'm gonna execute so ste empty. Exact, exact. Cute. And what I'm gonna execute in here is gonna be in the value of these first question Mark, I'm gonna grab this title. Then the 2nd 1 is going to be the body. Okay. Okay. So we got these. After you execute everything, what you want to do is you just want to redirect in here to the page again. So if you redirect to the page, you will see the page reloaded with the new post that has been added so I'm gonna do Heather. Okay, I'm gonna do location. And now for the location. Guys, I'm just going to go in here on the top. Just copy these link for these posts, Page, and I'm gonna put it in here. And hopefully, if I'm not wrong, everything is fine. Okay? So let me just reload the page. I'm gonna put in here opposed three. And this is the post three. I'm just going to click on it. And look, we just added the complete new post. Okay, Let me just see if this is on the database as well. Refresh and entities, this is the post three. Now, there's one problem at the moment in year. Like, do you guys remember that? I only want people who are moderators or add mean to be able to add some new posts. So at the moment, I think we are logged in as let me just see. Lower profile. This is Tommy. So told me at the moment is just a guest. Okay, so let's go back in here to our database on the users. So if I just scroll these in here to the right side that beat, can you see, the role of all these persons at the moment is guest. Okay, so I want to make sure that only a person that is a moderator is gonna be able to do this. So I'm gonna check these first of all, on my blogger dot PHP where I have where I have my button. Okay, Toe create a new, um, to create a new post. These buttons is only going to be available for people who have a moderator. So I'm gonna do in here pH b open and close. And now I'm gonna do in here if Okay, the user role of this person who is at the moment logged in if they use a role is equals to more. The Reiter okay? Or user roll is equal to ads mean. Okay. So if the user role of this current user in here is a moderator or they use the role is a nod mean these buttons should be available. Okay, So I'm gonna put in here. I'm gonna put in here a closing time. This is Ph B. Okay. And I also just want to require these new post from the HTML if the user is also that. Okay. I need to make sure that I only required these model if they use it is actually a nod mean or a moderator. Okay, so let's see these again. I'm going to refresh my page if I go to my block. Now, look, there is nothing in here. Okay? So what I'm going to do now is I'm gonna do a log out. I'm gonna log in as, um Telmo Mort. Not that I think his gmail dot com. Let me just see if this is correct. Yeah. Tell me more that gmail dot com and then I'm gonna put 123456 log in. So he's logged in at the moment Block. There is nothing in here to aunts. Now, what I'm gonna do is on my database. For now. I'm just going to change the value in here because on the next video, I'm going to create a nod mean that will be able to change the rules of the users. So in here on tell moment, I'm going to double click on these role, okay? And tell Mom what is going to be more the ray. So Okay, let's save this. No, in here. Look, tell Mom what role is a moderator? Because I change these on my database. I refresh the page. Now, look, create a new post is in here so I can click in here and maybe other new posts. So I'm going to do post for this is the post for both the click. And look, I just created a new post. Look is stealing here now, Look, I can create more post if I want to. Now I can just look out. I'm gonna just, like, log in as Tommy. I told me at email dot com 123456 if you remember. Told me was just like a regular guest. If he goes to block, look told me because he's just a guest. He cannot create a new post. Alright, guys, that's it for this video on how to create some new block posts. And that's it. The last one is gonna be the last video about. I've mean how you can create the knob mean to change the user details or their user roles. And yeah, that's it for TV. You guys, I hope you like it, and I'll see you in the next one 8. Creating Admin Dashboard: All right, guys, welcome back to the final video. Now we're going to create enough mean dashboards for the admin to, for example, update the users to delete them. And so one. So the first thing we're gonna do is in our index dot PHP in here on the top. Okay, if the user role equals two guest, you're going to see these, and I'm going to see else if I'm gonna put the user, the user role is gonna be equals to, um, more the Reiter I'm gonna put in here something like more. Ah, Reiter. I'm gonna put a message that is gonna be your role is a, um your role is more the Reiter. And then finally, I'm gonna check if the user role is admin. Okay. At the moment, we don't have any admin. We're going to create one. Okay, So if this is, I'm gonna create my message. I don't remember. Now, if we actually your role is I'm in. I don't remember if we actually put something down here, I don't think we put so welcome gassed, then inside of the body. I'm just gonna put in here a message, so I'm gonna put in here as a paragraph. Um, people like is NH three. I'm not sure. Let's see. How does this look? Like a the moment. So I'm gonna put in h three. I'm gonna put in here, eh, Beach, be close, and I'm gonna put the necco of the message. So we are always going to see a message, no matter what. So I'm just feeling just refresh the page. Uh, online 41. So what's the problem in here? So if is set, use an I d. Oh, okay. I see. So we actually we actually don't want Teoh. Um, cause when you want a guest, this is not gonna happen. So I'm just gonna put in here if is set message. So where it is, I need to put ah as a message. So what I'm gonna dio is BHP Then I'm gonna go If and I'm gonna dio is set message. So as you guys can see, this will never work for the for the guests. So we're just gonna delete that on the top? Maybe I was trying to do something before I didn't finish. So in here, where it say's guest. Let's just get rid of this. And let's just have these. If use a role equals two moderator message, Your role is moderator else. If use a role because to have in the I mean we should see this. So let's refresh the page. Nothing. I'm gonna go in here as tell more mods. Tell more Maud's, then 123456 When a log in your role is a moderator court. So now I should creating a new user. So let me just look out. I'm gonna register. I'm going to call this one. Telmo Teoh. I'm just going with John Doh. John Doe is gonna be the abdomen. So, John, add mean at email dot com So this is gonna be the the So, John, have me not email that com password. 123456 When a register. Okay, now I should be able to win toe the log in and dio John. John, have me not email that 123456 I'm gonna do a log in and are welcome. John Doe. This is a super secret content, so it should be all good, actually, on the top look, which we could still have the same thing to know why I'm just getting confused in here. They use the role. It was to guest. Okay, in here. I'm gonna put another else. If I'm gonna close this now, they should be goods. OK, your role is guest. Let's refresh the page. I know that John Doe. I just registered it. The role of it is a guest. So let's go to our database. I'm going to refresh the page and John Doe our email. I'm just going to double click, and I'm gonna turn these guy into another mean, Okay, so now this guy is a nominee. Let's refresh the page. Welcome, John Doe. Your role is admin. Okay, Perfect. Now, if I go into my block, if you guys remember because these users as a user role of admin, I can go in here and just click and just, like, go create a different post. Okay, Now, this is all the goods, and the next thing I want to do is just this play in here at the moment, all the users. So where I'm going to do that I have in here. This is a super secret content, and I guess I'm just gonna do it. Um, on the top. So what I'm gonna do is in here after all of this inside of these cards. Body. Okay, I'm gonna put a northern list, so I'm just going to check. So if it's said the user, the user should be set anyway. I'm just gonna put in here. Ph b. Yes, that's gonna We're just gonna try these, um we're gonna doing here if oh, I get I see I I think I can put these After all, I'm just doing these almost all the same time with you guys. I'm just thinking about it as well. So on the top, when I have these role of the abdomen. Okay, what I'm gonna do is after I said Thies message, I can just go in here and do s t m t. So creating a new statement equals two updo. Okay, I'm gonna prepare a statement. Pre pair, prepare the statement and the SQL statement that I want is select all from users. OK, so I'm going to see, like, all the users now, I'm just going toe s TNT statement. Eggs acute. Okay, this time I'm not passing anything inside because You know, there's nothing of any positional parameters in here. Now I'm creating a new variable called users equals two s, the mt. And I'm gonna dio fetch. And what this will do is in these variable users will actually just crap all the users that I have on my database. So after I have this Okay, um, I'm not actually not gonna put these message in here for the admin. Okay, I'm gonna do in here if, um, I'm gonna put in here if it's sat the message, okay. Else else If okay. Else if And I'm gonna put this set users. So just to show you guys users, these variable that we just created up here is only going to be available when you are logged in as a nod mean, Okay, that's why I'm voting in here else. If is set users, if the's variable is set, it means that we are looking as a madman. So what I wouldn't do in here I want to do in here. Uh, you? Well, okay. And these ul is gonna have a class off list group. Okay, let me just close thes down, so I don't forget. So I have a nor the list now inside of these you. Well, what I'm gonna do is I'm going to start doing a four loop. So in here, I'm gonna do a Ph b and close again, and I'm gonna do it for each. So for each okay, lets just dio before each that I want to do is I want to go inside of my users the variable that I just grabbed on the top and I'm going to do in here. Ah, us loop user, You know, because I already have a user variable on the top somewhere in here. I don't want to use the same one, so I'm just gonna call this one a loop user. So whenever I'm doing the loop through all the users from my database, I'm going to call these loop user. Okay, let me call and let me close my for each because it's always good to keep track of it. Whatever you open, you should close. Okay, now, now that I have thes, I need to check something in here. I'm gonna check this. Okay. Uh, Ph B K open. Close. What? I'm gonna check now. If is my loop user email. Okay, loop. User email is different from this current user email. Okay, so why am I doing these at the moment? I'm just doing these extra security in here to make sure that when I'm looping through all the users, I'm gonna display all the users, but not this one. Okay? These because when you go into the database and you're grabbing all the users from there, I want to make sure that I don't want to put in here my admin because it doesn't make sense . I only want to see the other users that are on the page. So I'm just checking these partying here that it does. Um Anethe statement. Okay, if if the loop user email, which is this look that we're doing right now is different from the current user that is logged in email. Okay, if this is true, what I want to do is, um let me just see. I'm gonna put in here a list item. So, ally, with a class off least group item, Okay? This is just some bootstraps stylings, so I'm just gonna close this one, so I don't forget off course. Um, So I'm going to do in here. This okay? Close. Now that this is in here, the next thing I want to do is I want to put in here the name, So I'm gonna go Ph b close. I'm gonna put Echo the loop. User name hoops name is just, like lower case, so they should be fine. Let's just see if this is working. It's so far. Let's go to our home. We got some error online. 55 which she is. Um where was this coming from? If it's said, I think I have, like, a next room. Probably. I had, like, a next throw. One. Let's just see. Oh, I see. I forgot to put this one in here. That's why I let me just end do on deal. Okay? I just forgot to put in here on these. If statement now should be all goods, let's just refresh. And now it's fine. Look, at the moment, I'm only grabbing the users from the database. Look, I have tell me some pie. I have John, I have told me, and I have tell Mom what I don't have at the moment. The John Doe, which is the current army. So we're grabbing everything from the database. Perfect. Now, let's carry on with this After this. After I grabbed this, I want to start with a select. Okay? This select is gonna have a name off user info. Okay, We need these name. Because after when we're gonna be submitting these stuff, we need Teoh. Ah, give you the name. Of course. So I'm gonna have a selecting here and now inside of these select I'm gonna have a PHP. Okay, Beach B. And we're going to say if Okay, the loop user, the loop of abuse, it roll equals two more than Reiter. Okay, let's close this. If the loop user equals two. Moderator, I'm gonna put a new option a NA option in here. And these option is gonna have selective equals to select selected. Now, I'm gonna put in here the value it's gonna be equal to more. The Reiter. Okay. And this is what I should see in here. So value moderator and I have in here more the right and in here, I'm gonna have the option to close. Okay, so this is what happened. If the user is a role is a moderator, then I'm gonna put the nails, if else if OK, I'm gonna check in here. If the loop user roll. If they're loop, user role is equals to guest. Okay, it's open. Another ph be closed. The curly braces. So what do I want to do in here? Oh, I forgot to put after these option for the moderator. Hoops after the moderator, I want to make sure that I put another option to be with a value of guest. These volley we need is just like to submit to the form after, and then here is gonna be guest close down the option. Now these in here. If the loop user role equals two guest, I'm gonna put an option. Okay, well, the value of selective I'm just gonna copy these part selected equals to select it. And then volume equals two guest in here. I'm gonna put guest. This is what we see on the front page. Okay? And that should be after I'm just could gonna put one more option. So if they use a role is a guest I'm gonna have inside of these Select a non option with the value of more, the rated and the value of more than Reiter. Okay, well, let me just see. How does things look like at the moment? Okay, just refresh and look. Look what's happening in here. I got to tell more guest, which by default, tell us some by which is a guest by default. Okay, I have John, which is just John is just a guest I have told me, which is just the guest. And I have Tell me more who is a moderator by default. Look, that's why it comes in here as a moderator. Okay, now we got all of these. I'm gonna put some buttons in here, so I'm gonna put in here, Um, after the select, I'm gonna put a button. Okay. A button with a class off Bt n bt and primary. And now I'm going to give it a type off submit. So whenever we submit these selecting here from this form, we should be all goods. So let me see if I'm missing something. I think I missed the bit. That's fine. We're gonna have these later on. So I got these button with the type of submit, and now I'm gonna give it a name. Name off. Super. Have it. And these button is going to say update, user. Okay, this is a button. Let's close these down. Okay? Now, after these, I'm just gonna create some bottoms in here That is going to be to the lit the user level. Just refresh. Look, I got these buttons saying, updates the user so you can change anything, and then he's gonna update the user. Um, maybe we can We can do these rights now. Maybe we can do these right now. Um, OK, so in here, on the top, let me see. Where is it? Let me see. Where is it? Just before our an order list. I'm going to create a form. I need to make sure that this is all inside of a form, A form with an action to I've mean dots. I mean, update, not Ph. B. And he's gonna be with a method off post. Okay, so I started my form appear, and I'm gonna close the forum just after my on order list. So just in here. Okay, so I got I should have a form now in here, which is all goods. So whenever we click this button, we're gonna submit these with the name of Super Edit. And we said that we're going to do these two. Um, we said that we're gonna do these in here too. Where is it? To the Army update on PHP. Let's create this file. I mean, update dot PHP. Okay, so, no, I'm just going to go in here and I'm just gonna stop to the regular PHP dark, Okay? There's gonna be a lot of things in here that is gonna be the same thing like from before. So you need to have You can copy these from other files sessions start like we do all the time. We're going to copy these part from the logging or any other page. If is set session underscore. Use that I d require the connection. Setting up the user I d variable grabbing and grabbing these current user. Okay, Now, we need to make sure that these person in here Okay, So I'm gonna do if a user roll equals two. Okay, I'm gonna put I've been so we want to make sure that Onley, if they use the role, is enough, mean it can do all the things that we're gonna do right now. I'm gonna put in here role. Okay. Roll equals two, um, dollar sign on the score post, and I'm gonna put in here on user info. Oops. Role is gonna be equals. Two door sign and the score post user info. If you guys remember, this should be in our index dot PHP. Uh, it should be. Let me just see. It should be in our index dot PHP A select with the name of user info. Okay, so you're gonna grab these. The role is gonna be whatever you have selected at the moment, then. Okay, What I want to do is I want to grab the tonic. It Tom gets user, I d This is gonna be equals $2 sign on the score post. Okay? And these is going to be whatever comes from the targets user, I d. And if you guys don't remember these, let me just go back in here. Target User, I d is gonna be closed. $2 on in the score. Post target User. I d. Let's go to our index dot PHP. I'm actually gonna put in here after my ally. I'm gonna put the name put okay with the type off. He even Okay, I'm gonna give it to the name off Target User, I d. With the value off now I'm gonna put in here. The value is gonna be b hb kind of them. Just close these down. I'm gonna do a Neco off the current loop user loop user I d. So whenever we are looping through all of these and creating these stuff, I'm actually in each one of these rows. I'm actually putting a name. Put him even that nobody sees with the name of Target User I d. And the value is gonna be the i d of the user. Okay, so now on my admin dot PHP I'm having in here the target user I d equals $2 sign. Underscore Post target user I d. Now, what do I do after these? OK, I'm going to do it. If is set, I need to make sure that the post has been submitted. So dollar sign on the score post. I'm gonna do my, uh, user info. Okay? No, not using envoy Super and it. Okay, why is the super Evitts? Because on my index dot PHP, this is my bottom. That is actually submitting the form. Okay, The one that says update user. So name Super added. So if this form is submitted, I need to make sure I'm doing all of these steps checking if the user is logged in checking if the use it is a nod mean checking If the form has been submitted. All of these security guys, you can make it as secure as you want. Okay? So if all of these is true, I'm gonna start with the statement esti empty as TMT and equals two a PV. Oh, and now I'm going to go in here into a prepare hoops. So I'm going to do a free pair repair. Sometimes I get these wrong all the time, so I'm gonna do update, update users. This is my table. I want to set the role to be equals to question Mark. Okay. Where the idea is equals to something. Okay, this is my statement. Then I'm gonna go esti empty execute just as usual. And what do I want to execute in here? I want to execute the role, which should be this one in here. Okay. And the second thing is gonna be the i d the i d comes in here from the target user. I d Okay, so hopefully this is fine. Okay, so these should be all goods now. Okay, Um, and after after I do all of these things, I actually want to do a redirect to the home page. Okay, so we see that things actually changed. So after these, if statement, let me just see after these, if statement, but still inside of the if user is this'll Yeah, I'm gonna put in here and Heather location, and then I'm gonna every send this to the home page. Okay? Let's try it out. Remember these, user, I'm going to refresh the page. Remember these user Tommy, which was just a guest. I'm going to go in here and just change this one. Toby. A moderator, OK? And when I click toe update the user Looks like we got a narrative on update PHP line 23. Let me see. What is the problem? I think we're just missing in here. A, um I think we're missing just a semi colon. Okay, let me go back. Refresh the page. I'm gonna put Tommy as a moderator click, and now it should be all goods. OK, I'm just refresh the page again. Um, I'm not sure. Why are we having these connected successfully again? But that's fine. Okay, I got in here. These welcome. Talma. Moderator. Um Why mine here is a term moderator. It doesn't matter. I'm gonna go and get it to the log in. I'm gonna go to the Tommy. That's email. That's got 123456 I'm going to do a log in on this, um, error in here. Looks like we are also logged in. So log in. Okay, so I'm gonna do one more time. I'm not sure something went wrong in here. 123456 Log in. Um, there's something wrong in your okay. Let's just check what's going on. Right. Let's see. I'm just do one more time. I think I was just, like, under on the wrong logging up here. Um, I think this one in here is logging. I think I put in here the wrong redirect. That's what messed up everything. So on my where was it On my mean update? I think I put in here the wrong. Okay? I think now this should be good. Should be logging Dash project. OK, and then they should be index dot PHP. I think they should be all good now, guys. Sorry about that. Let me just go and get a log in. I'm gonna try the Tommy told me That's email that calm. 123456 Log in. Look, tell me your role is a guest. Is it the guests? I thought we just changed it. Let's just look out. Log in. I'm gonna do, John. I mean at gmail dot com. 123456 Log in and I'm going to go in here. This one update the user, and it looks like he didn't update. Let's see what's going on. I think I know what mind be the problem in here. We need to make sure that our form Okay, uh, we need to make sure that our loop is outside of the form. Probably. This is what's wrong in here. Okay, so if I'm going to go in here, okay? And I'm gonna put thes for each outside of the form, and I'm just gonna grab these one, and I'm gonna put it also outside of the form. Just a deal. Grab the sported outside of the form. I think now we should be fine. Okay, so on my admin dot PHP, I just put the Neco in here of saying inside off the super added and done to make sure that we go in here. And also, I'm just making sure that I I just put the Necco of the role and the target Use an I D. Okay, let me just refresh the page. I'm gonna put Tommy now, Tommy, let me see on my database. Has a 90 of three and has a role of guests at the moment. So let's just try it. Tommy is gonna be a moderator. Update moderator three inside and done. So let's just refresh the page. Now tell me, is a moderator. So let's put these back these Heather now, everything should work. Fine. Let's go back. Refresh the page. Look, told me now is a moderator. Okay, I'm gonna change him to guest. Okay? Refresh My database. Told me now is a guest. If I put told me again as a moderator and now is a moderator, okay? And that should be it. Now, let me just look out. Okay, let me log in as told me. Okay, Tell me at email dot com and I'm gonna put 123456 Log in. Told me now is a moderator. Let's go Two blocks. And now I could create the block. Okay, so let me go back lugging s. Ah, John. I mean, at email. Don't calm. Okay, now, this should be all goods to update the users. Now, there's one more thing I would like to do, which is and I want to be able to, for example, delete the user. So for that, I'm gonna do in the next video, because this one is just running a bit too long by should be symbol, like 5 10 minutes maximum. Right, guys, that's it. The next one is whenever really the last video. So I hope you are enjoying the Siri's and I'll see in the next video 9. Deleting Users: All right, guys, Welcome back. So this is the last video where we will learn how to delete users as a nod Mean, Of course. So the first thing I'm gonna do is, um I'm gonna go in here inside of my index dot PHP. Okay. Let me just get rid of all of these things. I'm gonna go in here where I have these button from before where? It says update, user. Okay, just below it. I'm going to create a new button. Okay, I'm going to create the new button, so I'm gonna put in here button. And now what I'm gonna put in here on these bottom is inside. I'm not gonna put nothing, because this is the bottom that I actually want to submit when I want Teoh. When I want Teoh, just delete the user. So I'm gonna put in here the type off submit now, I haven't hear the type of submits I'm gonna put in here. The i d. So I d is gonna be Bt and delete form, and I'm gonna put the clause off. I'm gonna put DTN beauty and danger. I shouldn't even put these class because you're not going to see these, but I'm gonna put the deduction in so we don't see these, But I'm also gonna put after something else which I'm gonna show you after. So this is the bottle that is actually going to submit everything into the database? After what I'm gonna do, I'm gonna create the new bottom. Okay, I'm gonna put the new button and these button. Okay, let me just put button length is in here. I'm gonna put a delete user. The late user. These should be the bottom that I'm actually going to click in. Here. Look, this one delete user. So let's put in here some stylings to these. So I'm gonna put in here the button of type bottom, and then I'm gonna put the clause off. I'm gonna put BT enemy tm danger, and then I'm gonna put Bt and model. Okay, Data togo. I'm gonna put in here data talk, go. He's gonna be equals two more, though. Okay, then I'm gonna put data target, they target equals, and now I'm gonna put example model. So this is gonna be my my model example. Mo don't Okay, so we should see these now. And when we click issued. Open a model. So these model does not exist at the moment. I just created in here the file for you guys. Okay, so these file Okay, I'm just gonna put in here. So, you guys getting copy? This is the whole thing. You can just pause. This is gonna be the same thing, like our new post. Okay. These kind of model. I just took these basically from the the bootstrap page. And the only thing that I added was Are you Are you sure you wanna Ah, the ladies user? I put in here on the top of warning, and then I just put in here these three buttons, OK, one. But then he's gonna say, Close one button is going to say in here, delete the user. And this is just an input where we're going to grab the value of where we are clicking on the moment. Okay? These stuff. I just grab these from bootstrap. Look, the same thing. Bootstrap motile gonna go in here. Um, I think I think it was these codes. Yeah. So these girls is just the same thing. Just grab it. You can just pause the video and copy these If you would like to right now. Now that we got these all of these stuff in here. Ah, we shoot require these model. Okay, I put these motile inside of my includes so back to my index dot PHP in here after I required the heather. I'm gonna put in here the mobile dot html now, OK, let me just go in here and just do a bit less of what scorn on. Yeah, like, thes is much better so I can see both things. Let me just refresh the page. I'm gonna click, and I can open the model. Okay, now I can do something on the top. Okay, I'm gonna create in here. Um, available called, countered. So counter he goes to zero Whenever I'm doing these loop that are here with this forage just before these finishes Where is my forage finishing down here? So this is my If where's my forage? My forage goes Until where Until here, Ok, just before these finishes, I'm gonna do counter I need to do the beach p darks. Okay, each be counter plus plus And I'm gonna explain you in the second wind where I want to do a counter. Okay. Every time I'm gonna do a loop, these counter variable is gonna just, um is just gonna increase. Okay, So I'm gonna put in here on these button, this one to submit the form thes button to submit the form I'm gonna put in here a the late . Okay, I'm gonna put in here after this. I'm gonna put the class of the league it, and then I'm gonna put a PHP echo, and I'm going to say counter. So what am I doing in here At the moment? Whenever I create these delete user button, you see these bottom That is completely heaven. Every single user, every single delete button is gonna have a unique class of the leads. For example, the 1st 1 is going to be delayed. Zero. The 2nd 1 is going to be delete one and so one. Let's just save these and make sure that I'm correct. Inspects. I'm gonna going to get into the top inside of my list. Okay? And now I should have a button. The leads. And look, this is the late zero. I'm just do some zoom so you guys can see better delete zero. Is this one for the 1st 1? For the 2nd 1? If I go inside of the online, I should have delete one and so on so I can know which one I'm gonna grab. OK, now let's go into our main door. Chess. Let's start putting some stuff in here. The first thing I want to do is, um I'm just gonna go in here and just dio um, if Okay, documents, dots, Queary selector. Uh, and I'm gonna do the Bt and model. Okay? So if we have these bottom of the bt and model, if you guys don't remember which one is the bt and model is on your index start beach be Where is its IHS? What is the beauty and marvel? Oh, is the beauty and model which should be on my model, maybe Pity. Handle it model. Where did this one? Let me just check on my index? Oh, it's just here. This one BT and model. Okay, thes bt and model is what I have these button that says delete user. So if we have these button on the page, it means that we are on the correct place. So this is what I'm checking in here after I want to look through all of these buttons that I click. So for that, I'm gonna do a four Loop four. Let's r equals 20 eyes going to be less than Documenta query selector all Bt and model. Okay, I want to look through all the buttons, not length I plus plus. So if you guys don't know, this is I'm just starting available. I equals 20 and I's gonna be less than all the buttons. So this is going to run exactly by the number of buttons. So these four loop is going to run four times four now, what I want to do in hearing to into these I want to grab my Documenta query selector all Bt and model. Okay, I want to select the current one from these loop. I want to add an event listener of click. Okay. And now I want to put the function I met up with the function and let me just increase these a bit more so you guys can see Okay, Document acquitted, selected all event listener. And now what I want to do in here, I want to Dio, let's gets volume. He's gonna be equals two. I think I'm missing something in here on my index. Okay. After these bottle, let me see if I have something missing in year. I wanna have after these buttons, I wanna have a name put. Okay, So after these button, I want to have an input. Okay. Is going to be an input with a type of hidden because I wanted to be hidden, and now I'm gonna put the value off the lead. So I'm going to copy what I have in here like this. So the value is gonna be equals two, the leads, and then PHP echo counter. So it's gonna be either, um, like the lit zero. Delete one. So we know in which button we are clicking on. So this one get value that I have in here, what I'm gonna do in here is just doing a document. Dockery selector. All bt and model. These off course needs to be the I. Okay, I and then I'm gonna do dots next sibling element dot value. Okay. And I'm just gonna do a console, not log of these. Get volume. Okay. So look, what's gonna happen in the year. Two guys, Whenever I click, I'm gonna open my consul in the top. Whenever I click in one of these buttons next to these buttons, there should be some sort of a input that is invisible that we just created with the value of either the lit zero. Delete Wanderley, too. So let's see. I click in here at the moment. Let me just go in here. Sometimes this is not loading correctly, so I'm going to go into the top, right click empty Kachin reloads. I'm gonna click. And now look, it's working the late zero. Okay, if I click this one, delete four. Okay, I'm just could increase the zoom so you guys can see so we know in which button we are clicking on at the moment. Okay, after these, What I'm gonna do is I'm gonna go in here and I'm going to do with document dots, get element by i d. And the idea is model value. Okay, so these elements that I'm grabbing is actually on my model. I have these input as well. That is haven, and there is a value of none. So whenever I click those buttons I want to set up these value in here to be these get value that we just grabbed from before. Okay, then. Okay, so this should be fine now. So after you do this after you do all of these you wanna select? Just put in here. This is my for Lope after my four loop. Okay, What I wanna do is grab my document dot career e selector. B t n the late model. Okay, where is thes bottle? These button is on. My model in is in here these bt and a little model. So these button in here you see from the model what I want to do in there is I would have then eventually Senate Aunts event. So aunts, even listener of a click, then I'm going to run a function. Okay? Okay. Now what do I want to do inside of year? First of all, I want to grab these value that is in here, so I'm just gonna grab it like this. I want to grab the value from these inputs that is already there. Then what I want to do is I want to make sure that on my index dot PHP you remember these button that we created? Okay, with the idea of btm delete form. These with the type of submit is the bottom that is invisible at the moment. But we need to click on it to make sure that our forms submitted. So that's what we're gonna do now on our main dot chess. I already got in here the value. Okay, Now I can just do document dot gets or the query selector. Okay, if you guys remember every time that we run these lope, all these invisible buttons, OK, they will have a class of the leet either of zero either of one or so ever, because we already got these value from before. Okay, I can just grab these documented query selector and then put in here. Okay, As these template laterals, this is gonna be the value. Okay, I'm going to call this one gets ah, button So wet gets button equals to this. I'm going to copy these, base it in here, and that's it. Now I'm grabbing these buttons, and what I'm doing is just a click. Okay? These make sure that there will be, uh, click event. So when we click the form should be submitted here. Okay, So what I'm gonna do in here is on the top. We should see these echo of the role and the user. I d. Because this is not a post super Abit. If you have checking here, these bottom is what What is the name of it? Um, I think I didn't put the name. Oh, it's gonna be this one. It should be the name, So Okay, I forgot the name. The name should be in here, So I'm gonna put what is the name gonna put it in here? Okay, I'm gonna put super delete, okay? I don't think I have these anywhere else. So whenever we're going to submit this, it goes to our admin dot ph being here. And we just should see these at the moment because we don't have a statement for that. Let's just see, I'm gonna grab these guy in here Telmo modes. So let's hear in our database. Tell me more Ease with the ah, with the i d of four. And with with the role of guests. I think because I changed it before. Okay, so I d of four and role of guests. Let's click on it. Going here. Hopes. Looks like we have another on main dot Yes, 20. Mental chess 20. Let's see what's wrong. We should be this one query selected, I think, is this one. Okay, we're missing a dot Let's just refresh the page. I'm going to going to tell Mohammed's delete and cannot read Click property of North. Okay, so we have a problem in here. Let's see, um, Documenta Christlike out an event listener. So bt and delete Mobile, which I think if I'm not wrong, is this one BT and a late model. Okay, let me just to a consul dot log of these Sometimes it could be just a small problem in here . So we are just going to debug these life. Okay, let's refresh. I'm gonna click open my console. I'm gonna click in here and on the 22. This is fine. But then 23 is no working, so let's see what's wrong in here. So get button Documenta queer pattern. I cannot re property click of No. So I'm also gonna do a console. The Lord of this Okay, it should be working. Let me just go in here click going here and no. Okay. Something is wrong in your let's see. What's it? Ah, I think I know the problem is that we for what? The dot OK, when you use the query selector, you need to put in here a dot because this is a class. Okay, let's go in here. I'm going to click on Tom Ahmad's click. Uh, we got another error. Where is it? Um, okay, I'm gonna put the dot in here as well. Okay, refresh. Let's click. And it's working, OK, it's working. So the problem is on our I mean that PHP. We are doing this Heather to redirect. We don't want that. So let's go in here again. Let's go and look. We got guest and we got four, which is from our town moderator. I d four roll guest. Let's around this. Tell me now. Okay, So let's go back. Refresh. The page told me should be with the idea of three and the role of moderator. Let's click Delete User, the lead moderator and three. So we are all good now. There's only one thing left in here to do, which is on our else. If so, if is set Super edit. I'm gonna check else if and I'm going to say if is set. Okay, This time I don't want to check this super edit, but I want to make sure that I grabbed these super delete button the one that is being clicked on. Okay, Super delete if this is being set via posed what I want to do. First of all, I'm gonna do it s TMT A statement I'm gonna do what PDO p d o prepare delete from users where the i d is equals to question Mark the i d. We just got it in here on the top, which is the target user, I d. So let's do our ste mt. Okay, execute what we want to execute. Is this target I d that we got from before? Okay, now, if everything is fine, we should redirect to the home page and delete the user. So let's go back in here. Refresh the page. I'm gonna just delete these user called Daniel. Okay, Let's just make sure that everything is on the database I got in here, Daniel, which is the idea of eight and guest. Let's try to delete these user. I'm going to click in here. I'm gonna open my mobile and now in here on these delete user, and when a click, are you sure you want to delete these? Use it. If I'm not sure, I click close. But if I'm sure I'm going to click the lit and look, Daniel now is gone. Okay, let's check the database. It's also gone. Let's delete these user called Carla now. So, Carla, let's go the lit to leave the user and it's gone blocks. We can create more blog's if you want, because we are add means and, yeah, basically, that's it for these course guys. I hope it was not too long, and I hope you could learn some things out of year. We learned how to do logging registration out toe, add things into the database, how to delete out toe edit them. We learned as well how to make sure that some pages are only available for certain kinds of users. And yet that's it for these course guys. I really hope that you enjoyed it. Make sure that if you enjoy that, you leave me a nice review as well because I put a lot of effort in here and that's it, guys, I'll see with the NOX course