Transcripts
1. Introduction: Hey guys, welcome to this exciting new course, applied web development with BHB. I am your instructor for war Williams. In this course, you will be learning the fundamentals of web application development using HTML, CSS, PHP, Apache server, and hosting tools such as Heroku and C panel. Of course all of that wouldn't be complete without tying it in with a MySQL database so that we can have a fully dynamic and data-driven web application. By the end of this course, you should be able to accomplish basic web development with HTML and CSS setup as XAMPP server, understand the role of PHP in web applications. Build a MySQL database using PHP, my admin, and all of that would help you to build a data-driven and dynamic web applications. We'll be looking at PHP PDO and how to style or application using the most popular CSS framework bootstrap. Ultimately, we would want to incorporate the use of source control through the US github and ultimately host our web application on Heroku. Most other courses will teach you how to understand PHP, hard to do the basics of PHP, but they don't really show you that enter and development process. And that is what we hope to accomplish in this course. At the end of this course, you should be empowered and equipped with all the tools you need to build any application that is asked of, you know, before we go much further, I want us to take the next few minutes to understand the tools that we'll be using and how exactly they work in the whole web development ecosystem. Now let's start with PHP. Php is an open source web scripting language which is used mostly for service side stuff. And it is short for Hypertext, Preprocessor or personal homepage. Each B is designed to be fully embedded in HTML and manipulate HTML as you the developer. So deserts. If you have any exposure to any other programming languages such as C plus, plus, C Sharp or Java, then you'll find that it's very familiar working with PHP. If this is your first time working with any programming language, then that's finally because we will be starting from the ground up. The next big ticket item would be XAMPP server. And XAMPP is really short for, well, the X really means any environment. The basis of XAMPP server is really what we call the lamp stack, that's AMP. And the L really means Linux, Apache, MySQL, and PHP or Perl. So zombies really just an adoption of the lumps stuck onto other operating systems such as Windows and MacOS. Since the L stands for Linux, and it is the most popular PHP development environment. It's a one-stop shop. You installed XAMPP and then you get the Apache server, which is required to host the PHP code. You get MySQL, which is a database server, and you get all the required libraries for PHP and well pearl, but we'll be doing PHP, so we'll focus on the PHP libraries. And more recently, the m also means Maria DB, which is a spin-off of the more popular MySQL database engine. Now here we take a quick look at what HTML and CSS and PHP will look like. Well, this code snippet has no CSS in it just yet. But there you see that you have some HTML, which is the black text. Then you have the PHP tags that were previously alluded to and some cord which would print some text inside of an HTML page. Traditionally HTML pages have the extension dot HTML or dot HTM. When we're using PHP, these files will have the extension dot PHP. And PHP tags will only work in dot PHP files. But as we go along and we get into more developments of then you'll see how that works. Now every step of the way we will want to track our progress and we will be doing this using source control and more specifically through GitHub. Github are by extension on source control that allows you to track changes to your project, collaborate with team members. If you have to work in a team setting instead of copying and pasting code fuzziness, upload to a central place that everybody has access to all of your changes. We can also manage project documentation centrally. Then of the big GitHub is probably the most popular and also free source control tool that is on the Internet and it's widely accessible, widely available to everybody. And once again, it's free of charge. The last thing I wanted to mention is Heroku, which allows us to deploy PHP web applications in minutes. And we can host the web application as well as the database all on Heroku. We'll also be looking at using the more traditional routes, which is C panel. I didn't allude to it here, but as we get into the course, we will see how valuable those tools are. Once again, we have an exciting journey ahead of us and I hope you stick around and I hope you apply yourself. At the end of this course, you should be able to take on any challenge that comes towards you in PHP web application development.
2. Project and Website Setup: no starting from scratch on Didn't deliberately haven't shown you a preview off anything that is to come because I want us to be in this journey together on we can all work towards , then seat as it involves. I know that we are the ones accomplishing all of this. So the first thing that we want to do is find our server folder having already installed some on we go toe see zump HD docks on. Then we create a new folder inside off this folder. So each time we have a new site or a new Web application that we're going to be building, we want to create a new folder on that folder will be our baseline for everything else to come. So the first thing I want to do is create this folder on I'm going to call it attendance. All right, so that is our new project attendance. The next thing I want to do is open this project in visual studio court. Someone's going to open a visual studio code on get that synchronization underway Now. I had a previous project open and that's fine. You may have been doing other things that's OK but you can always get started by going toe file goto open folder on. Then you find the folder which is sees some Ishi docks and then you select the one that you know. Is it going to be your baseline on? Then you select on. Then you let visual studio cord reorganize itself accordingly. We have a very empty folder. So this is what your interferes is going to look like at the end of it. No, by this stage, already comfortable with the pH B pattern, Andi, if not you can always backtrack to my previous videos where I went through step by step with whole pH. B interacts with each hole. We get everything working now, the first thing that always goes in tow. Any PHP website is and is a PHP page called index. So Index is always going to be your first file should always be your first file on that would represent maybe your home pitch. But once again, every Web server always looks for a file called index to Lord first. Next, I'm going to go ahead and build all the full of structures because for other files that aren't necessarily website faster don't want everything happening at the same level, so I'm just going to bring them out into different folders. So I'm going to add a new folder just by clicking this new folder button here in Visual Studio Court on the first folder I'm going to add, is what we call includes. So my includes folder will have. Well, we went to include and require. What happens is that sometimes we have external files, maybe helper files with, like, a Class or Leo Files. We can put them in here, and then we just referenced them as we go along. So haven't includes folder. I'm going to create a CSS folder because maybe we'll have style sheets. I'm going to create a scripts folder on Be careful when adding Fuller's because if you have the polar selected and you click new folder, then it's going to turn at it. Underneath that, so is so just a click. Make sure nothing is selected and then you click new folder. All right, so I have scripts because maybe we'll have J query or JavaScript code running in our sight on going to create one and call it db db Short for database aren't because well, maybe I want the court files that will be interacting with the database specifically in a folder. So that's why I'm doing this separations. Anything I you see at the root here or call the route off the folder would be a director upset file, and everything else in a folder is just for referencing purposes. But once again, this delineation is occurring because you don't want everything in one place. Now the first thing I want to do is start doing my nails for my Petes. So in previous videos, I would have mentioned that you can always do ish email call on five and you get what I call the HTML skeleton that every webpage should have, at the very least, fire. But then I want to take this a step further, and I want to use bootstrap for my loads. So just as a reminder, we can go toe boots, get bootstrap dot com. Andi. There you will see all of the documentation relating to bootstrap chicken is Go ahead and click that on. When you want to documentation. You see all of the all of the documentation as it relates the bootstraps, so I could actually just use this skeleton where they're giving me the same tugs assist under. But they're also giving me the stash. She references that I need four bootstrap so that once I put that in my sight, any shim a piece that I have that many interference of these automatically has bootstrap built in. So I'm going to go ahead on and copy this cord from the bootstrap website. Andi, I'm going to replace it. Use it to replace the existing code in visual Studio court on dso. No, I have Ah, bootstrap. Ready in decks page. Now, once again for every piece that I would like to have these styles and all the bootstrap cord, I would have to actually copy and paste discord each time on day. One way that I'm going to reduce the need for that or eliminate the need for that is to start using My includes someone to create to file Syrian. Includes on going to call one hitter dot PHP on the other one on, still in the includes folder on went to call their own. So you can always click the folder to make sure that you're selecting the folder so that when you add new file, it goes directly into that folder. On the other one is footer dot PHP No, I want my loads to be a one time activity, meaning I want to set up My reference is one please. And if I have to sit up enough by, I want everything to be one police so or do it one time. Rather, So instead of doing it every time I have a new page, I'm just going to do it one time and make a reference as necessary. And so I put the cord in my index speech. But what I'm going to do is remove it on segmented into the relevant pages. So my head adare Ph. B. Should have called. That goes at the top off the page on the footer dot PHP should have called. That goes at the bottom of the page on this court should be called. That doesn't ever change on a page, so you'll notice that most of says they're consistent. Every piece enough bar looks the same on the footer looks the same. I had The filter is the last part off the beach. Most websites are standardize that way. So we're going to join that revolution on. We're going to make sure all four sites are standard or or site right now is standardize with awe again across our Peter. Sorry. So what I'm going to do is take from body because every p it starts with body. What comes in between body is unique to the page. Alright? Someone to take from body were all the way up. So from doc types of body, I'm going to cut from the index speech on understand to place it in the header peach. All right, so every page that references header is going toe automatically have that at the top of it on then in the same fashion. I'm going to take everything below. What? Whatever contents are in our content is just Hello, world on. That's fine. But everything below that should also be standards. I'm going to take everything below. Whatever the content would be caught that and place it inside off footer. All right, I understand the species don't a bit. So all of these javascript files need to be referenced on the page on all of these tags as well as the style sheet for bootstrap. They all need to reference on the page. So then that leaves the question. Where are the references? So what we're going to do is open a PHP tug at the top on inside this speech, Be talk I'm going to see require. So we looked at include and require on the differences that include will try toe, find the file. And if it doesn't, you just use a warning and say, Hey, you know, I tried and I didn't find a fire. So I'm sorry, whereas require will stop your website and see Hey, I can't find this file sandwiches require and then you have two options are requiring two options for include which would be the once they have include once and they have require ones. This means that if by some mistake I accidentally required to are included, two files are the same. Fire other twice. Then be if you will actually try to include it twice. However, if I say include once or require once even if I repeat this directive to require the sea and file elsewhere, you already included it are required it so it won't do it a second time. So That's the difference between the the require and require once and the seam is for include and include one. So I'm going to go ahead and see require once because I really only wanted one time on. Then quotation marks on I just in with a cynical done so that I don't forget. But inside the quotation marks, I'm going to put the path to the file that I'm requiring. So at the top off the page, the first file I'm need to require before anything else, even if I have other things I want to require. The first thing I should be requiring is my hitter, because I hit her has in the top part off May each dimmer body on Bush, potentially whatever content comes at the top off the page, All right, so I'm just going to require once and I'm going to say includes because that's the folder. All right, so the files header and footer are currently inside includes Fuller. So to get for them, I have to see includes on then say slash on then hitter. Not PHP on then I can just copy this because it's the same concept for the footer on I'm requiring once the footer. So I'm just going to change this reference the footer dot PHP So as many times as I have are as many files as I have in the includes folder that I need on a page. I can just reference them in this same fashion off course at the appropriate points. All right, Another thing that I want to do is make sure that I'm setting the title across all the pages because it's set one place here on. That's a static sitting here. But if I have another page like when we get to actually building the register and viewing stuff and so on, each page is going to have a different title, they all can say hello, world, Someone to me that the dynamic by introducing a variable that I'm going to call title at the top on I went to see Title is equal to on this Oh, I'm sorry. I'm declaring it outside of the Ph. B. Todd. So you notice that the color coding was not There was just white takes because he would have rendered in HTML. So I need to actually put this in between the PHP tags All right on, then. I'm going to be clear it before the require on that is because remember, in programming, everything is done in sequence. So if I referenced title inside here, which I'm going to do, I'm going to say I want to print title and I can always just pretty static text. Well, I can't. Just pretty tight. That would literally print dollar san title Softworks after I want to put my static take. So this is attendance that's up Name and then I'm going to see it dash. So that's a static text. But in the dynamic parts, which is title, I need to wrap it inside the PHP thugs. Someone to see open that Ph B Echo title on Echo. So you print. So I want to print to this. Tug the content off this variable, and I'm just going to close this quickly. All right, so they're all married. Lines are gone, so I'm going to always have attendants dash, but then relative to the value off title, whatever comes out to the dash is prone to change. So on the index speech, my title is going to be index on. I declare it and set the value on then I require this so that by the time it includes this file title would have already been declared on initialized. And so it knows exactly what it is. The echo in this statement. Another thing I'm going to add to this project while we're on this topic is a CSS file. So there are times when we want our or little flare that bootstrap in its native Andy for state will not provide. It does a number of things, but it doesn't do every single thing. So what I'm going to do is add my own CSS file, and I'm just going to click on the CSS folder, Click New File on. I'm just going to call it site side, not CSS, And you can use any special characters in the fire naming society, not CSS. All right, I know it's blank and I leave it blank because I haven't found any use for it just yet, But I'm still going to include it in my header that BHP. So I'm going toe underneath the bootstrap section. Add a link link row equals style. She it All right on, then the interests is going to be the past to the file, which is CSS slash onda fire Name is site dart. CSS are. And then I can close this. I don't need all of these attributes. These are the two major attributes that you need when you are including a star. Sheets are so having done all off that When I lowered my index page, I expect to see reference being made to bootstrap reference being made to my style sheet. The title should be reflective off whatever value I put their aunt they were going to see Hello world Onda. We should see a foot or so. I'm going to modify the footer a bit on. I'm going to add, uh if Andi I am going toe print inside off this Div. Let me see what's good prints. I'm just into put the PHP tug on, then went to echo copyrights. Andi, I think I can do something like it's why. All right, I'm going to contact you. Needs used to use a full stop some printing the word copyright on then I space and then I'm calling captain eating the year the deeds, right? The year so be it. Why should give me about the year. All right, so that's what I did there. And then I'm just going to close this def. So I'm going to preview this site as it is. I'm bringing in my browser, open a new tab on, Then I'll navigate the local host. Which brings me to my some dashboard. But that's dashboard. I don't want dashboard. The up I'm looking at is my attendance up. All right, so attendance on, then that should Lord. All right, so we have on error here. Oh, I made an arrow in defining the deeds command. The era was that I should have put this in quotation marks, so I just fixed that, and then we go back and refresh on. There it is 19. I guess 19 is 2019 right? So you can look our own the the manipulation commands. And I have a video on that. You can backtrack to my video on the D time manipulation to see exactly how we can get different values printed out, has released the deeds. Now we're going to make one more adjustment on that is toe actually not have our content stopped onto the left margin like that. So What we want to do is make use off bootstraps. Uh, they call it the Container. So I think in layout, you see something about how we can use a continue narrative class that actually, you know, kind of center lines are content in between the margins. The extreme margins off our page aren't on most most websites kind of flew by that. I don't always go extreme after extreme right there. Like leaves some space in between. So I'm going to just modify my header on footer to accomplish this. And there is someone to do both. Is this What would happen is that I could say give here, Andi, close the div. So my content is in between these lives on. Then I can say class equals container on what this would do is actually invoked that class our own. Hello, Also, when I refresh you see Hello world move on The copyrights did not move So my footer contended moved But my page content moved the this please find this works But then the don't side of this is that when I have another page and I need to seem effect after keep on saying give class equals container on, then put in my content. So what I'm going to do is actually take this old. Since this is going, how is going to repeat? And I always want our pages toe Have this court in it. I'm going toe cut this and please it as the first thing in my body. Tug. So we're opening the body tug here in our hitter on. Then after the body talk, I would have put this contain a class anyways, So I put that inside the hitter so that every time I include ahead or require the header, this is implied on off course. I need to close it. So I'm just going toe always close it as the first thing in the footer. So, uh well, yeah, I want to put their content also get pushed away. So this is inside. This would be my footer. So if I just give it up on i d So I know what it is. A boat I'll say footer. So on ideas. An excellent way to identify what your elements are. Four. So I just have a random Dave Andi, I just to go out to remember what it's for So if I say Dave on, just give it an I D footer, I'll always remember that old that's what This devious four. So that's the Po off using ID's. But further to that, I'm going to put this closing live after the filter tive all right and understand the tablets. They can see where everything starts and stops. So this closing give seems random in this speech. But then when you look at it relative to the header, it's opening here on its a glass container on its closing here in the Footer on DSO. When I refresh my page, I expect to see that my foot or content will also join Hello, world also inside that container. Alright on, then just still close this out. I'm just going to make reference to the paid source and we see that everything fits together. Everything is snapping together as if we had one big Asian appears. So all of this from line to straight up the line 17 would have been a referenced inside the hitter dot PHP file and then we have our content inside off index and content content content on. At the end of it all, we have or closing part, which is all constant in our footer PHP. So once again, the poor off doing Leo's this ways that you can make the change one time in one file, and it ripples across every page that makes reference to this one file. Now that we have our Leo, it's don't. The next thing we want to do is start designing a form and start looking at the different methods off submission.
3. Create a Bootstrap 4 Form: Hey, guys, In this video, we will start the saying or first form using PHP and bootstrap on the Schimmel and all of these wonderful things that come together toe allow us to collect data from users. Andi leading towards storing it's for later use. So we already started designing. Our site we created are full of structure putting our fathers and our references. And we kind of segmented or cord toe have that kind of Leo's who can feel so know what we're going to do is start modifying over index speech to be, ah, form that collects data and we look at using the get submission for or form. I'm just going to start modifying the contents off the page on I'm going to instead of printing Hello world. I'm going to see registration for a conference. So we're creating on I t conference or districts on form where we want all of the would be attendees to start putting in their information and declaring all you know, I'm a DBS so this we get an idea off. Who is that going to attend or conference on? We can store it up for analytics later on, so I have this. Each one tug at the first thing on the page, and it's always good to have on each one tug not only for readability by the user to know where they are, but also for search engines and love. Each one times on pages on you get higher around kings for these kind of things. I also want this each want out to be in the center off my pit. So if I take a look at what happens here, I just refresh. No, I have it seeing registration for I t conference. So instead of just seeing that I'm going to or instead of having it to the left, I'm goingto center align. It's a bootstrap as a class on Do you can always go and look at the documentation for yourself I've been using for us. It has kind of become a part of me, but always feel free to go back to the bootstrap website. Look at the documentation. If it is that you're not sure hotel complex something. So I'm going toe center, line it, and while I'm here, I just try and find that reference. In the documentation our city will go under utilities and look for text. Then the first thing that you see sticks alignments. If it is that you want to move your text their own with it to the left, to the center, to the right, or you know that different ones. Then they give the court example, so you can use a class text left to push it to the left tech center to push it to the center, etcetera, etcetera. So it's really simple. Want to catch on? You remember them quickly, so I want to send our lines, understand to see a text center. Another thing that I want to point out is that even though the example may have been with a pita, it applies to anything that is text. So it's a pita example. Yes, on were using it on an H. One time you can be used on almost any tug that has to do with sticks. The fact is, it will take any content off that on the center, align it, are aligned it accordingly. All right, so I don't have to worry about the example versus the type of tug you're using and what did it. I just highlighted this and you shift upto push everything back to the left column. I'm just a sick stickler for for indentation. So next we're going to do ah form. So to do a form, we have Italian, a general called form Onda. Understand toe open and close this quickly as forms can have quite a few things, and you don't want to forget to close your tacos. That can lead to some annoying and irritating debugging issues. No, to design my form using bootstrap classes understand too quickly. Jump over to the documentation on find. I think that would be in components. Find references for forms. There we go. So they give us some sample forms. They say, You know, if you use my classes, this is what they're foreign can look like with relative ease aren't and they tell you the different controls that you have a Nikon style on the size ings. They have quite a few options. If you go through on, you can get quite a bit off. Inspiration for a formal layout are designed from their examples. For today, however, we're going to keep it simple, so I'm just going to go back up to the first example that was there where we have the label , what does he want? And the specific control for it up to the submit button. So I can actually just take a look at this score and see how it's done. I'm actually just want a copy. This scored. Go back to my site and replace the form times that I just so cheerfully puttin on. I'm going to replace them with all of that court on. I'm just going to spend a few moments here on d get all of my indentation proper so that I know where everything starts and stops. All right, so I have my form talk, opening and closing here on. Then you notice that there bunch off, gives in between. All right on, then, if you look inside off the dibs, you have the label, which is the word that is printed. Seeing this is what I'm prompting for. So this one was the from for the email address, and then we have the input type. Andi, initial five. You you have quite a few types available to you. In this case, that type is e mails that actually runs specific validations on the email. Just to make sure that you're putting in an email address out of the box without any additional cord. Andi. Some other attributes. And then they give you a small where did give you a tip. So if you just look at the court versus the example, then you kind of see where everything is. So we have email address on that was in the label section you have the the text box on. Do you know that one has in ah, placeholder. This is enter email. So that's on attributes Placeholder that uses that kind of greedo takes that disappears when text actually goes in on. Then under here you see that there's a little town here that says you never share on. They show you the cord to get all of that. So the fact is, you copied it. You may not need it. You can remove underst reform as needs be. No, I just added a few comments here, or one be coming here with the different fields that we intended toe collect from our would be attendees aren't so wants to call it the first name last name the date of birth and will be using a date picker will take their specialty on because and these are the specialities that we're catering for. Andi another's at other, just in case. All right, on then. We want the email address under contact number, and that is what or form should be designed to collect from our users. All right, so, no, we'll start butchering or start modifying. I'm not, say butchering. We'll start customizing this code so that we can accomplish our mission. No, I'm good. I'm giving a template. Be as creative as you need to be. If you want to try something else, that's fine. If you run into difficulty, always feel free to reach out to me. I know there's certain things about this school that I'm going to retain because that is just how bootstrap I lows us to take advantage off its design. So, for instance, you notice that you always have a diff class called form group on inside this Div. Then you have whatever else goes in tow that form group, meaning the input type on the label for it and anything else. So each one of these would get its own give with class form group. So I need I already have email address. I'm not going toe delete the email address. But what I'm going to do is copy this form group and then went to create another one. All right. And then I'm going to modify this other one. So I have email address here already. I'm not removing email address, but email address is not the first thing on the list. It's like the second to last. I still have at least four other things above it. So I'm going to keep on putting things above the emitters until I get there. So I'm going to change the label. Firstly on, Do you want to change the lead before on the i d. Because what happens is that in html five, they give the label for an I. D. Thing. Well, really bootstrap, because when he quit the label, it kind off highlights the contra widow to clicking the control itself. So watch as I take the word password. This highlights. So you want that label four on the I d. Too much to get that kind off effect. And you don't want to repeat the ideas. You never want to repeat the ideas off your elements. Gerald, Your your your website. So I'm going to change this one to something more meaningful because I'm doing first name Andi. I spelled first wrongly. So you want to make sure you get all the spilling squarely on day? So that's the idea. I'm going to change the label. Four attributes toe first name. I'm going to change the labels. Text from the military's toe First name Andi. I'm going to change the anything here that had to do with email and basically going to change. Thought relative tow it being used for a first name. So the input type here is email. First name is not an email. It would be more text Onda, I'm going to change this Emir, Help toe say first. I am actually I've never I don't think I need this. This attribute is not absolutely necessary. So I'm just going to remove that one. I don't need that one. All right, I want to remove this This this small text Alright, I don't need that so pretty much if you just want a label on the text box. This is what is going to look like. You have your live with form with class formed a group and then you have your label, and then you have your input, and I'm going to change the placeholder text to see Inter. First name on, really? And truly I don't need placeholder text if I have a label. So once again, it's up to you on your design. All right? I don't I don't like seeing a placeholder when I already have the label Someone to remove that one. So this is what it looks like. If I want a label on that takes box on this is for the first name. No, to call it the last name would be pretty much identical to understand, To copy and paste this on change everything out to be relative toe Last name. All right, so no, I'm collecting for last name. I changed the label text. I changed their the four attribute value and the i d off the text box so that they work together. Andi know I'm collecting for last name. Next. We want date off birth, so date off. Birth undergoes Quintal. Continue along the same line on the four is going to be on understand. To read you will be for short. The idea here is deal be did off birth. The label text says, Did it off birth, birth within a on. Then the type can be. Actually, it's picker because html five actually has will beat. Sorry. So the data type the input type date actually gives us a date picker on. We're going to look into if this is the best way to do it or not, but for no, that can work. All right. Next, we need another control because we have specialty, which is going to be more like a drop down box, allowing us to select. So if I use a drop down, obviously you can only select one off their above. Who could use a radio button on diffuse the check box. Then it would be a loss, a select all at least one are all off the potential values. So that's up to you and your conference for my conference. I believe that your specialty is only once I'm going to give you a drop down box. All right, so I'm just going toe copy this once again on I'm copying and pasting the Suspiria miss off retyping things that I don't have to re type. Right? So this one, the labor is spare shell T Onda. I'm going to see a specialty, our area off expertise. I can always meet my labels as worthy or not, as I want that the text that is being printed to the user I'm going to change the I d. Well, I'm going to change this. Who will control? Because a drop down box wouldn't be involved using an input, Jack someone to just go over toe bootstrap and see what they have for me in terms off drop boomboxes. And there they actually have it. They have the foreign control section where they're showing me a drop down list, and that's what it looks like. Once bootstrap is applied. All right, on the example for it is right here. We have the form groups, so that's basically our constant they have the label on. Then what we have is a select tug with options in between. So I'm just going to copy this out because we have everything else, all right. We have everything else in police, understands a copy of that select section and then spend some time fixing our indentation here on then I'm going toe leave this class. You notice all of them Have that class form dash control. Right. So with all the foreign dash control, they wouldn't look as dynamic as they do here. They don't. They don't have that bootstrap look and feel, and we'll look at that later. So I'm going to select administered a tug I'm with to change the i d. Here till a specialty. Right? So that's the label knows to work with it. And then I'm going to put in my options. And my options once again were database admission. So data base add mean ah, software developer on Web administrator and other. So just type those in quickly. All right, I know that is done. So we have or drop down list. I know we haven't been looking at it. I don't know if you have been, and that's good. If you have been I haven't. So we're this building blindly and hoping that everything snaps together as we would like it. And if it doesn't really just fix it as you go along. So nowhere at the email address. And once again because we took that example, we already have email address I'm just into sure it upward. Change it up a bit because it's a bit too wordy for me. So label for email on the controls ideas, no one to be email so that these two Noto work together. Andi, I'll just leave everything else here on this would actually be a nice little disclaimer. We'll never show your email with anyone else because you ready stuff for my conference. I'm not. Want to use your email maliciously. You know, you could use this little all right on. Oh, so this idea and this attribute go hand in hand. See that? So I I didn't remove it from previous times, but here is why bootstrap would off kind off. Put that there are taking experiments with them. The next thing that we want after email is the contact number on. I'm actually just going to routine the email cord on going to reuse it for four number because, well, I'm going to show you Why So for eso the label for his fall, the forms, the controls I d is full on, then the labor is going to see well, contact number. All right. I believe that there is. I'm going to use text. So there were still me. The text box looked like it's supposed to collect a four number, but we're not there yet, so I'm just going to use text on going toe. Remove the placeholder on. I'm going to do the same for the placeholder above. Once again, I don't like having both the level and the placeholder. I think they should be used interchangeably. That's personal, though. On I'm going to leave this help text that I'm going to say full help instead off email help on the CME here on then I want to leave this help text and I'll say, Well, never share your number with anyone else So we have to privacy disclaimers here that we're going to minty in the privacy of your phone number and your email address are no, that we're finished with our form because we have everything that we require. We can the screen of the rest of the court. So I think part of the example that we took also had this text box for password, which we don't need, at least not yet on this check box for an example also, so we can remove that Onda, We have a button which is off type submit which involves a submission attempts to the to the browser on we have class equals VT and Bt and dashed forever, which gives us a nice blue button on the submit form. All right, so you could always customize the secret changes. Start green button by saying beating that success and see register. It's up to you. Personalize it as you wish, but for no, we're going to go ahead on a preview orbit. So this is where we left off on little See where we are. No on Look at that. That is lovely art so ever each one time nicely center line. We have our first name. I notice when I click just the label each control kind of lights up Once again I was the label for and the the i d at work on then That is what our form looks like. We just built a form, and all we did was doing the way bootstrapped it and look at the beautiful. Oh, come all right now I'm just going to play around a bit and show you other things that we could do Firstly, if I take all the form, control class reference that make sure you what happens then our textbooks looks like this , which is just the standard and default. So that is why we have in that form control class so that it takes on the bootstrap attributes its understand to introduce that if I wanted my button to not be so small. All right. It's kind of lost all the way down there. Then I have a class beating dash block on Bt and dust block meets the button stretch right across the peach. Alright, Andi, I want some space between my submit on my copyright, so I can probably put in a few br tags, so understand, to put in a boat about four or five on, Then you notice that you have that space so we can go back and fix the footer later. But just to show you that whatever content you put in the index space between the hander the header includes underfoot includes it will render that we are on. Then if we look appear in our title, we see attendance dash index, And remember that we're sitting that value in the in this top section of the site on hitter is just making reference to its No. One more thing I want just to look at is our DEET control. So we had used for date off birth the day it ate a type. All right on. Then, if you just take a quick glance at it, you actually see where it looks different from the regular textbooks on If we try to change these values we see that were confined or restricted to only put. Indeed, it's related, but there's other takes. Can't go in there, and that's perfect. The danger, however, is that this eight control is going to get rendered differently relative to the boroughs are being used. So in this situation, our chrome browser is going to give us a nice control calendar control on. We can just let the date, however, in my edge, explore when I click this, I'm getting a different type of control, all right? No, they all. They both work, so it's not a matter of functionality. However, they work differently. Andi. I can see where this is going to lead to user experience issues or even support issues, because if you are used to seeing your website in cruel and somebody cause in seeing it's not working properly an edge then That's a design issue as far as I'm concerned. So we want consistency across over website on in all browsers Now what we need to do to maintain consistency across all browsers with our date picker or did control used to actually reference a deep picker and that one of the more popular and easy to use one's would be from Jake Worry you I it's understand to gula Jake, where you I and it will just go to the website on here. They have a bunch of what you call widgets. You can get a kardian controls auto, complete all sorts of things. So the one that we're interested in is the date picker. So we just look at how we go about including that on. Give us a quick example where you click and you get a date picker control, no matter what the bro's er this is how it's going to look on. That is what we want. So we just click view source on, then the show us kind off what needs to be included, right, So we need a style sheet on. They actually give us some seedy and links. So we need a style sheet and we need to script files. I need a reference toe. Jake, Worry on. Then. We need a reference to Jake. Were you y know what happened? Is that when we took the example from Bootstrap in our footer one of the scripts being referenced is J query. So we don't need toe include the Jake or again, what we just need to do know his reference the Jake where you I sections. I can just copy this, Andi Well, back to my foot or cord and paste it another space it as the last thing. So once again, order mutters, we need to make sure that we don't place it above the original geek were friends. Because there are things in this that rely on court from this. So this has to be rendered first. Before this, we can start to work next, we can take this script example. This scored block where what it is doing is invoking a function on it is looking for some elements of the I D date picker. So want to see the hashtag in general scripting Means is looking for on elements with idee on. Then the idea is looking for would be, in this case, the speaker. So we're just entity it. That and we're going to paste it underneath the reference to the J quarry called So once again order marchers. I can't stress that enough. All right, So if you don't follow the order than you're probably going to run into problems problems and referencing problems on, then I'm going to change this because we don't have any element of the I d date picker. What will you want? He is to get our element called the OBE. All right, So the enemy with I d d o b that is rule on the date picker. So I'm just going toe change this I d to deal be so we'll look for the element Do you will be once any pages loaded on it will tried to invoke the date picker class, which is Ah, sorry function, which is a form shown being called from this scored which has dependence is on stuff in this. So that is why the order marchers once again on then if we look into the rest of the example we see here that the input type is text, not be it. So we don't have to set it. A deity can be text on the i. D was there picker. So I already can establish the father. We need to change the i d. What I'm going to do now is change the type from date toe text. So here it is debt on. I'm changing that to text on dial. Save on one more thing that I didn't take over is the style sheet. All right, so this style sheet actually is what will make this look like this? Right? So we have to include that stash. It's understand, toe, take that chord on. That's a Cdn linked to the stash it somewhere. And I'm going to go and pieced it inside off the header, and I'll put it after the bootstrap stash. So have this bootstrap star. She didn't have the Jake or stash it, and then we have over personal stash eat. All right, so when we re fresh and try, we would notice that it is not working. All right, so when we want to debug when our like javascript on stuff is not working. We can always right click in the empty space and click Inspect on then. That kind of brings up this this debugging section where we can go to console and see what errors are being printed on the area that is prevalent here is that it is saying that there is something that it is trying to access from the geek or U F. That is not available. So remember, housing order mutters, we have to make sure we re friends The pier and file which is ghee clarified before we were friends the you I file which we did. But it is seeing that it cannot find certain things on one off the aero seriously and that being picker is not a function on it is referencing this J quarter file that we have already included. So what is happening here is a is a matter off version. So we have the latest geek wear, which is three point something point something. Whereas if we look back at the example, we see that it is relying on J query version 1.12 point four. So that is the root of your problem. So in other words. It is saying that you're trying to reference something that is not in the current version. And that's fine. So what we are going to do is just make sure that we reference this as the example would have indicated. So we're going to take this reference. We're going to go back to our cord on. I want to put that reference right here above the G query reference. So this is our Jake. Where you a reference. I'm putting that one right above it. All right, So we have the latest version, which is a query 3.3 point one on that is being referenced. First on, then this one is being referenced afterwards on just in time to load the Jake worry you I and then all of these references occurring alot date picker or should l o date picker gun control toe work. So if I go back and I refresh, you know what is that? All off my console Arrows are gone. I have no that The website is not complaining about anything. No, on then if I click in date off birth, I get my control. All right. On that is what we really wanted. So if I go back to Internet Explorer and refresh, then my control, my Collender looks the same way. And that is what we want. That level of consistency, Andi, accountability to our users are on. The great thing about this is that it doesn't follow any of the keyboard input apart from maybe numbers, but they can't put text are very, very random things in there. So you can actually kind off prevent bad data from going into your application using these controls. All right, so that's it for setting up our form. Andi, I hope you had fun. I hope you learned something. And if you run into any difficulty, just let me know and I'll be sure toe reach out to you.
4. PHP Form - $_GET Action: Hey, guys, congrats for getting this far. I hope that you have been enjoying the journey. And don't worry, it is going to get even more exciting in this video. We start looking at life after the form. In other words, when we actually click submit, we expect something to happen because there is no form on the Internet that when we click submits, nothing happens. Something always happens, and today we're going to see what happens and learn how to control it. Going back to our court file, we will start making modifications to the form tug so that we will said two things. Well said a method on on action there. Two forms of mission methods. One is get and one is posed in this video. We will be looking at the 1st 1 as get I'm going to modify my foreign tired and I'm going toe add on attributes and this one is called my third on. The value for this attributes is going to be the word get. No, you're probably wondering what is posed, what is get on. Why is this relevant? And I'm going to bring you on a little field trip toe kind of context Realize what get does . So let's goto a popular Web site like youtube dot com. Now YouTube is a very powerful and very popular video streaming site on. As a beginning developer, somebody wasn't worked on any projects off any real magnitude. Up to this point, you're probably Can't you two been wondering Wow, how do they do what they do? So I'm going to break it down into simple terms. Andi will see contextually what they get. Those and whole YouTube uses what we're about to explore. So up here you have the search on. Maybe you've realized by no, or maybe you knew before. But this search is really just a form. We are creating a form. We have a text box and we have a submit button. You, too. They have a text box and ever submit button they to see him Concept. If I search for video on puppies, I'm typing in that value. And then I click the search button, which is like picking or submit button here. What happens is that it goes to another page, this other pages, what we call an action. All right, so we're going to look at the action and where also can't get. So the next page that comes after submitting your form is called the Action Page. And then, if you look in the browser browser, the text in the brochure. Sorry, then you'll see this question mark, and then you see some value and you'll see some. So you see a question mark and you see some values. What this is is a result off our get action. So they get sorry to get method. So the action is the next page that comes after the form. All right, so in other words, when we click submit, we expect to navigate away from the form, toe another patient, See a you're successful or something like that, right? That's our action. However, the values that you're seeing here would be, as a result, off the values that were submitted by the forms. In our case, we wrote puppies to be submitted. All right, so puppies was submitted. When we click search on, then what you see is what you call a quarry result or a query. Sorry that the word is slipping, but these these are quarter results appearing in the U. R. L so when we use get it actually creates that question mark and then it passes every variable that was in the form as well as all the values. Alright, so that's a practical way off. Whole get is used on whole actions work. So we're going to be modifying our form such that when we click submit, we move away to another page on. Also, we will see what happens in our Earl wants the get method is being used. So we modified and we use the method get The next thing is that we want the action. So I'm going to say action as another attribute on. I'm going to say the action is success that PHP So the action page? Well, one, we need to create it because up to now we only have one ph b pay just really doing anything I understand to collapse all of these folders. So we see that we really only have one page, which is index. So we need to create another pitch called Success that PHP. So what happens is that when you click submit is going to see what page should I go to next ? And we would have defined that page right here. Success that PHP. So I'm just going to go ahead and create it. I'm just going to go over to my explorer pain and click new file. And then I'm going to say success that PHP on, then just the same way I want on my pages. Pretty consistent. I'm going to take the require court from Index, and I'm going to pay sitting success. All right, So I don't have to do much work to meet my pages look alike. And I'm just going to do the same here. And I'm going to copy these br tugs. And as we go along, we refined the design, So just work with it for no on. So in success lot ph. B. I'm referencing the header on the footer on. I just need to change the title here because this is the success. Paid someone to see success here. All right, No one more modification is I'm going to put on each one tag here on this is going to just print. You have been registered. So when they fill out the form and then they click submit, we want to let them known that they were successful. So we're just going to say you have Bean Mr Bean registered. All right, Andi, once again, I'm just going to use my bootstrap to easily move my content. Our own someone to see takes the center. Andi, I believe that bootstrap has something for green text. So, you know, color coding kind of helps give visual cues as to what? When something is an era versus when something is good someone to use text dash success which I believe should give a green old put on we can We can always test that theory. But essentially, that is what our success speech should print. So lets us see what happens when, after doing these modifications, go back to my registration page. I'm going to refer, So he gets the latest cord on. Then I'm going toe fill this boat. So Mary Poppins on her days off birth is probably and notice that this this is not a limited at the date on the time or the money, specifically from the year. So we're going to modify that does. This is going to be irritating to users. If they're born in the fifties, you can see the problem. There And so I'm just going to go ahead and work with this date Onda leave for as a database. I mean, I m pop at gmail dot com on her number is 12 something random. All right. And then when I click submit Look what happens. You have bean registered. Isn't that lovely? So that is how we start seeing a whole forms work. So when we submit the next page that it goes toe would have been defined by us in the action. Alright? Success that PHP. I noticed that the question mark is appearing because it's said to get so because we're getting that question Mark is already there, But nothing is coming after the question marks. So no, we need to make some more modifications, toe or form to start getting content to appear in, or you are oh on. So we know that the get is actually functioning properly now going back to our core the next modification that we need need, need, need need need to make on Really This is in the context off PH. B and certain other service I languages. We need toe actually give our input controls what we call the name attributes so right? No, they have idea and ideas mostly used by Jake, Ori and CS sister, you know, name our control. So using the i d. Like we saw in the case off date off birth, we could actually say that the script should tar get any element with the i d date off birth. So we see that I d can be useful for the visual side of things, but for the server side of things, we need to use a name attribute. So when we use the name attribute, it's like we're simulating variable. So the same way we can just declare a variable here and give it a value, and we can echo that variable and it's all nice and dandy. The only way that PHP can actually access the value inside of for control is if we access it by its name on. The only way to set the name for our control is to give it the name attributes. So I'm going to go ahead and give first name, the name attribute and the name can be the same thing. So you don't have toe worry about what you're going to name it. You can have the ideas first name and the name is first name just for consistency. See? All right. So you can actually just copy and piece. The idea is going right across, and it has changed from I D to need. All right, so whichever method is easier for you, and I just do this for a few onscreen, but our continent the lesson, and you can do it on your own, um, four year old edification. It's also a good idea to give your your submit button name. Andi. I'm just going to do that. So I've given all of the other ones names, and I'll just give the submit button and name also because sometimes you want to actually call it by name by PHP to dynamically, um, accomplish things with it. So every thing that is in my form, that is a foreign element. Once it's collecting data or it is interacting with data and the submit button would fall into that second category, I am giving it a name. No, I want to go back to my form on refresh on. I'm going to fill out this form. Some were using Mary Poppins information on. Then I'm going to submit Onda. We went our action, but take a look at our you are Oh, so you can see that for each element. Whatever name we gave the element. We're seeing those values there. Alright, so first name last name, you'll be specialty email on phone, and you also noticed that you're seeing the values that were put in. So first name equals Mary. Last name equals Poppins, you'll be equals. And this is kind of four months because of special characters. Slashes aren't really alone in the euro, so it's compensating for the slashes, which is what it looks. A cryptic but specialty database, plus admin. That's the space right there on email in pop once again special character gmail dot com, etcetera, etcetera, etcetera and submit which, well, it had no value. So that's fine. So it will put the value. Whatever the value off some it would have been, it would be there. But the point is, for every name that you would have defined in your form, the values can be seen by our server side aren't so they get action is actually putting the get method. Sorry is actually putting all of the values here. And this is what you call a query string or quarter results, right? Anything after the question mark, No contextually for this registration form. This is harmless. If you are doing a using, if you're doing a registration form where they can use any amount of postural answer and sensitive datum data, you wouldn't want to use the quarry string here. All right, so that is when we look at the other method that does not pass all off your values on all of your valuable information right here in the URL. Publicly accessible. All right. No, the next thing is, what if I wanted to reprint the information that was just submitted in the form on the page ? So the context is that Mary just signed up and we just want to show her that this is the information that she entered while affirming that she's successfully registered. So what I'm going to do is modify my action page on I'm going toe, actually print coat everything that came back from the form. So on our page, we're going to start using PHP code. So, firstly, I opened up my PHP tag. All right, on, then inside off this tug, we're going to start using a specialized function on, uh, this one is called get. So this is what we call like a super variable in PHP. So I'm going to say a dollar sign, underscore, get all right and then open square. So it's really like a super ari super variable. Meaning on because I'm using the square brackets can tell you scenery, but then each subscript is really going to have the name off the controller that you define . So they literally the name that you defined is a subscript off this Get Ari. All right, so let me put that into context if I see get first name. All right on. Well, this is just going to get the values, actually want to print it. Oh, so I'm going to have to say echo, get first name. All right. So when I go back to my page on that form Phil, it'll on, then click. Submit. Then I'm going to see Mary being echoed to the screen. And it's printing Mary because it's getting the value that is associated with first name. All right, so then that is whole. You start getting back the values that are being submitted in the form you click submit it passes the test. Somewhere on that somewhere is into this super variable pretty much on you can segment this super variable by using the name off the control that you intend toe get the value for All right. So once again, this is just one big variable or Ari and you just get the specific ones by using the subscript name that is that corresponds with the control. You're trying to get the value off. So then that means I can actually use this and get all the other values understand to duplicate that line and see get last name. I want the deal be that is being submitted. I want what else is there was a specialty. They can go ahead and just feel these old off screen. I'll do it all screen. I need it on your own time. All right, So, having done that, if I go and refresh, then I'm going to see them all being printed and off course. We know how the Echo statement works. It's not when toe try and space anything for us. But that's pretty much know the wonderful thing about the get once against that, I didn't have to go back and submit the form. It is just whatever is in the URL is available in the get here at all times. So just because I refreshed on the values are already there. Then it just got about the values on. Look at this. If I change this toe my name and then I refresh the page. No, sorry. If I change it to my name and then press enter, not refreshed, you will refresh the page. We reload the page on because the variables are still there in the world. He would just get whatever value is in the variable being referenced. So once again, this could be useful. We see that a big web site like YouTube uses it, but they're just using it to show you the search results. They're not using it to pass over sensitive information at all. No, I have in another window example. So this is boots nip on. People come here and make you know little snippets that you can take inspiration from, if not bar altogether on here. I'm just They can't a simple, uh, profiler representation of somebody's information, and I'm going to use this as inspiration toe style hole over pH. Here we look for anybody who has just registered. Now I'm going to go to bootstrap site and I'm I'm going to use the card component, and I went to you draw inspiration from this to represent the the registrants. Information on this success speech aren't so. I'm just going to kind of draw inspiration from home. Wants to look they didn't submit any profile picture, and that's fine. I'll leave that alone but at least lets us see whole best. We can represent their data using inspiration from this control our components. Just I'm wondering to look at all of the templates, and I see here that this one is simple. It's designed just for text, and all we have is text. So we're just going to copy this on. Then we go back to our success speech, so I'm just going to paste this up of all of the PHP, spend some time doing over indentations, and then what we're going to do is just modify all of this. Remember that out of the box this court will work, but we want this This representation toe actually have the Dita coming in from or form. So this is what we have so far, we have the car title and a subtitle, and then we have some stuff. Some text are. So what I'm going to do is put the registrants full name here, where it's his car title. So I'll just do that change and we just do it side by side. So what I want to do is Sprint first name and last name in this section for this age five talk. All right, so remember that we have multiple ways we can do this. The simplest way to me is toe put in a PHP block. Alright, Some superimposing that in between the h five tug I want to echo on. I'm going to be echoing the value for first name on a space on the value for last name. And then remember that really and truly this space is not going to exist. So I'm just going toe, put them in quotation marks, use my interpolation. All right. And then what that is going to do is just create one long, contiguous block off text. Andi, with my semi colon, and I think there's something wrong here. Okay, so then this and we're getting a red line. And if I look back at my page, I see that the peach has broken for this error. So the D about the debugging is quite clear, and it's sending us where Which file has the arrow on dawn? Which line? And that's lying 11. And that's our line here. So if that won't work, then I'm going to instead removes the quotation marks on. I'm going to use some concatenation here, someone to conquer tinny. It's the tool variables, some echoing first name. And then I'm going to con Captain eight a blank space onto last name. All right, so then we get that one long printing. So sometimes you may have an idea of whole to write code like I just did on. Then it may not work coat, and that's fine. You can always you know, all the techniques. He can always refresh its and regroup and try it another way. So when I refresh this, then we see our values being printed as they are in here. So I don't like being called puppets. Understand? To rename changes, but to Mary Press enter and then we get Mary Poppins. Alright, so that's that's the first step. Next, I'm going to change the subtitle. Toby her provided specialty. So she says she's a database admin. So I'm going to change the text here for the subtitle in the same way. So no, we're seeing that we can just copy this, and I'm just going toe create a space. So we get two lines here so we can see what each six starts and stops on what is inside h six, which is our beach b block on. Then. I'm just going to change this to Specialty. Andi, that's all I need printed. So I'm just going to remove all of that on then when I refresh my page the Mary Poppins database at all right. And then I'm sure you get the idea. You can go ahead and change out. So we took inspiration from an existing controller on No, we're making it our own. Now we're making dynamic because the values here are relative to what is being submitted in the form. All right, so as many changes as you make, you can see them reflected here so I'm just going to go ahead and steam rule through the rest of the changes here. And then we just look at the final product art. So I made all the changes, all that it was. Take the P class. That's a car text, and I just made each one itself. And then what I'm going to do here is be explicit. So date of birth, colon for each one off them. I'm just going to make sure that the static text is there. So this is all we start merging static text with dynamic text because each time date of birth is going to get printed email address is going to get printed. But what is on the next side off the colon is always going to be relative toe what is submitted. All right, So when I do all of this on day, I'm going to take all these uncle tugs because I really don't need them, right? No, I don't need them in the profile, but off course, you know, make it your own. You customize its whole you wanted. And then when I refreshed my page, I see here that I have ah slight error and it is seen that I have an undefined index called Contact and determine the line number, and that's because the indexes phone on, not contact. So you made me those mistakes, and PHP will always be alert to them to keep you in line on when I refresh. That's what I end up with. So a nice little contact card to show Mary Poppins all of what she just submitted.
5. PHP Form - $_POST Action: Hey, guys. In our previous video, we went through modifying our form submission toe. Actually, Goto another page once of data was submitted in the form on DTI to display the data being submitted. So just as a quick recap where distance of quickly, Philo this form. And I'm just going to use values that were there before to expedite that process. And then when I click submit, we go to the next page where everything that we just filled out is there on display. Now we looked at using one and action and to the method get onda get method actually puts all off the values being submitted in the form right there in the Ural in Plea Insect. You don't want that if you're dealing with sensitive information, you don't want that. The earl will be a source for anybody who has malicious intentions to just come and get the data. It shouldn't be that simple. So for certain applications, yes, it's fine. But then when you're dealing with sensitive data, you want to use another method which is called post, and that is what we will be looking at today. No, get on post are very, very very similar. So the changes that we need to make toe or farm and tore action peach will be very, very minimal now. The first change that we would make is to change the method. So we did say, Get what we want to say this time is post Andi that is half off. The work completes right there. So just by changing the method from gets to post the form, behavior will be entirely different. And I'll show you. No, you notice that there is a difference. So I'm getting an error here, and it is seeing that I have undefined indexes on do other things. So it's looking for the values that should be there, and it's not finding it. Also, you notice that my girl does not spots over any values. So then you're probably wondering, OK, where are the values? So if we go back to our action page, which was success that PHP, we would have written quite a bit off course, we we took this from bootstrap Society Does get that card on. What we were doing was echoing get on each index off the get. So what happens is that because we're not passing over the values in the get method anymore . The Super Vera before get is not getting any values. That's that's pretty much what's happening here. So that is why we're getting these errors. That undefined index first name because they get Super Variable is looking for some value with the index first name. Same with the last name on every other one. So this no is a way to introduce the other super variable, which works with the post method on that one is post. It's that simple. So I'm going to leave this court and actually just going toe comment out this score, or at least most off this court, because after coming until the PHP manually. So I'm just going to commented thoughts. Andi, I'm going to leave a notice above it. So we all know what this code was for. All right, so I just throw this little guy to see that this spring Sol values our past toe the action be using method equals get so that we know that that's what this section off court was for . However, I'm going to duplicate this section of code. I'm just going to make some modifications like I said Get on post are very, very similar on the big difference is that instead off seeing get, I'm going to see dollar sign on the score. Post on that is half of the work on Peter's. I'm just going to leave it with first name on. Then I am going to go back now. Another thing is that because the values are not here in the early, it's kind of visiting the backgrounds. It's like it has Tofik retransmission, Ari submission. If you try toe, refresh the action, Peter. So when I continue notice, know that Okay, in the midst off the noise, you see the word mirror, and that's because the Post post super variable is actually seeing a value called first name. It's It works in the same way as against if you understand what the get works literally just changing the method from get to post on where you would have said get you see Post. That's the major difference. But then I want toast. I appreciate the fact that the behavior is also different because no, we don't have that query string. We don't have that question mark with all of the values like we saw with the gets. All right, so one where we post data, it happens in the background. So the sensitive data are potentially sensitive. Data is not being passed in the URL as it was with the get. However, as much as it is obscured from the user's eyes, we the developers know exactly what is happening because we know we're the ones pulling the strings. So I'm just going to go ahead and change all of these, get references into post, and then I'm going to refresh the page, which is going to ask me if I want to. You know, it's going to say the page I'm looking for had some information. Do I want to resubmit? Right. So that's what Crume is asking. Understands? It could continue on do that. Then we snap right back to normal. So all of the data that I just passed in the form is no. There it is there no more errors, but notice one. No data is being passed in the URL. However, we are able to access it in the back in nonetheless, someone to start from scratch. I'm going to just go back to the form and then went to submit a brand new record. So I'm going to make up a new person. Agnes Walters Onda. On a dead end of this exercise, we're going to fix this date picker, and then this person is a software developer. Andi. She's a Walter's at gmail dot com. All right, that's not the proper evening address on the number is one through night. All right, I know when we submits notes, nothing in the world, the early screen cleaners. Also, however, all of her data is there for display. So once again, that is because we're no using the post method. No, I did see. Just know that we're going to fix the date picker and let's do that together. No. So I'm going to go to the DJIA query You. I references for the Deep eaker on going to see what options are available. So I go to the website. I find it picker on. Then I'm going to see over here if they have any adjustments. Are customer custom ways to display the date picker that is more conducive to my user base ? All right. And I found the one it's display month on dyear menus So you click on the example and you see that it allows you to select the month, select the year and then select the date on. That's much better than the one we had previously, which is the default on where if I was born in 1950 I would have to click. That's not practical, right? So no want to modify it. So I'm going to look at the example cord to see what it is that makes that one difference. And then the major difference that I'm spotting here is in the function, So we know we call date picker. But what we did was call date picker and open and close varies. What they're doing is passing in some values here. So I'm just entity those values because that's all I really need, cause I have the rest of the court. So they want to copy all of this and replace what seniors are. You just copy the relevant parts and replace. I'm just stopping in the relevant part on. Then I'll go back to my court, where my date pickers defined which the function was being called in my footer file. Onda, I'm going to just modify it with the new values. Alright, Under Spend some time fixing my indentation. Alright. On DSO my date picker No has some parameters being passed in. Okay, so this is why you're court needs to look like if if you have the same concern that I So when I go back and I refresh my page and I reload my date picker No, it looks like a more user friendly. We alot my users to select their deeds. I can know comfortably Scroll on then you see, it's limiting which year So that's another adjustment that will probably want to meet so back to our court. I'm going to add one more attributes on this one Will set the year range on the attributes is literally here. Range on then Colon Sanders following the same pattern as the previous ones on. Then the value for this could be I think it's negative One on Dredd on call on plus zero. All right, I'm going to see that and see what it looks like. And when I reload on take a look at the years available to me in this drop. Don't I see? I have up to 2019 on as far back as 100 years. So let me just explain that when I set year inch and I said minus 100 colon plus zero, it means I can see upto 100 years ago, but nothing beyond this year. All right, so this will be a moving target off course, because next year 100 years ago would be 1920 as opposed to 1919 as it is. No, on also close ear would be 2020. So that is how you would go a boat sitting the range off yours that is available for selection from your calendar control. Ultimately, the goal of this tutorial video was toe help us to understand whole weekend submit data from a form to the other page using the post method. So we already have the get and you still have the court reference scored above on. No, we know how to do it with post on comparably, Each one has its own purpose. Each one has its advantages. I just use them wisely
6. Design Database with phpMyAdmin: Hey, guys. In all our previous videos, up to this moment, we have been looking at designing a form, submitting the data on being able to view it after submission. The next major step to take after doing all of this wonderful work is to actually store this data that tomorrow we can view it the day after and essentially we are creating a registration system for a 90 conference. So at the end of the day, we need to see a full list off everybody who has filled out this form. The next major requirement for this to be accomplished is a database. Our objective for this video is to start or database creation process. And we will be using PHP my admin to design a database in Maria db slash my SQL No to get started, we first need to launch PHP my admin. And to do that, we just open a new tub in or browser and then we can rose toe our local host address. But the application that we're going toward the folder is PHP my I mean art. So that's a little the letters Ph b m y a d M i n aren't So if you type that in and it doesn't work, then you probably want to make sure that your my ESC UAL service is running sick and just open zump and make sure that my SQL is up and running. Andi, It should work, all right? No, something's the note. I will say my SQL a lot because for the years that has been the default database engine that comes packaged in Xam, pour one for any off those server ph. B environment applications. More recently, there has been another version off my school called Maria DB. And so you see them. You used very interchangeably quite often. No. If you have successfully Bro's two Petri, my admin than your screen would look something like this. If it does prompt you for lugging, then you can use route with no password. That usually works. And if you still have difficulty, you can let me know and I will assist you. No PHP. My admin is a database management system designed well, really. For my ask your slash Maria DD it's completely were based on, and it would have been installed when you installed some. So this is a Web based portal that allows you to manipulate the database as you need to, without any additional overhead off, opening another application and using up too many resources. So it's really handy and quiet clean. It's easy to use on, so we will be using this to design our database that will be accepting data from this form . No, based on the nature of this form, I have scoped two tables in my design on It's Okay if you create wanted, we could create one table to store everything. So we just know that we're feeling wanted with all the information on day, I'll explain why I have two tables when I am designing the tables. So the tables that we're going tohave will be one attendee on to area off. Expertise aren't on. I'll explain why I'm making a table just for ear off expertise so we can go back to Peach Member at men on then to create a new database. We click on this new this new button over here, that link and then that will bring up a dialog box or change appeared rather and ask us for the name off the database so we'll just call this one attendance. So I always name my databases. Um, clearly enough that I know exactly what application it is relating to so I can see attendance underscore Bebe art, and then we're going to go ahead and click, create, and then it's going to then move on to allowing us to start creating tables. So the first table, um, went to create is a 10 B on the number of columns I want because I can do all of the granted. I can change that along the way, but I can just counter and say I want 123456 columns, seconds increases number 26 and then click Goal on. Then the next screen is asking me toe actually fill out what I want so well, actually should have gotten an extra column, and I'll see why. So the first column that I usually put in any database I'm designing is on auto incriminating primary key. So the 1st 1 would be an I d. I'm going to call it a 10 d underscore I d. On. I leave the data type of int Andi, I'm going to one say that it should be a eyeota increment Means is going to co owned by itself. So that means for every record that goes in this column, the value in this column will always change. And so that will always be unique. And so this will be my primary key, since every database properly designed it Abi's should be should comprise records that are uniquely identified on having our primary key that is unique is step number one. So the easiest way to make sure that you will always have a unique idea because based on the data we're collecting, there is nothing unique. The only maybe the email address has to be unique each time and really and truly doesn't because I'm not going to stop somebody from using the same email address twice. So there's nothing here that really says old. This person is definitely unique, so I'm going to create my own unique value. Call it attendee. I d make it auto incremental ing Andi, I'm also going to and then that promise the addition off on index, where they're asking if I want to make it a primary index. Others go on. What that did was to create the index called Primary so That's how we get the primary key. All right, on auto increments. Just by enabling auto increment, it's just going to say, Oh, is this supposed to be the primary key on By enabling that, then we know we have the primary key that is also implementing. So then I'll go ahead and create a column that corresponds with each data point that I intend to stores first name, last name, date of birth on DSO. I'm just going to feel those old in Petri. My admin. So our first name and then the data type for first name can't be int it. It has to be something that would be like a string. So we databases they're not. As for giving a speech, be with BHP. The data type didn't matter for the variable. I could put text number everything inside off any type of variable. But in the database you have to be very, very special because what kindof data will be stored inside that column. So the most popular ones that you see will use are at the top on. We have our char, which is one used for text a lot, and then we also have text, All right, so you can use either one. I'm going to stick to barter because Varta has been around for a while. And that's what everybody has always been using. And that's the more popular on. You'll always see another database engines. I'll just use var char onda. I'll set this length toe 50. So by setting Bartsch are, which is a text on the 50. I'm saying that I am allowing this column to store any block off text upto 50 characters big. All right, I think I can set defaults and all sorts of things, but I really don't need to sit anything else because I don't need a default. Value should be getting it from the form on the other. The other options don't really apply, so I'm not going to put those in. So I just move onto the next one, which is last name and last name. I'm sure you'll agree. Kit can get the same treatment as first team, and I also me that 50. You can increase this if you wish. Maybe 100 because sometimes you do have people coming with some very unique and long names . So what will happen is that if somebody feels old here, form on their name is more than the number. You sit here, it will truncate it so it will only store as many characters as it can. So sitting a bigger number is always a good idea in case you're anticipating that ah based value is not practical. All right, so our first name last name next is did off birth. All right, so notes that this does not necessarily have to line up with the label or the name or the idea that you give the control because mapping will be done in another way. But you always want to make sure that you you have defined some space to store some value on date off birth would not be a number. It's more off a date. So I'll use the date data type because I'll be storing at date inside off that column. I don't need to set a length for the value. So you know what is that? Sometimes subtle. And sometimes you don't. It doesn't need a length. They doesn't need a length. The other two columns would be email address Onda contact number. All right. On both of these would be var char Onda, said email address to 100 on the contact number. I'm going to sit as Varta are also not int because people may come and they use their dashes and their brackets when they're tapping in their numbers, because some people type their numbers like that and then species right? So you may end up with variations unless you're going to be very strict with your mosque, and even then you don't want to use a number. I my personal rule of thumb, is don't use a number data type if you don't intend to do month on the column. So we're not going to be doing any mathematics with the person's contact. Numbers are leave that as Varta on the typical number is up to 11 or 13 characters. Also said this the 15 so that anybody, from any reason whether use 11 digits or 13 digits are you are one extra one. There's no space for you. All right, on, we left out a column, which would be the era off expertise, so I'm just going to add a new column. So we ran out off columns because I asked for six But I used one off the six to do something else. So at the top here, I can just see odd one columns are just let go. And then that just gives me another column. So I'm going to say special tea. All right, Onda, I'm going to leave that as it on. Do you see why? So I'm going to say specialty. I d All right on then I can choose to preview the SQL because really, actually, this is just going to execute some SQL in the background on If you are not sure about SQL or the language itself, you can trick or my other tutorials on my SQL development data database development and mastery. And you can also check out my other set of videos on Microsoft SQL Server for everyone. So, without previewing my the SQL that would be used to execute this, I can district save which will actually involve the SQL in the background on give us our table and then this is our table design and structure. So we look and we see all off our or columns as they were defined. So you see, this little key indicates that this is the primary key, all right. And then they tell you that it's also incremental. So that means for every value that goes in on, uh, value will be generated uniquely in this column. No, I did say that we would have to tables, and I would explain why. So you noticed that I use specialty I d. Instead off specialty as a bar chart, even though we're asking for Well, we're showing words over here, but we're not going to be storing words on. The reason for that is I don't want to store words it to me. It is useless tohave database add mean 20 times in my database. Instead of doing that, I'm just going to create another table for just the specialties. Andi, I went to reference the that table whenever somebody selects something from this drop known . So in other words, if I had a new specialty to this list, then this drop down list should automatically pull that from that table. I shouldn't have to go into made into my html every time. And if you remember the money or presents off adding the options and then I would have to go on, I don't know the option on then that that can be tedious. So what I'm going to do is create a table that will have the sole purpose of storing the list of specialties because we only have four on DWI. No, I tease our broader, um carrier than just four specialty. So we this will give us greater flexibility in managing our lists on wherever we have a drop down list. It's good if that dropped on the streets. It's data from a table. So I'm goingto go to new create the table called specialties. All right, on, I need a specialty on the score I d. So I always start off with my i d column on. I'm going to make it also implementing, which will prompt me to make it a primary key. And I just go on interned. Everything else is good on, then. I'm just going to have another column. That's his name. So this this would be the idea, and this will be the name. My name would be the name of the specialty. So the text value that is associated with this idea will go in. This column aren't on. Then I'll just meet that one of our char on. I can do this 1 50 on, then I'll save. And so I have two tables. So I have specialty specialties and I have attendees so know that we have this new table on the block and we need to establish that specialty I d from the specialties table is somewhat related to an m Just going to click on the table here, attendee. Unethical. Suppose with African structure on, we want to establish that these two are related and literally the word related applies because in databases you have what you call relationships. So we need to establish that there is a relationship between the specialty i d in our attendee table on the specialty I d in our specialties table aren't so I'm going to go back to my attendee table Click on the structure on that I'm going to click relation view. So have table structure were relation view on Then this will know asked us to set up what we call foreign key constraints. No, In a nutshell, our primary key is a unique value in in one table meaning attendee, I d is the primary key in their 10 de table. However, we need to reference the primary key off the specialty table inside the attendee table, and that's what makes it a foreign key. So it's a primary key in the specialty stable. But over here in that 10 detail is going to be, ah, foreign key. Another thing with this constant is that if I have four specialties in the specialties table, I can't reference value in the attendee table. That doesn't take this done. That said so It's another level of protection to make sure that your data is going to be clean and it's going to be solid. So I'm just going to add this constraint name and number one to give it a name that tells me what it is for so f k short for foreign key on. It's a foreign key between attendee Onda Ah, specialties. All right, so that's my naming convention. You could name this puppies if you wanted to, but if you name the puppies and took a sabbatical and came back, I looked at your court. It wouldn't make much sense. So the point is, you want to give this a name that is actually symbolic off what it is for so my knees saying that this is a foreign key constraint between the attendee table on the specialty stable on. I can leave the restrict if you change this. The cascading means that if you delete the primary key value from the primary table that it would delete every related value. So I'm going to leave these for no on. I'm going to add the column from the attendee table, which is specialty I d on. Then I'm going to select the database that this is related to. So we're still in that tendon, Stevie on then the table inside off attendance. Db is specialties on, then the column is specialty idea, so it's always going to default. Whatever. The primary key is off the next team that you say there's a relationship with because it knows that clearly air trying to relate it to the primary key. So if you don't see it up here like this, that means you didn't set up your primary key properly. Now, once again, if you want more in depth understanding off databases, relationships, SQL all of those wonderful technologies, he can trick on my other videos. My SQL database development and mastery or Microsoft SQL Server for everyone. Now, having done all of these things, we can go ahead and click Save on, then this would actually have executed this quarry on set up that const range between the two tables. If you need to quickly modify your table and make something the primary keys because you may have missed it or you didn't quite understand it the first time, you can always take the column and click primary. All right, And then, if you want to make it off the increments, you can go ahead and click. Change on that will kind of bring it back to the design view where you could go ahead and write all of what you may have missed the first time. That is it. For over database designed for this small application, you will always have access to PHP, my admin and for every application that you build and you might need a database for, you can always go to local host slash PHP. My admin. Go ahead and create your new database on all of the tables that you require. If you need further information or have any difficulties following the steps in this video , feel free to drop me a line and I'll be sure to follow up with you
7. Connect to Database using PHP PDO: Hey, guys, In this video, we continue our task off, hooking up our registration form toe our database. You know, previous video we went into Petri my admin and created our database. With two tables on in this video, we will look into how we actually connect to this database from our web application. Now, to get that process started, we'll go back to our project in visual Studio Court on What we want to do is at a new file to this entire project on this fire will be responsible for handling the connection to the database. Soto get this party started. I'm going to click on the folder that we created from the beginning called DB. It's empty right now when we went to select it and create a new file in there. And I'm going to call this file, hon, that CEO and dot PHP And that's just a naming convention. The other tutorials or other sources may have it as db conficker or something else. It doesn't really matter what you call the file, but once again, you always wanted to be indicative off its purpose. Now, inside this file, I'm going to start off with my Ph B thugs on. We're going to see how we build what we call a connection string on. We'll be using what we call a PDO object. No, there are multiple ways to connect a data base in PHP. They're what you call Ap eyes that alot or facilitate the connection between your PHP code on the database on the one that we will be using is PDO Now. The reason I'm using PDO is that it offers some more protection than other libraries immunity So literally the letters PD or that's there video, aren't you? See other drivers like my SQL on you'll see my SQL I Those are less secure than PDO, and video is the one that people recommend you use to prevent things like SQL injection on to prevent against other security concerns. Also, by using the PDO method, you actually reduce the need to connect to the database every time you want to do something against the database, so connections can be expensive. Andi, the more your application is being used is the more connections will be there. So off course, you want to minimize how often somebody connects, even if they have toe a lot of the times. You don't want to multiply that. So video offers quite a few benefits to the traditional my SQL In my school, I, um ap eyes on. But it is what we will be using in this course. Noto start modifying or file. The first thing that I'm going to do is the clear on variable, and I'm going to call it host on. I'm going to give it a value. Now, the volume giving it is relative to the database server, meaning my database server is sitting on local horse locals and Petri. My admin is just a window into this database server. All right, but the database server is at local host, so I have two ways I can read toast. I can see the word loca host. Or you may also see people refer Toho, co host by I. P has 1 to $7.0.0 not one. All right. And I just put this I p address in the context if I attack this into my browser, All right, So remember what happens when I type local host into the browser. It automatically. Brose is to the sump landing Peach look what happens in a type that I p address into the browser. It also Brose is to this landing page. And that's because that I p address is what the word local host results toe. All right, so you can use them interchangeably or in any time you were a local hosting dinner at this I p and vice versa. So if you see the i p address in any other example, that's really what's this is just another way of seeing local host all right, someone to leave it with the i p address. So we would learn something new today on going to declare another very bacall db onda db that we're connecting toe B b is short for database. So the databases that were looking for and looking to connect toe he's the one that we created on understand, to double check and make sure that I have the name quickly, which is attendance on the score db someone to type attendance on this score. Devi Next we have a user. No, my my PC. My admin did not promise me to log in when I was loading it on. I did mention that if years prompted. You could use route with no password and you would get through. So by default, once you install my school or Maria DB here, you're given a default user called route. All right, so I'm just going to see route as the user on wherever there's a user, there's always a request for a password on. Now, just a pass is equal to Andi by default route does not have a password. If you were using having the my SQL Community Edition, then you would have been required to put a password on to the roots user at the time of installation. But once again, if you're using zump one mom for any of these ph B environments all in one installation packs, then you would have a user called Roots with no password on that will grant you access to your database. This is not the more secure way, but this is your machine, so it's fine off course in production. Then you want to take greater consideration with your user name and password on The last thing we're going to sit is Ah charset on. This is really just see what kind of symbols we expect to be working with. And I'm actually just typing this because this is a standard charge sets. They don't necessarily have to remember this. Just appreciate that it goes there. All right, so after declaring these variables, I'm going to declare another one that I'm going to call BSN. No, de Yasin is actually ah, terminology used in the PDO connectivity. So it's the way that it connects to the database is just another engine. All right, it's short for data source name. So if you ever heard off jdb c r o d b c are stuff like that, they they all ds in is related to that kind off connectivity are so we're going to do is actually build a decent connection using these values on. I'm going to type it out one by one and explain as I go along. So the first thing that we do is the clear variable TSN. It doesn't have to be called the assemble once again making variables useful in terms of their naming on. I'm going to use double quotation marks because I'm just going to take advantage of the interpolation one some typing in the text. So we have my SQL. So, firstly, we declare the driver This is the type of database that we intend to connect. Oh, no. PDO actually supports different databases, different drivers. You can use it for Oracle if you had a Microsoft SQL database working with. So this driver is saying that I am about to connick toe a my SQL database and we see colon and then we feed some some parameters in tow. This connector. So the first parameter is host, and it was the host equals on die can actually just use my variable. That I said, is host. All right, so I declared host here. So instead of typing, it's old inside of this string. I'm just going toe tap it here. So the advantage of this is that I can always change this without having to modify this entire string on Another advantage is that they were going to do this once. So it's not something that you do every time you want to connect with it. Every time you're about to build a site, then you have to do this. But for the purpose of this website, we only do this activity one time. And in this file All right, so I'm connecting to the horse that I just referenced. Variable. Next parameter is DB name on. We declare db off top on. We give it the name. All right, So I'm just making reference all of these things, and then the other thing is charged it on Once again, we call in the char set, So this would have constructed on O. D. B C like connector four or my SQL instance that's sitting on local host as a as the value here suggests on we want the database called attendance TV on the chance that that we're working with is this universal and standard one. In the next line, I'm going to do what we call a try, catch statement. So try Catches are used for what we call exception handling. So I see try And then I opened the curly breeze on. Then after the second appear to bring someone to see catch on. Then we opened up two parents sees, and then we have the curly braces again. So let me just walking through the track. It's what this block does. Is it attempts to do something that will be defined in between the open and close for the try on. Then if that feels, then it You can specify what you want to happen here, where they want to throw an exception. You want to stop execution or just want to show a nice, friendly error message that sometimes things happen and you would see a im im aero pop up unexpected error. A lot of the times this is what is happening. They have tried. They tried operation that you requested. You didn't work. So they say unexpected error. So what we want to do is to try to connect to the database someone to declare on objects or another variable on. I'm calling a p d o on in PDO and win today find or declare a new instance off a PD or class. So this class is built into PHP. So we're looking at some object oriented stuff on the inside the parent This is for the PDO function or class. Sorry. We'll be passing in the values D S n So we're passing in Whatever value is inside of that variable d s n. Then we'll be passing in the user that was defined So we didn't use user initially on, then were passed in the password on Dwell. There are other apartments is that could go in there, but right know that that's all we need. So we just need the DS in the user on the past. Now you notice that there is, Ah, little Red Line under the capture nets because we need to put what kindof exception we intend to cats so you can actually go with those apparently sees in a lot of languages, and it's just a generic catch. But in this one, you need the parent disease, and you need to specify what kind off error you're looking for. So in this situation, since we're trying to connect to the PDO, they gave us an option for PDO exception. So we're watching to see if there's going to be an arrow with the PDO object. Andi, I am going to assign its toe a variable called E. All right, so once there's a PDO arrow during this attempt, then it will catch the arrow on, then store all the details off the era inside off some variable called E, and then in the next line you can decide what you want to do it. So inside of this sketch block, I'm actually just going to say something like throw new on once used the word through, it means is going to stop all execution and just display an error. All right, So you may not want to do that all the time on your try catches, but in this situation, we actually want an error to see if our connection is working or not. So I want to throw new, and then I am going to see a PDO exception. Andi, I'm going to put in some cord that will get me the message. So this e here presents an object, and this object has quite a few functions in it. So I have a phone sign that says, get message on this get message function is going to actually print what the era is seeing . So whatever the area is, sometimes you want actually read the arrow so that you can Deeble property. Once you catch the exception. You can always get the error here, so I could just echo this without throwing on a new exception. But once again, I actually want to stop the execution for something as critical as the database connectivity, but you can do it more elegantly by just maybe echoing that old. There's an error with this, but the page would still load, So that's up to you. So I will leave with this cord on what's once again. What's supposed to happen is that it's supposed to attempt a connection. Toe A. My SQL server phoned a dislocation with this database on supporting that. Charset on it supposed to try that connection using a user name and a password as provided here. So if anything is wrong, well, if nothing is wrong, then this will, Lord are this will be okay. And if something is wrong, then all execution will stop and you see an error message someone to make it a bit more clear when nothing is wrong. Island See echo. So if it tries the connection and nothing goes wrong, I'm going to see Echo Hello theater basis. All right, so if we see these words appearing somewhere on our peace, then we know that a connectivity or the connection attempt was successful. So now that we've done up this con file and it is in this director under DB what we want to do is actually include it in our page, someone to go back to my form on Dougherty Road. The line. So what I did was see require once on dime, requiring the file db slash conduct PHP. So not because it's not in the includes full. That means I can't include it. I have to include it. Everything that is not native on the page that I want on the page. I'm going to have to use either require or the include to get it where it is the is, though it's only a matter off finding where it is. So in this situation is in the folder db on the finally Mishcon. I just see require once when the db folder and get me that file. All right, So when I refresh my form, the expectation is that I'm going to see Hello. Database somewhere printed, and I refreshed on. There we go. Hello? Databases being echoed to our peach. All right, so that's great. So that's some progress. No. What if I got some off my details wrong? Like like maybe I missed upon the i P address. I did once to 7.1 dot 0.1. There's no address anywhere near here that goes by this. So if I attempted this connection with the wrong data, what would happen is, well, what should happen is that Hilo database would disappear. So apparently 1 to $7 wonders series valid. So I'm just going to take with one. Let's try that experiment So I botched some detail in this case. Well, my attempt a budget feel the first time, but let's I know for sure this won't work. So let's go back and see what happens on when I refreshed. Then we see that we caught this error on what we're seeing. Being printed here is the result off us calling the gets message function inside off the object aren't so. That's what happens when you are at your core. That way on, you could insert once again instead off throwing the exception. I could have let me just comment this oats Onda let me echo on elegant message on. I'm actually going to say something like, Well, I went to print in each one on I'm going to see no database phoned. All right, close my H one tug and I wanted to stand out and show that it's dangerous. Understand, added the CSS class from bootstrap class equals art. And then here is one of the dangers off. Using single quotation on double quotation marks, I'm going to use double quotation marks on the outside instead on single on the inside. Someone just made that change quickly on I went to see a text dash Danger. So let me just refresh and show you that the paid still loaded everything still executed. But the message is being printed, as expected because no data business phoned. So that's what I'm saying. There are multiple ways, the 100 year errors. He can do it where it's doomsday. If something doesn't work for associate, give a nice message that's user friendly, and it doesn't cause too much disruption to the flow of your sides. Off course. You give that based on how important your task is, all right, So I'm going to put but my exception throwing right there, because if I don't cut into the database there is no form, there's no nothing else will happen. Are should happen. So that's it for facilitating a connection to the database using the PDO object in the next video, we'll look at Hall. We actually store the values in the database
8. Save Records to Database: Hey, guys, In this video, we will start wiring up or registration form with our database objective off this video Is that we when we submit our form the data that is displayed on the success that PHP page will also get submitted so over, attendee table in over days of this. Now this process starts with modification off our course. We go back to visual studio code on. Then what we want to do is create a new file in our DB folder some winter creating you file click on the DB folder, create new fire on them. Going to call this one carod dot PHP No crowd is an acronym for Create, read, update and delete. On the purpose of this file is the store all of the function or functions are all of the court associate ID with those operations. This will also represents our first step into using an object oriented approach toe over THB development. So the first thing that we want to do as usual in any PHP file is create or PHP block on. Then inside this block, we're going to define a class on the class name. I usually meet my class name reflective off the file names. That way I know exactly what class is represented by a what file? Someone to call this class fraud. And if you're not very familiar with what I'm doing here with class and objects and so on, it's fine. You can just stick with me and I will explain it along the way. But this is object oriented approach to programming. No, I'm going to define private attributes and I would supply this one db on. Then I went to define a function. But this is a special function that every class has our chan have, and it's called a constructor. Someone to read the word from John on. We would have looked at how we defined functions before and impeach Be all have to see is the word function on Give it that name. So it's a form son, and then this is how we define a constructor. So it is two underscores and in the word construct, So that's underscore Underscore con stroked on door construct. Er is going to take a parameter on that. Would you call this parameter con? So essentially, when you're initializing an object off a class, you can actually upped to do it without any objects without any parameters Or if you require a permit that this is where you would have defined that a parameter is required or some value is required any time you're initializing an object off that class, that so I'm going to take a parameter called upon on. Then I'm with a sign water value came in to this con apartment off to or private variable called DB. So and then I want to say this, which is a key word in most programming languages, which just means in this class in this class file that I'm in. So this is just a short way of saying I'm home. All right, so this gives me access to all the private and public attributes off this class file. So in this case, I want this db to get the value that con us. All right. So in other words, wherever I'm going to use Devi throat discuss, it should embody whatever value came in upon. All right. So I just went ahead and added some comments so that we have a bit more context as to what is happening with each line off court. Now the cool thing about the classes that you can put as many functions and variables are inside of that class. And wherever you need to reference it, all you need is a instance off the class on you just make reference to the function that is there. So we are going to be using this class toe house all off the functions associated with creating a record in Ardis Abi's retrieving airport for more data bees or beating a record in our data Bees on did the meeting at report in our database. So for this video, we will focus on fleshing Oh, it's the insert function. So I'm going to say function. But then I have to qualify this one as public. So I did say Private here on die also have option off public. So what this means is that if I declare an object off class crowd, there is no way for me to interact off BB because devious, private to the class. So the only way I can interact with DB is from another function inside off the class. However, when I do see public, then it allows me toe interact with it outside off this. So I'm which is a public function on. I want to simply call this insert, and I just read that function body on, then insert No. Is supposed to get some parameters so that it knows what it should be inserting. So I'm going to define parameters here inside this insert function that will be able to accept all off the values that are being passed in the post action. So let me, let me say that the different way, remember from or form? We're collecting a bunch off, Beat up one's name, first name, last name, date of birth, etcetera. So all of these data points are going to get submitted. When they get submitted, they go into the success dot PHP page, which is our action on. We can access them all using the post variable. What we can't do, however, is actually put them in the database, at least not yet. So I'm going to define a function inside of my crowd file that is going to be waiting and willing to accept values that would be coming from the post function article starry. So in other words, I'm goingto create a perimeter. Her data points Someone tohave 14 1st name and I'm going to kind of reacted shortened, so they don't have toe necessarily reflect the exact name someone to shorten it. But at least I'll know what f name means an L name on. I'm just declaring some variables that will actually holds all off the values coming back and specialty. Okay, So they so once again, when I call this function to inserts, the expectation is that I'm going to be passing a value per parameter aan den operation that I carry out inside of the function will use those values to complete the insert. No, unto the fun part inside of my function, I'm going to start off with a try and catch, so I can actually just write the word try and impressed up twice. And in visual studio just completes the entire try. Catch for me. Let me do that again at this. Wrote the word try first top twice if it select. If you have both speakers, make sure the 2nd 1 is one that is in focus on breast up twice and then you will get your track judge block automatically. If that doesn't work for you to understand this. Or try open brace close brace, Catch open parentheses on open, open and close braces. No. Once again, the track catch block sees that I'm going to try and do something here. But I'm watching to see if I get an error so that I can take a particular action with this air and this logical exception handling. So and I just added a community here. So what I'm going to do is I'm going to go ahead and create a variable called SQL on this variable is just going to play host to my SQL insert statements. I'm going to give you guys a little trick. If you're not so comfortable with SQL statements, then you can actually go over to your database in PHP. My admin Click on your table on then Goto sq Well on then click insert And it kind of gives you a little template as to what an insert statement needs to look like. So, following this guy, we know how we need to type out our statements. On the whole, we're going to put them into PHP. But once again, if it is that you want to develop your esque you'll and database development skills. Then you need to check out my course, which is my SQL database development. Andi mastery. So, back in our cold, where did you spend type so together? Someone to say Insert into So those air SQL keywords on Ben. I am going to specify the table name, which in this case is a 10 B because I'm about to insert a report into the attendee table. Andi, I have to specify which columns now They're two schools of thought are two ways to do this . You can specify all the problems. You don't necessarily have to, because if you put the column values sorry, if you put the values in the exact order that they would have appeared. If you said value, then you don't necessarily have to do this part of it. Someone to skip that part of it. And I'm just into right. It's the more you know, the Foster Lee. So the first column would be first name, last name, date of birth, email address plant that number on Dwell. Remember, we had specialty idea and not just a specialty, so we'll get to that. But that's the order you don't need to put in anything for attendee i d because it's an auto incriminating primary key. So after the would be list, off columns comes the word values. So we would have had attendee and then listing out all the columns and then we say values. But since we're not listing the columns, I just removed that. So if you're listing the columns, you have the parent disease. If they're not, they don't need them on, Then we'll save values. But here is the crazy part. So with a regular sq, a statement, you would have stipulated of value that corresponds with each one of these. So if I just feel this right now, I would have put something like the static value. Mary Andi Poppins, etcetera, Right? I'm not going to fill out all of this, but that's what I would have assumed in so everybody can see. All right, so that is what I would have been putting in if I was writing this statement manual. And if you were using that my s killer in my school I date Ah, ap I Then that's also whole. You would ever over the dangers off doing directly like that in PHP it opens you up to watch because SQL injections which are very, very nasty kind of attack that you really don't want to entertain. Okay, So as as a guard against that PDO requires that were active particulate away on, then it will execute it. So the particulary way that we're going to write it will have us actually using like placeholders. So instead of seeing the value, understand To put colon on a placeholder toe correspond with what should be there. Alright, someone to say colon f name fallen l name on. I understand you reuse all of those variable names from the parameters So really and truly , once again, these are place holders, they don't have any values right now. So at the end of this exercise, you should have these please holders. Once again I give them similar names to each variable that I know they will correspond with You didn't have toe boat for consistency. I I recommend that you do so we would have created some very, very abstract form often sqs demons. The next step is toe Prepare this SQL statement Someone to be clear on mother variable on. I'm going to call this one s d MT that short for statements. So that's just my naming convention on. Then I'm going to see this. And then this is so we actually access or private variable devious. Remember that TV represents the PDO connection or the connection on we would have already defined or connection from inside off con on this is our PDO objects, which represents connecting to the database. So here it within the class crowed This is an obstruction off this connection. So PDO exists here. But what we intend to do is pass it into the crowd class and it will get stored as DB. So the only way to access DB is to see this. So it's not the same this on then We use that operators. So that's a dash and greeted aunts and our listeners. And with a narrow on this arrow is basically hole We reference reference attributes or properties or functions within from a class aren't are from an object. So this represents the object off class screwed on. Then we're accessing the property, our attributes dd so we say db on then inside off db, we have another function someone to use that arrow again on this function is called Prepare on, then prepare takes a parameter which would be the SQL statement. So we could have just done this. We would have said this R o D B r o prepare and then just write that skill statement inside off the parentheses. But I like segmenting its like this or that. Eat step off the way it is clear what is happening. So we gonna defined rs your statement first on. Then we went ahead and prepared it for execution later. The next step is to actually bind over placeholders to the actual values to remember that these with the Poland's here inside off our SQL statement, I really place all us what the rial values are expected to come in through these parameters . So we need to have a section where we see STM tea on an arrow on that we call a film song called Bind Param. And then this function takes two parameters in the experimental number one which is our placeholder. So F name is the first place holder on. Then we have a comma on. Then we see What do you want? Oh, buying this place holder to on essentially what this would do. We see that within this statement that is being prepared, I want that these placeholders gets replaced with these values Now it may seem tedious it may seem Monday, and especially if you're used to just writing SQL statement any insert into. But once again, this is an added level of protection against the SQL injection attack. So you are going to do that for each placeholder and understand to duplicate these lines and understand to change what I need to change parents, they can to stop in peace than kind of expedite that process on. Here's one of the advantages with using the placeholder, making the peaceful that looked just that available name. The changes can occur more quickly, So I'm just going to go ahead and change those votes. All right, so I'm finished binding all off my placeholders and everything looks good to go on the final thing to do or sorry, I I made a mistake with my object name. I had the cord written somewhere else and I copied in pieces, so I've gotten that corrected. All right, so I know that we've boned all off the placeholders with our actual values. The next thing that we want to do is actually execute. So we cst empty Onda we on. Then I use my arrow on. I'm going to call a function called execute on this one deep snow parameters. And then after we write execute, I'm going to write return True. So at least if he gets this fire and it returns true, then we can take this as a success indicator that our statement was executed successfully. Now, on the negative side off the fence, we want to actually handle any era that might come up. And at this stage, we really want to see any error that comes up. So I'm going to replace this terrible th with PDO exception. So remember that we're tearing oughta PDO operation here. So the more, more slightly exception to occur is with the PDO operation on, I'm going to assign any error object that comes back to a variable called E on then well, I'm going to do down here is echo to the screen. So I'm not one to throw. Those throwing would stop the execution altogether, but I went to echo e r o get to message so we would have used E ira. Get message before Andi. I'm sure you're wondering what that iro meant. So I hope that this exercise has brought more context to what this area is because he's really just an object off a whole class on. We just established that within a class, those with access what is in the class is to use the arrow operator on. Then we can actually interact with the other functions. So est GMT, here is an object off off db Prepare on then the SD empty object has a function bind Param on the function execute. So in the same way e dollar sign e represents the class or an object off the class video exception on one of the functions of illiteracy is get message on then if on a released carts on so not throwing but printed to the screen and we want to return falls. So then then this is just an indicator to say it was not successful. Now that we have me those changes to our crowd page or crowd file. No, we have a few changes that we want to make toe our connection file The first thing I want to do is at what we call attributes toe the PDO object. So I know it's connecting to the database, and it's often and dandy. But when we were doing this exercise, we did not set attributes on those actually says I really want are, like a remote. So in other words, PDO will always try to behave itself and be not unintrusive. But then you can force it to behave a certain way by setting the attributes on a certain way I wanted to be here is that if there's an exception, I actually wanted toe stop all execution and show me that there's an exception. So what I'm going to do here is reference a PDO object on. I'm going to call the function inside this object called the set attributes on. Then I'm just going to type these oh, quickly off screen, and then we just review them. So these are the two attributes that I want to set on their both as it relates the era. So I'm sitting this attributes on the era more is exception. All right, so essentially this attribute is you're saying that whenever there's an air I want to see that there's an air. Don't behave yourself. Tell me that there's an error because the last thing you want is doing the bullying. You think it's all well and fine, and then you push the production and then nothing works. So we want to see when there's an error. So that's what this line is seeing. That's change Number one. The next change that we want to me is to actually reference our crowd file from right here in cod in Khan. Andi, I'm going to explain why we're going to do it this way. Remember that in our pages we are a frenzy are con not PHP. Instead of having to reference conduct Ph. B and then reference crowed. Why I'm going to do is after setting up a successful a successful connection, of course, because the only way will get this fires. If no exception is thrown, I want to include or what I can require on I'm only going to require once. Andi, I want fraud dot PHP notice. I don't have to put db slash Croat because this is the con file that is requiring the file . So if I look at the four the level They're both on the same level in the DB folder. So I don't have to say db slash crowd. So if you're a db slash card, I didn't work then trying to remove the d v slash and it should work some referencing crude that PHP on. Then I'm going to define ah whole new object on. I'm calling it crude on to define an object I'm going to see this is the name off the object. And I wanted to be equal to a new instance off my class. And remember that the class the finally mr dot PHP the class they missed red dot PHP on the object name is crawdad ph with screw. So everything is cross everything looks uniforms. So the new instance off crowed on, then crowd requires on here. Visual studio code is telling me that there is something that needs to go inside off the parameters. And that's because when we defined our constructor, we defined it to accept some value. All right, so the value that is expecting institution in our context because we're the ones building its we know that the values expecting is some connector. Someone to pass the PDO object in tow, my parameter. So PDO so know what will happen is that for each time that we actually include or require the con file, the connection will be established on this will be done once. And then it will just reference or profile so that any operation you want to carry out, we need only reference the crowd object on whatever function. So at this point, we only have one function so anywhere we actually have fun referenced on any page. We can actually just go ahead on reference Dollar sign crude on then using our operator, we can look at all the other phone sons in there. So armed with this new knowledge, what we need to do is actually start setting up our success speech to be able to call our crowd function. So step number one is require and I'm just into require once on, we're going to put on the db slash con dot PHP. So when this speech Lords, it's going toe require this file which is going to go ahead and initiate a connection on, then it's no going to require or crowd file on defined a new class on, then anything we need to do on the page, we can receive object crowd on, call the function. So this way we're segmenting the court and we're keeping everything. One place we're not putting too much PHP are too much, too much database, too many database operations, mixing up with our HTML all the time. And it's a nice, cleaner way to have your court. Now that I have completed that activity, I'm going to start writing some more. Ph. B Chord on. I'm just going to use the block that's there already. So what I want to do is check if this if this page is being loaded on a post action on now context, realize that, remember that we can always go to our application and just see locals that tenants slash success that PHP all right on because it's opposed. We don't have any off those parameters. We don't have a question, but and when I do that, I'm going to be getting a bunch of errors about undefined variables on that is really because it it is trying to print values from the post action. If there was no post option meaning, I'm just browsing to the page casually. I didn't actually submit a for him to get there then these variables don't exist. So before we attempt to put anything in the database, we probably want us check. If such a variable exists, and if at least one out off all of the's exists that we can assume that the that arrest off them exist. Alright? No. When we were designing our form, I had put a name on the submit button and this is why so a lot of the times you see that people put a name on the submit button on what they'll do is check if the submit variable exists, because this would have come over in the post variable along with first name, last name, etcetera, etcetera. Even though it has no value, it really it exists. It's like an empty variable on the post action. So what we want to do is start off by checking if that submit value exists. So I can say if a NASA is set. So that's a function to say doesn't exist on inside the set. We passed in a variable, so this is a nice way to check if a variable exists on a page, someone to check if is set square bracket on submit. So once again, this is not checking for our values. Just checking. Does this variable exists? Whether it's empty or not is irrelevant. Does it exist on the page on? But if it does, then we want to carry out an option. The red lanterns there because I missed this cynical and cerebral that all right, so we say if it's set post summit, so if the Post submit variable exists, then we want to proceed with our operation. So I'm going to define another variable, and I'm going to call it. Success equals on. The reason I'm doing this is I'm about to call the crowd function insert. And remember that insert is a value returning function causes either returning through our falls. So that means Well, I probably can call this is success instead of success. So this is asking a question. Is it successful? Drawer falls? Is it a success on? Then I'm going to assign to this variable whatever is returned from the phone shown. No, because we defined are we required baby slash corn dot PHP on Remember that it initialize is some object for us called crude. I have instant access to this object called fraud on then, inside off the crude object I want my insert phone son on this insert function is defined the except in this order these values first name the last name date of birth, email, address, contact and specialty. So I'm actually going to go ahead on and fill in these values using the post values. So one pattern would be the Adidas past the post value directly into the function. I'm not particularly fund off that I prefer to define my variables. So I'm going toe have f name. So within the context, off success that Ph. B F game didn't exist, so I can reuse it here. So F name is equal toe post first name. And I'm going to do that for each off the values in the post. On going to do that, all screens, you can go ahead and do that all rights of added all of the's values from the post. Hurry on some comments so that we know exactly what's happening. Adede. Step on, then what I'm going to do is call the insert function on pass in the required parameter. So it's F name l named. You'll be email in that exact order. So I'm going to see the Assign F name as the first parameter. The sign l name Andi. I'm just going to do all of them. Just make sure that they all line up. All right? So once that is done, let me just recap. What What happened? So when this page is loaded, it will check if the Post, if one off the post variables exist on. We're working with the assumption that if one exists and the others exist, So if one exists, then we want to extract other values from each one on. This is done because maybe you get in the valuable. You want to manipulate it before you put it into the database. So that's why this is always a good idea before actually calling the insert function on. Then at the end of us that we check if it was a success. Well, we passed those values into our insert function on Then the result of the insert function will get stored in this variable is success. So then this comes in handy because if we want to print a specific message if it was successful versus appeals. A failure than this gives us the ability so I can see if its success meaning if this was true, then do something else. Do something else. So right now what we're doing is printing a nice alert on ice success message when you have been successfully registered up until no, there wasn't any real operation to me to make it a failure. So what I'm going to do is did this h one tug on actually echo this each one tug if it was a success. So if is success, then we will see that success message being printed to the screen else. We're going to echo the opposite someone to reuse that cold. But I'm going to change this text danger which will give us read text on then that we will see there was an arrow in processing. There was on arrow. All right, so once again, we're checking to see if we got true. Are falls returned from this insert function. And if it is true, then we want to give them a success message. And if it was falls, then we want to show them that. Hey, there was an arrow processing your registration. No, we have one additional and very major change to make. And that is as it released our specialty. No, up until this point, specialty has been coming across us. Text. We have a select control here, A drop down list giving us each specialty on when we submitted it on the success speech. When we printed the value, we got a text. You got somebody off text now? The problem with that is we designed our database to accept a specialty I d. So we have a specialty stable, which is empty right now, so we can always feel this with data on, but the attendee table is taking the i d relative. So the specialties in this table. So first they need to insert, at least Well, I'm going to insert one for this video, so I'm going to insert the database, add mean specialty. So I go. So I click on my table specialties. And then I clicked on insert on. Then I'm going toe, not give it on. I devalue because I can't. But I'm going to give you the name, database, admin and then go. This will show me that it's about to execute an insert command on the bottom of page. I can click go again on then that inserted. So when I look in that database by just clicking on the table, it will always show me what's there on. I see that they did a double entry. Okay, I can remove one. That's fine. But no, I see that I have at least one record here on day. I have database admin on the I d hear history, which means because we set off that constant on the table, I can put in an I d value here that doesn't exist in the table. So whatever I'm about to insert into my database needs too much with the Wisconsin train. So I went to force this. So we're going to me this more than I am because you go along. But for no, I'm just going to force the value associated with this option to be three. So we'll just make sure that Mary Poppins, for example, see selects three on. She's a database admin on. When we submit what the expectation is that three will be printed here on the two or three will be passed over in the post on. Then three will be the value that is received here in specialty Onda ultimately put into the database. All right, so I'm going to try and submit the form and let's see if we get any URLs and try and whether through them, so I submit. I'm seeing that I'm getting a few Arabs here on the 1st 1 is saying that there's an undefined index date off birth and they give me the line number in the file. And when I look back, they are right because it's not get off birth. It's actually deal be. So that's my mistake. So just meet that correction. The next arrow is seeing that I have on on defined method call to get Massad. So that's a spelling error on my part. So on crowed that PHP on line 30 weekend go ahead on me, that correction So that's get message on. Then we can try and resubmit Andi, I expect to see ares again because all right, there we go. There was an heroin processing. So what this did was it attempted the operation I specified and it returned falls on then because he returned falls. We're printing all that bread text seen. There was an arrow processing. There was an error in processing. But then we're also echoing the message because we did see gets message to be should be echoed on. What we're echoing is saying that the insert value this does not much something, something something. And that's because the data, the word database Adminis still coming across instead off the i. D. All right, so let's try that again, and the era was because I did not refresh my page. I just resubmit to the form. So let's try and fill up the form once again on then we can proceed to submit, All right, so I'm still getting the error. And if we read the RNC and that the value list does not much the column list. So that that is because, well, it seemed that there's some cola mismatching with the values that we provided. So what I'm going to do is just modify my insert into and be very, very explicit on. I'm going to see a long first name. Last name dates off birth. So these are the column names as they appear in the database design. So just you can always just remind yourself as to what the columns are by clicking on the table itself and then going to structure. And you see, first name, last name, date of birth, email, address, contact numbers. We have to be very, very, very sure that you're spilling them. Right, So that's date of birth. Email address. Contact number on specialty. Underscore I d. So we can try this one more time, so I'm going to go back to my form. I went to refurbish Once again, Philip totes on Submit and I have success. All right, on then you notice that there's a three coming back here in this in this printout, as opposed to the word database, I mean, so that that's fine as we go along with refine it. But the important thing is that after all of these changes and activities, we have finally registered somebody successfully not validate that the person has been registered successfully. We can always go back to PHP my admin click on our attendee table on. We will see over record their Mary Poppins on you see that the date off birth came across zeros on. That's because of a former thing issue. But we will start that. Oates. But right? No, we have Victor because we have successfully submitted or 1st 2 records in the database corn egrets.
9. View Database Records with PHP PDO: Hey, guys, In this video, we will continue our path towards creating a full crowd application in PHP. And today we will be looking at how we can go about seeing records that have been submitted to the database. By way off recap, we would have created a database called attendance DB On inside off attenders db we have a table called attendee, which is where our records for each attendee will be stored, meaning each person who registers on this page should get stored in this database once they click. Submit now to accomplish this, what I'm going to do is add enough bar to the top off this page, which will allow me to navigate to another page to view all the records, someone to go to bootstrap dot com and see what they have in store for me for nuff bars. So I go to get bootstrap dot com on die go to components in their documentation on knave bar. And here I see some sample code for enough bar. And if I scroll by, I can see the different options available to me. Someone to go with this snuff buyer ally. The way it looks. It simply enough on the court. Looks simple enough to understand. Understand, to copy it on because this is enough. By that I want to show up on every single page in this application. I'm going to add it to the header file in my project. So remember that header should be above everything and whatever goes in the head of means that that it's at the top off the beaches, at the top, off any each in a document I gets rendered. So if I add enough by here, the expectation is that every page that includes Hitter will have the nut bar being displayed. So I just fixed mine up my indentation and then we're just going to go back to the documentation underside on dissected of it. So you see here that they're opening a tug called Nav, and we have a few classes, a few classes that kind of give it some character RBG dash lights so that music can change what color scheme we want. We can look at that later on. Then we see that they have enough bar dash brand, which is a word and have bar so we can change that someone to go back and change that in my court from Nav bar Toe I t conference. All right. On DSO Usually this is the name of your sites, right? So similarly, they would have used the same court in their sights on But instead of using a word, they use their local. So whatever website you're building, you can put the logo in this section or just the word off the name off the website. Now, this section with the button, what it does is create those nav bar pills. So if I reduced this in size, toe the size off, maybe a mobile device and you see this happening here on, that's what we usually see on a mobile phone. So by using bootstrap, we have already got gotten what you call a responsive website on DSO that called that bit of court. Therefore, the button it actually is responsible for making the enough bar pills. That's what they're called on. Then moving on this Dave, which is collapsible C c. That he says collapse. So if you take all these classes, I'm sure I can guarantee you that if you modify these class differences as they're here in the example, they're not by will not behave the way bootstrap would have intended it. Toe. So advise you if you're going to make changes to these classes. Be very careful and you should be very, very deliberate. So this section hoses all off the links on you see that? They're just regular Uncle Tax. So there's nothing release fancy about them. So we need to do is change the name that is being displayed. The name off the link Onda also the trip. So we're going to do that quickly with over website. So right now we will have Well, we have one page but by the end of this video, we should have at least two someone to leave to here on day. I leave this as home because form is aching to index So I leave that they're on the air. Traf will be index dot PHP So any time somebody clicks the home link in the nav bar, it should goto index dot PHP on. In addition to that, I'm going to use the same link inside off this uncle talk for the for the logo or the branding section. I had enough by brand. So you know it is that this is also an ankle tag. So if we go back to Bootstrap, their logo is actually clickable. If I click, this little weight goes back to their homepage. So that is the kind of behavior I want. Similarly, there's ah, home one. So my enough bar will have a conference which, if clicked, will reload index on also ah, link that says home, which, when clicked, will reload the index page on then my next page. I'm going to make it save you attendees. So the expectation is that when I click this snuff by link, I'm going to see all off the persons who will be attending my conference on the Since I'm here, I'll just see that the HF should be view records dot PHP. So that means I need to create a page for this uncle talked to Navigator. So I'm going to go back to my explorer Pain. Andi, I have Index, I have success. PHP I'm just going to add a new file and I want to call this one view records dot PHP I know we have ah, new blank file and then off course, the first thing that we want to do, and I'm just going to go back to index on kind off, bar the cord from here. I wanted to look uniforms understand to include the header on the connection on I'm also going to include My Footer as well as some BR tags to put some distance between the content on the footer. All right, so once we have done those changes, we can go ahead and refresh your page on. We see that now we have enough bar on because it's using the light team. I don't know if it's very visible for you, so we can change that. Or the fact is, we have the Nuff buyer showing. And then if we click this, it really is the home page of foot to calm. It's the same thing. If I click view attendees, then we navigate away on you. See that enough buyer is still in place. So looking at these examples, I can see that I have not Barn of Bar Dash Dark and BG. That's primary someone to go back to my enough barcoding hitter on. I'm going to make a few modifications on which is enough bar dash darks on B g dash primary on. That's because I want my enough bar to be blue. So when I refreshed my page, I get my nav bar being blue on my links clearly visible, so know that we have created our new page. We need to change the title so that the carts title will appear in our title bar in our browser. But the next big step is to modify our or crowd files so that we can start interacting with the data that we we intend to for the speech. No, for this speech, we are going to be working on the air out of crowed. Remember, crowd is create, read, update, delete when you're viewing fuzzier, reading or retrieving the files so I can go to the crowd on I'm going toe the crowd final story and then we're going to They find a brand new function. So we already have a function that is dealing with inserting data. I'm going to create another function, someone to follow the same pattern public function on. Then we will say, get attendees. So your function, your function name should always be kind off indicative off what is happening. So I'm actually going toe qualify. This one is really insert attendees so that nobody would ever wonder what I'm inserting when they see this functions. If I change the function name here that I need to make sure that all references to this function Archie is also start as quickly hop over to my success page, which is where I made reference to it and change it from inserts the US insert attendees are so backed or crowd page, we are defining a phone son that says, Get attendees on. Then, inside this function, I am going to execute a quarry that is responsible for bringing back the attendees. Now, if you want some help knowing how that would look, if you go back to PHP my admin and click on their 10 de table, then you actually see that when they go to Breaux's, they actually give you what the select statement looks like to get back everything inside off your database. Onda. We can actually click on this create peach V chord over to the right hand side here, and they actually give you that inside of a very was understand the company, so this isn't really necessary. It's just a guy, but it doesn't help. It doesn't hurt to have some help. The next thing we want to do is create a variable, and I'm going to call it result on the value off result is going to be this. Remember, this is our object for our con or crude that PHP file on the arrow operator toe, Get the private. The private variable that was declared here that is going to embody our connection to the database. So we're getting this d b r o operator on. Then we're going to execute the quarry. So it does say this DB query on dime went to pass in the actual clarify our query, that joke worry which is in this variable called sq. Well, this this function is going to just return whatever comes back in results. Okay, Looking good. So far, the next step is to start wiring up our view record speech to actually show these records. Now, the most common way to show records is in a table. Thb and tables. They have a love affair. So the first thing I want to do is actually create a table that would kind of have all the columns that we need to see foreign attendee on. We are going to write some court that is going to dynamically generate these rose for the attendees. So in other words, we're going to have a table. Some of it is going to be static, the design part. But then we want to dynamic a legendary terrible for each for each attendee, because we don't want the money while they sit down and write the semaphore. Each attendee, that's almost impossible. If you have over 1000 attendees, I want to quickly do a table tag. All right, so this is some BC gauge steam out here table and then inside or table. We always have a TR tug, and I'm going to open and close it in two separate lines. Andi, before you can have any column, you have to have a rule. So that's just the pattern that that HTML tables come in. So in the first TR tug what we're goingto have our table hitters because those will be like the titles for each column. So in inside this tr time, want tohave td Andi, I'm going to basically just replicate all off the columns from the database. So if I take a look at the table that is generated, this is actually just a table are on then These are the column headings per per well per column that is in the table. So off course, we don't want to print back first name with no space as it is in the database. So we can actually putting our design hats on, put in readable or more user friendly column headings inside off for HTML pages. So I'm just going to go ahead and do that for each column that's in the database. All right, So I just after finishing all of them are allies I'm using t d. I should be using T. H. So if you're using TD apologizes, should be using t it. So just change out all off the TDs into th All right? So th means stable header where STD means table data. So for the headers, when you say th you automatically gets like a it stands out. All right. So teach has a different function from TD on. Then I'm going to do off a sample roar. Just a show what our data needs to look like. All right. So I did sample rules that has created another rule, and I just put in some sample values just to show you what our table or put is going to look like. So when I go back to my website and have you attendees, this is what is going to look like. You're just going to see this table looking that this But this isn't very This doesn't look like a table at all. It is not very attractive on that is, in part because I'm not using any bootstrap classes. So I'm just going toe head on over to Bootstrap, See what? They have four tables, and you can find that by clicking content on tables on. Then you see that in their court example, they have a class four tables called tables, Understand? To make her friends of this glass here. Andi, I'm just going to add it to my existing table, in my view, record speech. And they don't want to refresh and watch the magic. All right, so now you see that it's no kind of justified from left to right on. We get a little line. So if you want it with more flair you can always idea on CSS students and they actually have other options. You can get a dark table if you want. You can get one where the table head is kind of defined. You can get stripes seeking it. Ah, whole bunch of things so you can personalize it. You add on the other classes that you want, Andi make it yours. But for no well, we just satisfy with referencing table. All right, so I just realized that was one column shorts. I just fixed it on there. That's better. So at the end of the day, this is what our table needs to look like. But each role here would be the attendees data. So back in our view, record speech, We're going to go ahead and declare a variable on. I'm going to call it are. Well, that's That's right. It Our results on the value off results is going to be whatever is returned from over get attendees function. So recall that we can always reference these functions by using or crowd object, which is defined in our con file. So crowd once Khan is included on a page, we have access to this object And once you have access to this object, we have access to every single function that is defined in that class. So I'm going to say, crowd arrow, get attendees. So back to my V records results is going to be equal to crude. And then my arrow operator on, then get attendees. So remember that this former sun is returning the values on the results from that SQL statement. With that done, we're going to start exploring hole pH. B helps us the dynamically generate HTML. So I'm going to remove my some parole on I'm going to replace this block. Firstly, with a PHP tag on in this speech Beetle, I'm going to write a while loop, so we're going to see a while and then, ah, while you take some condition, I'm going to say our, which is an arbitrary value, is equal toe result. So result is what were the results is what we defined up here. So results and, um, when she's my roo operator on a keyword, Fetch on, then inside off this, I'm going to say PD or colon colon, fetch A s s l c. I guess that's association short for association So let me explain what's happening here. So this speech B block can be inserted into anybody off html off course he can't inserted without wrapping it in PHP. So I just inserted this speech we block inside off my table. Onda, I'm creating a while loop where I am going to see Give me do this operation for each Itim inside off this collection. So when results comes back from this in line it when results comes back after getting all that in these, it's going to be like an area is going to have a bunch off results, something for each thing that came back inside off this results variable, let's are embody that value. So then we're going to say for each value we want to generate our rule. So this wire loop well, of course, no. While it is complete without its quotation. Sorry, it's braces. So I'm going to use just the open breeze here, and then you're going to see that this speech B block is no screaming because it's saying that the previous the previous Peachtree Block has incurred court. But we'll fix that later on. So what I'm going to do just the same time is actually a clear another Ph. B block on this PHP block is going to have the sole purpose off closing this wire loop. So I'm opening it in one, and I'm closing it in another. And you can observe that the syntax no, is that he's because it's a okay. All right, I see what you did there. He opened it in one PHP block and closes in on the in another PHP block. No, this is really cool, because remember that anything that's not previously or inside of a PHP block is going to be HTML. So I'm seeing that while this loop is running, I want to be doing something. And what I want to be doing is generating a t r. And I'm just went, Oh, open and close this TR on every t r has TDs. So for each time or each record that I'm going to be getting back when I up do this operation, I want to generate a new rule in my table on each rule should have a TV, someone to create. I'm just going to duplicate the number of TV's I Need, which is 1234567 So I'm just going to duplicate this on then. Each TD is supposed to have a value coming back from the database. So inside off the TD, I'm goingto have toe echo this value of coming back from the database. Remember, the only way to get the value from the database is to use this are this are variable that we just defined. So once again, each time that the loop ITER it's is going to store our results from the set of results inside off our on then through are we can actually reference whatever value. But the only way to get that value in here is to put in a PHP block Onda We have toe EC or the value from the variable. So we're going toe echo on, we say dollars saying are on. Then remember that this is going to be like an ar e. So this is an area off Ari's, because one we're fitting all the results. So think off all the results coming back from the database as an area or from the table as honoree on. Then each column formulates on entry into the area. So the same way we could have accessed data using over a little just about to success speech. Be the same way the post variable loaders toe access each ari subscript using the name or are value here actually allows is the access each columns value using the name of the column as the sub scripts Understand to say our square bracket on in single quotation. Specify the column. So my column was actually attendance I d or a 10 B. I actually attendee on the score idea can just double check with PHP My admin on? Yes, it was. Attendee, underscore I d. So each off these columns can be accessed through this arbitrary this abstract value that we created on it can be accord wherever we want. So what we want to do is that for every record that is there, we want to echo whatever body was stored. So I know we only have one record, but by the end of this video, we should be able to add at least two more records, see them in the database and see them on this speech. So I'm just going to repeat this treatment. I can just copy this cord on I'm going to go into pace it, and I'm going to write back the corresponding value per column hitter that I would have defined. So the next column hitter should be getting the first name value, so I'll just see first name, Since that's the name off the column in the database on, I'm going to repeat that for each off the other columns that need data. So just to recap once again, what we've done is create a while loop and inside the while we're seeing that for each time it's iterations. We want to bring back the record in the form off available called Are On. Then we're going to print oh to the different sub. Scripts from this are variable inside the different table Dita spaces per rule. So when I mean these changes and save and refresh, this is what my patients like on. I'm seeing Mary's record on. Well, we're going to fix the date off birth value situation here on then we're seeing each thing , and we also need to address the fact that only three is going back for specialty. So let's put our form to the test, understand? To click on my navigation bar Homelink to go back home on. Then I'm going to put in Agnes Walters. Andi, see what happens when we so me turn information to the data bees. So when I do all of this fellow the former and click submits, I see that, OK, you have been registered. So we know that our database connection is working on. We're reprinting her values, going back from the post values. But then when I go to view attendees, I actually see her information newly added to the table because she is no in our database. So just the very fact. And go back to my database and click bro's again on. Then it will populate it with her new record. So, no, we're inserting records and were able to view them in our website. Now there are two things that we need to fix one. It's the date off birth. We notice that it's going over a 00000 even though we clearly selected at it on the second thing is specialty. So I start with specialty because then it kind of ties into what we're doing. No. So the situation with specialties is that we have a table specifically designed for specialty. So if I go back to pee stream admin, click on specialties, I'm going to see that I have at least one special details. The i D three on database Adminis admit as a value, no years may not be three years, maybe one years maybe, too. It depends. All right, on you may have added more or you may have fewer. So I'm adjustment to insert as many specialties here in the database as I actually have in my drop down list. So remember to insert all we really need to do is put in the name on dso. Just remind myself the specialties that we advertised were software developer, Web administrator and others. I'm just went, Oh, go ahead. And at these on by conflict go. All right. And then it's showing me the actual insert statements. I'm just going toe, modify this insert statement and just enter other at the same time. Oh, it's saying that authority did it. So I'm just going to remove this on. I'm just going to use this same statement to do other. All right? So that's why reading is important because it did it already, and I almost did it again. So I'm just going to go ahead and click goal on one more Royce insurgents. When I look in the specialty stable, I know have all four of my specialties on you don't just at the ideas here are because, well, I'm jumping from 3 to 5 for whatever reason it's keeping for. But the point is, in your system, you might have different numbers than I do so in my video, where I would have hard coded the option value off. Three. That is not the best way to do this, because we want to dynamic a legendary it on option, her specialty. So if you had five specialties, then you should be generating five options. I only have four. If you had 10 then your your experience or your users experience shouldn't be tied to my own. So I'm going to write some cord that is going to actually query my specialty stable on generate the options, her record in the table as well as actually modifying, sitting the value according to whatever i d. It's had in the database. So, as usual, the first place I'm going to go because I have a database operation to carry out is crude on I'm going toe Create a new function, public function. And I'm calling this one Get specialties on, then get specialties is actually going to look very similar to get attendees. Because all I want is all the specialties and all the columns. Understand? To copy this score on, I'm going to re freeze the SQL statement from attendee toe specialties so know that we have defined a function, get specialties. We can go back to our index speech on actually repeat similar steps where we have some variable called results on. We're going to call our function inside off our crude operator. Get specialties. No, We're going to modify over select control where we're going to actually take all the hard coded options on. We're going to repeat what we did with our while. Oops. I'm going to declare a Ph B block on inside this Ph B block. I'm going to say a while some arbitrary value and I'm calling it are once again on that. I'm going to see results are oh, fetch. And that's PD or colon Colon. Fetch association on. Then I went toe open my calibrates on, then I'm going to just close it at the same time so that I don't forget. So that's very important. Guys never forget to close your current embraces. Sometimes you, me get carried away with the court, and he really didn't remember to close it. And because of former scored, happens in between you, you don't realise, and it spent always be Buoying over this, so it's good to this open and close the same time. So what we want here is to actually generate an option tug for each rule coming back from our specialty stable someone to declare my option. Onda close it, so I really only need to do one option because it's a template and it's just going to repeat it for each recorder on. Then what I want in between option for the printed value is the PHP tag. Where I'm going to echo are on. What I want to echo from our is the value in the column name, so that's what this option tag will look like. But there's one more thing. We also needed the value, so we need the value, which should be the same value as what would be in our database table. So the i d in the databases, what the value should be. So I'm going to say value equals once again, I'm going to Well, I'm actually just want to be clear. Put quotation marks, but I have to use my Ph b tug on. I'm going to have to echo are on whatever value is in i d. So let's look at this again. I'm generating an option tug for each off the results coming back on each option. Time is going toe have as its value the i d. Coming back from the database, Andi printed to the screen for the user's eyes. The name. So when I go and refresh my browser, I get an error here. They're saying that there's something some undefined method, and I think I was spelling errors. So when I go back, yes, I have a spelling error. I said specialities. What? It is special. She's all right. So when I go back and I refresh once again, all right, So after fixing that era, we see here where we still have our dropped on, we still have all of our options on Just validate that these air? No coming from the database. If I view paid source, then look at the options being the fact. Then all we actually see a debug issue, anything undefined index. So I think I may have referenced i d. When it's specialty ideas so apologised. So I just go back and me that correction. So it's always good to have you paid sources, See if there is anything kicking and screaming in the background. So it's specialty on the score, i d. And there we have value equals 34567 So whatever the value is water, the idea is in the database. That is what is getting printed back as the value on whatever the name is what we're seeing in the drop down box. So let's just validate that everything is going according to plan. So I'm just going to add another another attendee. All right, so we're going to add Tom Hopkins. I know Tom is fully registered on. We see here that the I d value is being passed over Andi, when we view records. Sorry, View attendees. Then we still see this. I devalue Soto. Fix this. I devalue. We need to modify the select script so that we're getting about the name on, not just the i d. So I'm going to go back to my crowd, file on going to modify this quarry that says, get attendees on. I'm going to do what we call an inner join someone to say inner join on. I'm inner joining the special teas table on specialty on the score, I d equals specialty on the score I d. And then because where we have the same column name, what we have to do is actually give the tables are in essence, I can call attendees E onda specialties s on that. I'm going to see it. That specialty is equal toe s that specialty on the score. I d. All right, So if you tend us, follow this along. If you need a better understanding off this, you can always tickled my course on my school database development and muster on that will bring a lot of context as to what is happening here. But once we have relationships in a database, then we have to be what we call inner joins where we're actually joining the data between the two tables on what they have in common. And if you remember, when we were setting up or database, we had specialty underscore ideas, the primary key and a specialty stable on We have specialty underscore idea as a foreign key, which is, all we're seeing is the values that correspond with the ideas from the special do stable. So by doing this inner joy and I'm no seeing, bring back all the details from both these tables and a loamy toe do with it as I wish So for the display aspect, since I'm no getting back. All of the columns from the attendee table on all of the calls from the specialty stable, including the name know what I can do is actually go back to my cord about to my success isn't success. No, my view. Record speed. Sorry, Andi. Instead, offer a fencing specialty. I d. I can no reference name because the name is a column that I know have access toe. And then when I refresh this page, I am no seeing all of the specialties that are associated with I D three. So I'm going to go and add one more record just to drive the point home on this one is going to be some bit off gibberish on going to change the area of expertise, the website administrator, and then submits on here once again, we're printing by just the value because we're looking at the post value, which is just the option value. But then when we view attendees, we'll see the actual name because we know that we're printing the name associated with that . I devalue six. Now, the last thing that we want to change is the fact that the date of birth is giving those ears. And I was mentioning that this is because off a date for marching issue. So in PHP, the date format needs to be y y y y four wise to stems. And to these, in other words, year, month, day. But then we didn't know that the date picker, if we go about our farm is really giving us in the month of the year are day monthly or something else are. So what we need to do is tell the date quicker toe, actually change the former Toby Ph. Be friendly. So I'm going to go back to my footer fire on my way to find the function that involves their picker on going to add to the attributes here. So this press comma and right date formats colon on. Then I'm going to specify that the date for what I want is why, why, why, why? That's four wise to ends on two D's. So once of me that change, I can go back and refresh my form. And if I take a look if I selected it and I look at what is printed, I see Oh, all right, so it's actually Y y Sorry. So in jobless groups, the date four month thing is a bit different. So it's why why, mm bt? So let me refresh on do that again on right. There we go. So it's why why to give us the four numerals. So it's kind of where causing peach beats. Why, Why, why? Why If you use excel, it's why, Why, why, why? But in that was scripted. So y y so you just want to be called me sent off that So I'm just went toe enter another random record here just to see what this will turn out to be in when I submit everything went through. Well, we see here the date four months is retained. How when I go to view attendees, we see here that we are actually seeing the date format because it's no saving it in a Ph be friendly way. So that's it for how we go about selecting records and hold we dynamically generate issue mill based on watches in the database.
10. View One Record's Details in MySQL Database using PHP PDO: Hey guys, In this video, we will be looking at putting in functionality that allows us to view one records details at a time. In our previous video, we will have configured this speed. This is on screen here by the name of view record thought ph. B On the purpose of this page was the list all of the records currently in or databases or at least the attendees table. Now the task up ahead is toe alot us to be able to click on one record and view only details related to this record. To do this, we will be building a new page, and we will call it view dot PHP on the sole purpose of you is to receive on I Devalue, as printed here in this I D column, or the unique identified from the database on Bring Back the Record with all the details. For that I d. So back in our project, we go ahead and build and create a new file on. We call this on view dot PHP as usual to maintain consistency, we need to make reference to over header and footer soldiers copy these blocks from an existing page on at the header on the footer cord. Then, having done that, we would just rename the title view record. So we have you records, which is bringing back all of them in a tabular form in viewer accord. We intend to only see one record at the time, so I'm going to just remove this court reference that is calling. Get attendees because they're going to need a new function to get one record everything else. Canada, me and as you still need the header and we still mean require over database connection. Next, I'm going to go ahead and go back to our view record speech on I'm going to add an additional column so this one will be called actions, some adding an additional table hitter on I'm going to call it. Actions on actions were for this video. We will be putting one button there, but eventually we'll want to put in functionality to see the view, the details that we're about to do but also edit and delete per roll. So we have the column that say's actions, but then we're going to modify our role cord, and we're going to add a new column to be added with each row for each record, some just opening and closing the TV on. Then in this space, what we want to do is print on anchor tag that has a sole purpose off navigating to view dot PHP on passing in our query string with the I D value. Now all of that was a mouthful sake and just pay attention and I'll go over it again. So first we start with an anchor talk and I'm just going to open and fills my ankle tag on and meet the riff B E full tour. And they were going to see view start PHP on. I'm just interrupt the text of you, and then I'm going to want my uncle tired to look so sort of like a button. So I'm going to put on my boots, start classes, someone to say, class equals how we're going to use Bt and for the button on. I wanted to be a blue button somewhere to say primary now, As it stands, what would happen is that it's going to generate for each rule that we would have seen before. No, we're supposed to have another column on that column is going to have a link called view. So if I refresh the page after making all those changes and sieving, then we see exactly that. No, we have an actions column, and we have a button that says View. And if we click on view, it navigates. The are newly created view pitch. The problem here is that everyone off them is seeing just view dot PHP view dot PHP so all of them would never get to the same space, which is fine up to some extent. But what we really want, his ability that toe print out all of these leaders on that articular page. Now the scenario is that it's because you have a small table working with with only 56 feels it's fine. But if we weren't printing out, see the person's date of birth in this list on baby, we're ensuring their contacts number In this list, you would have to go to view details to see the rest off their profile. Then that is where this view page really comes into play. So just to drive home the context, I'm I'm going to remove the e mail address contacts, number on the date off birth from the listing. So when we goto our view, records stable to view all who are have listed that they're going to attend always sees their first name, their last name and their specialty, and then we'll get the option to view more details. So I'm just going to remove the table headers on that. I'm going to remove the corresponding table columns that would have come up for each of those records. All right, so having done that, we see that we have fewer columns than we had before on. But the expectation is that when we treat view, we see the person's full profile, just like when, when they would have completed the registration on, they would see that bit off information being presented. So what I'm going to do is borrow the cord that would have given that representation so that coal was on the success speech where we can have created like that little card with their information, some distant will borrow all off this HTML here on, I'm going to put it on my view page. So at the end of the day, what we need to do is make sure that the corresponding values per well for the print coat off the person's details. We need to make sure that we're fitting the values for contact, number, email, address, date of birth and everything else. Now that we've done the client side stuff, the interfere stuff. To some extent, what we're going to do is also get our hands dirty on. Go into our fraud dot PHP, which is where we're storing all of for database related functionality on. Then I'm going to create a new function on. I'm going to call it public function, get attendee details, and then this one is going to take a parameter called I D on just for so I like to group my court. I like to group all the cool that's related. So we have inserted and these appear. We have get attendees here, so I'm just going to write this one here above the get specialties because I like to see where all the related functions are. Now that we have set up the skeleton for our function, the court is going to look similar to the function above it. Forget all attendees where we have a variable called SQL And in this variable we're putting in the quarry where we say select star from attendee The stable name Where on then we need to pass in that previous holder. So we're going to say attendee, underscore idea because that's a name of Porpora. Murky column is equal to that place holder i d And then having done that, we're going to prepare the statement. So next we prepare it. So I'm going to say STM t is equal to on we call on our object. This points to DB points to our narrow Onda. We call our function prepare and passed in our SQL statements. And then after that, what we need to do is bind the perms. So we call back or object s TMT, and then we call the function in that one that's called Bind, Haram. So I hope you guys see that this is really just a pattern. If you've done it before, which we did already, then it's pretty much the same pattern every time you need to do it afterwards. So we're passing in over i d placeholder on binding it to the value that came in through our parameter on then after all that is said and done, we want to store the results. Someone to say result is equal toe ste mt on. Then we execute. So just to review this, we have our SQL statement, which is getting all the columns. So select star from use. Get me all the columns from this table where the attendee id's equal to some place holder, which we would have kind of defined before. No on. We should have an idea store those placeholders are for And then after it prepares that statement, we bind this placeholder toe. Water of value came in in the parameter. Now the expectation is that the value coming in from this parameter should much the i d for the column that we wish the view. So if it is that we wish the View Column five or roll number five or the record with idee five. When I click view, we expect that it should invoke this phone shown call passing five, then running this select worry to get the attendee record with i D five on, then return it toe result on then the end result is that we need to return whatever comes bark in this object so know that we have the SQL and the the heavy lifting pH B part out of the way. The next thing we want to do is go back to view records on make sure that our view link actually sends over that idea. So what we're about to do is use what we call a query strip. No, we would have looked at Cory Strings earlier when we looked at how they get function works for forms. Remember that they get from show would have passed a whole bunch of things in the URL on those variables, we could have gotten the values associated with those durables using over get super variable. So the thing is, you can actually create your own query string. So in this situation, we have HR equals view dot PHP. What I'm going to do is create a question by using the question mark on, then creating a variable names. Someone's going to call it I d on. Then I'm going to say this I d needs to be equal toe some value. So the value that it needs to be equal to is whatever the attendee idea is for the rule that is being printed. Remember that this is a per roll for parole cord. All right, so for each room that's it generates. Or Prince Old this attendee i d column on the first in the last name and the name. I wanted to give me an anchor tag that is going to navigate to the view page on passing the query string variable i d. With the value that is, Well, phone here in attendee i d So I need to make sure that in after the i d equals portion, the next thing that comes out of that should have the same value as what would have been printed in the number column for that rule. So I can actually just t this speech feed block that is echoing that value. Copy it and place it right here inside off the quotation marks. So once again, this is going to generate a link on the HF is going to be viewed dot PHP on we're creating our own query string. So I'm seeing question Mark and then some very big names so you could call this anything you want. If you'll feel more comfortable seeing attendee underscore, I d there. That's fine. Just to remember that that is the variable name that you need to look for on the next side . So I'm calling it I d on that. I'm saying that it should be equal to and that I'm just going to put in my PHP block because I can stick those almost anywhere on, then echo the value that is actually coming back for that role. So the same value that is printed out still left, that is visible to the user. If you decide to keep it there, you could take it off. It doesn't really matter. But whatever that value is should be upended. Toe the u R L Once this on Qatar is generated. All right, so let's test that theory. I'm went to me. Those changes See those changes Refresh the speech on then if you hover over the button and he looked at the bottom left corner off my screen, you see local host sensitive in slash view that Ph B ID equals two. So notice that this one says to this number is, too. If I go down to the one that says nine noticed that I d number in the URL is not If that is too small for you to see, I just inspect elements that we can see it more clearly and you see view dot PHP ideas. Nine. So it is very relative on pearl. It's going to have a different value. So this one is five on so on and so forth are so know that we have the idea being passed into the view dot PHP page we no need toe retrofit our view dot PHP page Teoh, run the query on, then get all the values that are coming back on by extension print them now back on our view page, Understand? To modify this comments, understand? To see, get a 10 d by idea. All right, so that we know exactly what we're doing on. Then I'm going to start off by saying if he is set. So this is a nice function that allows us to check if they, if some variable exists on, is usually used when moving between pages and use really use when you have Cory strings. We would have looked at it when we were doing our success, not PHP, where we said if he set submits then do something right. So it's the same concept. So in this case, I want to check if the get variable has some subscript by the name off i d. So if he set get I d meaning. If this subscript exists in this variable, right, that's what that's a saying. Then we want to take some form of action else. We want to take another course of action. So if it's there, then we want to proceed to print all the persons details. So if not, I want to just read that, if not, scenario first. Sorry. I'm just going to echo so I can just equal to the screen. Uh, possibly an each one Tug that as an error. Someone to see, please check details on. Try again. So we're giving them an error message that Okay, maybe you got here by accident. So that's that's what the person should get if they just typed in view dot PHP because the i d. Doesn't exist. So it's wrong. All right, on. I'm going to put in the text. Sorry. The cross for danger. Text someone to single course for that part. So bigger and text to see you got here incorrectly. Now, if the I d value does exist than what we want to do is call on our crowd. Object, Andi. Well, firstly, we need a variable. So we're going to see the results. Are are something Is he going to be equal? Toe crowed and they were going to call our get attendee Another school back here and make sure directing the function properly says, get attendee details Understand? To copy this. Get attendee details on. I'm going to put in the get superhero by personally, I delight in it. I like putting the variables. Sorry, the super very providers in another room. So I'm going to declare a variable I d on. Then I'm going to meet this. I'd be equal to whatever value is coming over. So it's going to be equal to a lot of our values in the super variable here on then. I'm going to use that variable in my form. Sean calls. I think this looks a bit neater. You would work either way, but this is just me opining on horror. Think it should look so now that we've done all of that and we have results, what happens that results is no or Ari or are variable, that is, honoree, that has all of the values. First name, last name and specialty. So I took the court from the previous page where we were posting that data. No, I'm actually going to replace that post with the result variable. So this was a Skopje NPS score. You don't have to write the post than right result. Instead, I'm just telling you that no, instead, off using the post were using results because results is hosing the data coming from the database. It's understand to go ahead and replaced all the posts here. But we're still echoing on. We're still interacting with each value viol. It's it's subscript name. So now that we've done all of this, we're going to go back to our page and look on. That is what we get on. That is what we should be getting because I left out a step. So just work with me. No, we have crowd that PHP on ditz in that there's an error online 46. If I go back to the crowd file and look at Lyon 46 I did indeed miss a step so what I should have said here was result. So we had result is equal to execute on. If we did that, that's all we started off with. Then we would have gotten nothing coming back. All right, So what we need to do is actually take this result equals take Take that part off the court coat on. Then what we're going to do is create another line in Lyon actually lying 46 and the result is equal to on. We're going to see ste empty. Andi, call the phone son called fits. All right, so I'm going to explain it. So what would have happened is that all of this would have executed or quarry on. That's all fine and dandy. But then because you only want back one role really and truly were taking just this one record where the ID's equals that we have to use this function called Fitch, which is actually just going to give us that one thing. So that's that's one thing, toe. Remember, when it is, you're using video. So when you're looking at multiple than just doing execute, that's perfectly fine. But otherwise you want to see Fitch and you put Fetch, you fetch the record in tow one variable, and you return that variable on, then in the view part. Well, whatever is fetched and put in tow on returned by. This function is put into this variable, and then we use that variable throat. So when I go back and I refresh, I'm now seeing at least some data about some other Ariz. And these arrows are because they're saying that we have an undefined index in the view. Because while I'm talking about specialty and talking about you, b and so on, remember that these are database. So this is the old post cold where we're looking at the feels from the farm coming over? No, I'm using the same court in the view part, but for the view page, we need to use the data BCE related names. Seconal. Just go back to PHP my admin and refresh myself. And that is date off birth and email address and contact number on. Then we have the specialty. I d situation here. So let me just go ahead and change all the birth, the managers and contact number. All right, So I change those old so results bit off birth email address contacts number on. Then I refreshed my page and I see here that at least those are fixed. But I still have that error on specialty. So recall that with specialty because it's in another table. What we're really going to see when we select star from that 10 detail is just the i d. So we need to do an inner join on our quarry that were passing up. So I'm just going to kind off copy. This was done in the above one for all attendees. That is the same quarry. But the inner join always has to come before the were are so in SQL we always do inner joy anywhere is almost always the last thing to come up. So let me just go through the change. I mean, select star from attendee on, We give attendee and a list. I'm calling it a and then we see inner join specialties with an alias off s on both tables in that speciality idea and it's not special. The idea is that this is what the two tables have in common. That's a nice way to look up when how to energy in tables. What do the two have in common? What they have in common is especially I d column on. Then we say we're or attendee I d is equal to or boned our place older. So then I go back to view on then I know that well, when the whole inner join is done, the value is really going to be called the name on Just to recap. If we go to the specialty stable, we see name. So I'm listening to quickly do that SQL statement here so that we can see exactly what happens so that we're not too confused. So I'm just going to copy out this SQL statement on in PHP My admin. I just went to the SQL tub. I went to replace all that was in there with our SQL statement except on which actually give this a value and I'm going to give it the value five on. Then when I say go, we see the full record coming back. So that is what that rule looks like. We see attendee i d the first name last name date of birth which ran those zeros. Email address contacts, number the specialty i d in the attendees table. The special The idea in the specialty table on the name being name off the specialty. So if we wanted name to be printed, then we have to make reference to name in our courts are going back to the view pH I'm making reference the results with subscript name. So after doing all of those changes, I can no well, back to my view record. And if I refresh once more than I see the correct value being printed right there No, I just noticed that I was slight misalignment with my foot or my footers appearing inside of this card. That should happen. More than likely I don't have on there we go. So in my view page, I copied it over only. So if you're experiencing that, you can speak. Sit with me on. Do we just close this Dave properly on? Then I re first on the order is restored. So let's just review all that we did. We modified our attendees age or view it in the speech where we're printing out every single attendee that has registered to date on. We put on off on action. But that allows us to view the details on that specific attendee. This view button is really just on Uncle Todd. Firstly, on DIT is linking to our view dot PHP page on it is passing over a Cory string in the URL called I D. So when we click it, the expectation is that we're looking to see if I d came over in this query string on If it did, we get the value run on SQL statements that looks similar to this and bring back the resulting data on then print it back to the screen. No, we did put some chord in to see if the I d value was not in your A something else should happen. What we did was to see Prince the error soul I'm going to remove. So if we look in the euro seaview dot PHP and the query string, if I remove just the query string and leave you dot PHP and print, then we see our error message coming. Please check details and dragging on all of this. Arum Well, everything that it's trying to buy into it can't buy into. So actually, as a last act, am goingto flipper on this if statement, so I'm going to see if not is set. Meaning if he said if this doesn't exist, right. So when you put not too, you're doing the negative. So if not you said he set that, then I want to a cool these statements. All right, so I'm checking for the negative First else. Meaning otherwise that it is there. So if not is that means those if the get i d super if they get idea in the Super Variable does not exist. Then we echo this error message else. We want to do all of this, and I'm going to actually move this disclosing breeze. And I'm going to end this whole if block after this devious printed someone to go after this def on, then I'm going to start a PHP block, and I want to say PHP on close. The brace on then close appears to be block. Know what this is going to do is actually just say, if the get i d doesn't exist, just prints the era message. Otherwise, if it exists, which is what we're hoping for, meaning it's in the URL. Then we get the value and we get the results, Andi, In the same if block because it hasn't closed it all off. This HTML will be rendered on everything. Up until this closing, um, breeze will be done. So if you look, I'm clicking this Bridgestone here and it's gonna highlighting the breeze up here so these braces are connected. So when I refresh this speech, if the girl isn't is empty, then we're just going to see the era message. We're not going to see any other nasty messages about anything not existing are are missing or anything like that on. Then If I put about the query string so I say ideas five, then we get back. Oh, are red card now? I encourage you to player on with the speed you can customize. You make it into a profile page, whatever it is, put some more issues elements in their Andi. If you had more details to the database and I have, then you would have off more full details Page and I do. But essentially the concept for me is the same. You pass in. The idea is a query string. You get the value, your in your query and then you print out your results, I will be found. This Listen valuable. Andi, these just remember to have fun as you modify this and customize it's to your liking.
11. Update MySQL Database Records using PHP PDO: Hey, guys, In this video, we will be looking at how we can go about adding functionality toe edit our records. The context for why you would want to edit your records are well up deity in terms off the acronym fraud, which would be the U for update, would be that maybe in current information was was entered initially, and you need to make a change. So, for instance, with the Mary record, if we view the details, we see that the date of birth is 00000 It's a bunch of zeros on that might be wrong. All things might be incorrect. Maybe her name was built in currently, or the email address. What? The fact is, you want to be ableto update the report from this screen. So in this tutorial will actually be going ahead on adding an edit button on building off that functionality. Now back in our view record speech, we would have already added off column that said actions and this actions column is really just going to be holding all off the buttons that would effect actions. So the first action that we would have worked on is the view auction, which we have a line here by this anchor talk. But what I'm going to do is expand this problem. I'm just into Indian so we can see where the problem starts and stops. So inside of this TD, I'm actually just going to add another button or another anchor tug. So just duplicated this land with control de on. Then the second anchor tag is going toe. Have edit dot PHP instead on then what we want to do. We want to retain the father. We're printing all this idea as part off the link because it will be necessary on we are going to change the class from BT and Primary made. My anybody would be art and someone to say warning for an orange button on then that takes that is printed should see it. But if I go back to my page and refresh, then I should see my new button here, which says that it on the link should look very similar to the viewing where we're seeing. And it's not Ph. B. And in the query string, I d equals two. Next, we need to do the edit screen. So back in visual studio code, Understand? To create a new file on this one is going to be called reddit dot PHP. Now, usually the edit screen looks just like the create screen, and we can policy here for a little context. Our registration form has all of the fields that need to be filled votes. I'm just going to go back to a registration form which, remember, is our home and we see first name, last name and all of the values that need to be filled out on. Then we submit. Essentially, these are all off the values that we also need to be able to update it just in case. So really and truly, our edit page is going to look just like our registration page. Maybe Beiring the title. So having created the edit page, I'm actually just going to use my code real quickly, and I'm going to go to my index page, which is very destruction form. I went to copy all off this on then, with the pieced it in the Edit start PHP page. We need a few changes. Of course, I'm going to change this one toe edit record for that's name off this speed, that's a title on. I'm also going to change that each one tug, tow, edit record. So if I navigates toe reddit dot PHP and I can just go to the girl and type in PHP, then I will see that it looks identical. But the Texans any record the title is different, and we can change that text in the submit button to see if something like Submit changes or save changes on maybe use a different button color on now. Use success to get a green button. All right, so that's what are edited Page looks like. Oh, are edit page looks like, So I'll go back to my list on the expectation once again is that once I click this edit button, it will navigate over on. We see that is already party in the chorus to know what we need to do is actually execute a quarry that gets the record with the I d. Being passed in on. Actually, Prince, these values here for us to see now back in or cold, we need to get busy with some PHP. Firstly, we're getting results on this results, variably storing the old put off this function. I'm just going to remove this coming from the speech. All right, so we're getting the old put off our crowed get specialties and remember that we need this function and the results to populate over drop down list for all the specialties that are in the specialty table. However, what we also need is the add some function call that is going to call the function that is going to return the one rule that we're looking for. So if the I devalue off to is passed in to this page, then we need to bring about the record with i d volatile on actually prints the values toe first name, last name, etcetera, text boxes so that they actually appear inside off our form for editing. So we already have a function that we would have defined instead of for crowed file on. We call it, get attendee details, so we call to get attendee details on what we're really doing is just getting the I d. On getting back. Whatever details there were about that attending this function can actually see the Syrian purpose. So the same function that got us the details can actually serve the same purpose for 18 because we really need are the data points from this query. So back in or edit page I'm just going to create another variable. And I'm going to call this one attendee. So that's attendee. Meaning this is the data for the attendee on. We're going to call our objects crowd on. We're calling the function Get attendee details on then Well, I skipped a step So before we can you do that? Because remember, we need to actually pass in this parameter apartments are there. So what we need to do firstly is check if they were flipping over during the bad first sourcing If is not, he's set gets on We're looking for the parameter i d. So once again this is seeing if they get super global Variable does does not have a subscript i d. So that's what this is in. Does that exist? If not, then I want to print some error message seeing arrow So I get back to this end Make it fancy later on aren't else What do you want to do? Is go ahead and gets the attendee data All right, so firstly we always use a variable first, so I d is equal to And then we're getting that value from the get super global called idea with the subscript idea. So we get that value, we store it in this and then we're passing that as the parameter. So once again, we get the value from the get parameter. We start in this variable, and then we passes variable into our function. Now, what we also want to ensure is that we only see the form if if we actually got an idea in the parameter. So I have the if statement to see echo on arrow. If the perimeter doesn't exist else, we want to actually start a process on then printed forms. I'm just going to move this closing breeze from this speech B block on, then create another Ph B block at the end off the form to close that statement. So that means all of what happens in between If the else statement on this closing briefs, it will only ever happen if the gets parameter, the I d parameter actually exists in or forestry. So we closed the brace. Close our PH B block on, understand the fix my indentation, Andi, so we can proceed. So let's recap. Once again, we have our I devalue coming in from our parameter and then were running the choir to get all the choir details on. Then we want to store that result inside off a variable called attended. So what we want to do to actually get these values printing out inside off the creditable reasons of four form would be to echo inside off the value attributes. So inside off inputs for first name, I'm going to put value is equal to on. Then inside off this, I'm going to create a PHP block open and close on. This speech we blocked will have a core overact, Indy variable. And remember that this variability is acting more like an area. So we need to specify which part of this area it is that we intend to print on since we're in the first name textbooks. Then we intend to print the value coming back for first name. And then once we do that, we see if we just refresh our page, we see that Mary will appear because it was Mary with I d to if I put nine and refresh, then that Clarence again. And then we see the new value for record night so we can just go ahead and do that for each off the text boxes at the very least, second of Skopje, the Value section. And then I'm going to piece that inside off last name. And then instead of her friends in first name, I'm winter reference last name so relative to whatever the textbooks was far. Do you want a reference that corresponding value? All right, So I proceeded to do that for all off the text boxes on Defy Refresh. Then this is what it should look like. You should see the corresponding vice if you get any era. Just make sure you check your spelling on the series in the CART column names. And if you see a Miss much ensure that your friends in the car it's column inside of the cart. Creditable reason. No, What I didn't do was bind the era off expertise from the drop down list to the one that's coming back from, or Query. So do recall that our quarry and I just jump over to Ph. FEMA admin. If I around that quarry I will be seeing specialty I d three. So let me enemy edit this. We're looking at record nine here, so I'm just going to see where it's equal to. Nine. There will go quickly on then. This one is bringing back. Well, it's database. I want to find an attendee who wasn't a database at MIT and that would be attending number six. All right, so let me just reload this page with I devalue being six. So you know, what is that? The database admin did not change everything else she in just relative to the new record except the drop down list when the drop down list, in fact should change, since the specialty ideas really six and Web administrators should be what is selected in the drop down list. So we have to make sure that the selected value for the drop down list looks like it's it's clearly representing the cart one. So we're going to have to get a bit creative with our select section on. What we're going to do is for each option we're going to have to do on each statement subject if they specialty I d being printed by this wild group is equal toe the specialty. I d being returned inside of this attendee. Hurry! So to get this process started, I'm just going to bring some lions here, someone to echo in the next line and then put the option below that so that we can see exactly what is happening. Clearly so inside of this stop option tug. Where? Open adoption target. I'm printing that. The value should be specialty. I d I'm going toe after that quotation marks, start another PHP block and then inside the speech B block, I'm going to have an if statement. So this is just highlighting the dynamic nature off beach. Your whole dynamic your Web applications can be when you can insert Ph. B at will for whatever purpose. So in this case, we're going to see if on our statement is going toe ask The question is our specialty I d some person to copy this if this value that is being returned from this very subscript on this iteration off the while loop, if it is at any point equal to and I'm just going to reuse that. But I'm checking for attendee this time. So if at any point, The value that is being returned for this particular iteration off the while loop on it is being stored in our specialty, I d if at any point it is the same value that is being brought back by this attendee record and if that is the case, then we want to echo. And we're just echo the text selected because in regular HTML, if we wanted on option to be selected would have just literally typed the word selected right here inside that option. So that option would be the selected option. So what we're doing is seeing that if this condition is met So this highlighted your statement. If this condition is met, then we want to just echo the word selected on were still inside off the option tout. So it would be echoed in a fashion light. This So having written that chord, I'm no one to go back to the issue of pitch on when we refresh for idee equal six. We see that know the selected value is Web administrator. So if I go back to view attendees on Die click edit on Mary its database at me if I go again Andi believe it waas the last one or the second to last one. Right now we click edit, then we see Web administrator being brought back. So that means our if statement is actually looking to see what specialty I d came back with this person's record on. Then it is seeing if it is the same value that is being printed because it's printing four values right now. If the idea that is brought about much is which of ideas being printed just made that one the selected one. So it appears here. Now that's half off the battle done. If you want this fire, that's excellent. Now the objective off this would be to make sure that when we change the beats like, for instance, we will have the date here being 0000 where they establish that that's not really what it should look like. So maybe this person should be born in 1973 on October 2nd. Part on. Obviously, the first name doesn't mean what, since ah changes the Archibald on this last name would be Thomas On the email address would be a Thomas at gmail dot coms off course. All of this is fictional, but I want to show you what the update is really four. So at the end, off making all these changes on putting in a more realistic contact number, I expect that when I click save changes that when the next speech is going to show me that all of the changes that I just need were meat and registered inside my detail bees. So, in evaluating our next steps, we need to remember that what we're doing is using a form on the form uses a method on also some action. So success, not PHP is the action, meaning once we click submit, it will try to never get the success that BHP No, we already established that success, not Ph. B is the auction page for our registration, meaning after they fill the form the first time, Then they should go to the space. So what we want is a different action page for our edits. Some just want to create a new file, and I'm going to call it Edit Post, not PHP. On the purpose off edit Post that Ph. B is just to receive the data from our post actions. So that means on the edit page on when to set the action to be edit Post Doc. Ph. B on what happens is that we're going to send over all of the data that was entered on this form imposed on edit courses going toe except this data on Then run the database operation toe, insert the records or obey the records in the database after it does them that operation. We don't need it to display anything. So we're just going to redirect toe. Our index was not in expiate our viewer record speech. So I did some comments so that we have a guide as to what is being done at each point. No, firstly, getting the values from the post operation. I'm actually just interview school that their authority. So I'm just going to jump over to the success that PHP peach on then really just wins a copy All off this cord listed here. So the thing is, we're using the same form on, but we didn't change any off the names off the elements on this forms all of the names, the email on the car, on the phone for reds. That's the name right all of those things remain the same. So really and truly the same Holstein cool that we used for success on the registry. Some pH we can use that same post scored for the action on the edit post off course. I still want you to appreciate the fact that if you make any changes to the forms may be left out one of the fields or your added feels or whatever it is you did, you need to be ableto understand and appreciate the differences on the nuances between the two, but for nowhere, reusing most of the cord so we don't have to make many adjustments. Another very, very, very, very important thing we always should have on our edit page is the i D. So the actual I devalue that we're getting here, we need it because we need to actually send it over in the post action. Because when we're doing that update query, we need to update where I d is equal to whatever record it is that we were anything. So the easiest way to get that I d. And you don't necessarily want to display this. I devalue to your users because on the form putting I random six. This is I devalue six. So you want to put a random value six somewhere here. They really don't need to know the I d off this person. But what you can do is use a hidden field. And that's a very common practice of user hidden. Sorry. We use our form elements off type equals shouldn't on. Then we can set the value just the same way we set the value for every other ones. I'm just inte Bara. This scored from the first name block set the value and echoing attendee I d instead. All right, so we have the value off or hidden inputs. Sorry, I don't know why attempt for him input. Apologies for that. So we have input type is hidden on. We have the value being attend the I d on. We can disclose that so that when we post our data, we actually are maintaining the value going across the pages. So a lot of the times people leave Oh, the or neglect the father. You need the i d. In the speech and then went to get to the posting. It's not working and they don't know why. So I'm just going toe either. So that's one of those minor adjustments that I'm going to make than that's the difference . One of the differences between the create on the edits because upon creation there's no I d but orbit prior to creation or other. There's no I d. But upon anything and I d would have existed already. Some just want opposed this. Oh, when setting up the elements, we need a name, so name is equal to number soon to call it i d on dso when I'm posting ideas equal to post super super variable post subscript Heidi. So I modified my statement. All of this needs stopping inside the statement If if if the post exists, then we want to extract the values, call the crowd and redirect else. We will print that arrow court so I'll get back to printing the era Court on. On that note, I hope you've realized I'm neglecting to print early each time because they neglected to printed here and I'm neglecting to printed here. The fact is, we can probably make an include speech, right? That just has, you know, a standard arrow message that we know, print across the speeches, so we, instead of printing echo each one every time we have an includes or with the sprint echo each one and then we just included right here in the else statement. So at the end of this activity, I'll actually do that on opiates. All the side are the pages in our website with that coat. Now we need to call our crowd phone, son, but there is no code phone son for anything. So, like all the times you have to go over the crowd on, then I'm going to create a new function. So those are to get someone to create a function right on unease, Insert attendee, and I'm going to call us on public function it it attendee on. Then we're taking a parameter i d. And then inside this function, what we really want to do is just right on update SQL statement with the placeholders on the value. So I did say only I d. But I d is not the one a perimeter. We actually need all of those values coming over from the form which would be F name l Neiman, all the other ones. So I'm just going to copy these parameters. Since they're the same parameters, the same set of values on I'm going to include them in the perimeter, listing with ideas. So have I. D and all the other fields that were expecting inside off our table on then because at the end of the day, we really don't know which one is going to be edited. I don't know which field the person is going to change in the edit field, but once I click save changes, I need to be prepared to make sure that any changes made across anyone off these will be captured Onda stored. So I'm just interested An updated statement. So we always start off with an SQL variable and then we have the update statement and then I will be realized. I know also that SQL is not really case sensitive. So sometimes I'm writing in uppercase. Sometimes in lower case on off course. Once you're in adults, you can always go over till your PHP My admin, Onda, Look at what it would look like as an upbeat statements. They opened this up. You goto attendee can go over two sq well on then you can click on the orbit on, then the update statement. Can they? Can I give you that template? Well, you can copy this on Pace it inside off the SQL variable off course, replacing each value sex son with the placeholders. And so I'm actually going to just do that. So I copied that on inside of the SQL statement on pasting it earned on. Then each place I have that value in square brackets. I have the place older. So the thing is, though, this can be misleading because we're not updating. We're not setting a new value for idee. So according to this update statement, it's asking to set attendee I d to be equal to some value. We don't need that because we're not setting that in the value i d. What we need I d for is the wear clothes or we say, where one We need to say we're colon ideas. I'm using a placeholder there, but the update statements is basically the word update than the table to be updated on. Then we say set. I know these are not single courts, so I would have used them in a previous but video where I copied it from from PHP my admin . And then you would have seen them. Those are not single courts. Those are what you call apostrophes. All right, So the foster is basically are a little secret talking inside off my sq als SQL syntax. But they don't conflict with double courts are single courts arts. I just want to everybody to be clear on that. So we up the it the table, and we set column is equal toe value on value. Here would be cool on f name Andi for as many columns as you're sitting values. You have them distance. So we're doing it for first name. We're also doing for last name on each one is separated by a comma the date off birth. I'm going to set this place order to be deal be on email address sitting the placeholder to be email and I do the others so quickly. Once again albeit table set column equals placeholder Kama Kama column equals placeholder fires militants as you have columns and values are place holders to be set on. Then we end it all with a where clause were something So we want to see where a 10 d underscore idea is equal toe the placeholder I d. So this is no going toe. Find this record with that idea water I devalue. It is our passing in, which is the I devalue that would have been hidden in our form on post it over on, then passed into our phone shown on Then it's going to find that record on then update all of these columns with the values that would have been coming across in our parameters. So now that we have that SQL statement written, we need to do the binding. Some just go into this. Copy this because it's pretty much a CNN cord. All right, on, I'm just into pieces here and then, just like we did before, over creates our insert attend. You did a track that so I'm actually just going to do a try catch because it's good. Practise the trans action on, then catch the exception in case there was an error. So I'm just going to rattle that try catch block on, went to try to do all of this, and if it was successful, I returned True. That speaks my invitation on. Then I'm just going to literally Skopje all of this because pretty much were just repeating on a number of things on DWI. Want consistency with whole. We look for errors and deal with them should they arise. So you'll see that your edit on your inserts operations tend to look very similar in terms off the fields that are being captured and the options being done. The only difference is maybe the SQL statement on the father. You have to account for the i D. Passing between fields or between screens when editing. So know that I have my crowd function defined. I can go back to edit post on actually call it. So I left out of step. I did not make reference to my connection file. So in every other page that we made some database call I had required my devi con I did not do that here. No, I didn't need that Ah header file because, well, this speech is not really supposed to be displayed. It's like edit and it's an intermediary. So it just does its operation and then read ARATS so you won't really see the edit. Poor spit. So that's why I'm not making this much of an effort to actually have the aesthetics there. So I'm going to make reference to my d. B. Khan because obviously I'm going to be doing some database operation. And I remember that once this file is in view off the page, we can just call on our crude objects. I'll see results equals on. I went to call on micro object on inside crowed. I'm going to call my function, which is edit attendees and understand the copy all of this Andi piece here. So because I'm using the seam variable names as a permitted you see, it's easier at the Skopje and paste, so you don't have to. But I'm just showing that made this this probably five minute operation off, typing out all of this into 30 seconds. All right, so once we do all of that, we can then see if on going toe check if result, remember resulted store in the old put off. This function on dysfunction is returning to a force I'm seeing if results meaning if this is true, so this could be rewritten as if true. All right. But I'm just in to see if results then we want to redirects to the index speech or redirect code looks something like this where we say Header on, then hitters, a function that takes a link So the link is index dot PHP. So if this was successful and we want to redirect to the list else, we can probably print on Eros, so echo error on, we will revisit setting up these generic error per insult printouts on standardizing them. All right, so once this is in place, the expectation, once again is that once you click, submit on this speech are, well, our button sensitive changes. But it's still a summit, but it's in the same. Is going toe posed over all of the data that may or may not have been modified but had been present as values inside off each of these controls on. Then it's going to hit this page as the action where we connect or databases get all of the values and then go ahead. Andi, call our operation, which is going to run the opiates statement in our database on if that was successful than we just redirect to the home screen. Otherwise, we print some form off arrow message So let's try that. So I'm going to go back to the form and I'm going to re fresh and I'm going to me. These changes some putting Archibald Thomas on the other details, changing his date of birth toe 1965. Whatever it is. So I'm just making some changes to make sure that I can see them reflected after I click save changes. All right, so because they're using email as the data type here or the input type, it's telling me that my email address is wrong. So I had a common set of a full stop. Let's try that again. Are we're getting a PDO error here and it's seeing inviolate perimeter number. Oh, I know why I've boned everything but the ideas. So because I copied the buying programs from the top part, I didn't remember that I have this additional parameters that needs to be boned. So I'm just going to duplicate that line and change this the I d and bind it to the idea coming in on. Then I'm going to just try that again. So if I f i V will say that the form re submission will take place meaning it sent up to reports the data, and then we continue. I understand to go back. Andi, I'm going to look at my cord. Ah, the location. So sorry that we should have said location, comma, colon index dot PHP my butt. So the header tix the text with the word location Colon on, then the page. All right, So everything was successfully able to dinner you, Derek. So let's try that again. Someone to resubmit this form on git redirected to the index speech. So I had forgotten that the index pages actually the registration, please. That's not what we want. We want to goto view records that PHP and we see here that the required was indeed edited so we can be confident in the fact that our edits court works. But I'm just going to change that. Read, Eric, because I wanted to go to view records dot PHP. All right, so once of me that change the fact teams mirror pop ing's, I'm going to edit Mary Poppins. And she is really a software developer, not a db Admin on guy. Say that change. Then when it comes back, it redirects toe our view records and I see Mary Poppins with her new specialty. So that is so. You go about adding any functionality to your PHP application.
12. Delete MySQL Database Records using PHP PDO: Hey, guys, In this video, we will be looking at how we go about adding functionality to delete records from or application. The objective of this video is to ensure that we have a button beside or view and edit buttons that say is delete on unclean. It will proceed to remove whatever rule was selected. Now the first teams I'm going to make is, in my view, records that PHP pH where I'm going to add another link, which will go under my actions column some distant to duplicate my edit button on. Then I'm going to change the text to see defeat on. I'm also going to change it to Bt and Danger, which give me a red button, all right, and then for my eight year, if I will have delete that PHP. So after making that change and refreshing I know, see, that's have a red button on recall that all of them are printing, wrote the attendee i d. For the rule based on the iteration off that while loop so each off them would have the same idea value At the end of the difference is that they're navigating the different pages . Now we can see that we have. Ah, spam record here. Record number nine. This person. This This is not a person as far as this information would indicate. So this is my target role for this operation. By the end off this video, I should be able to press the lead and remove this rule from the records. So since our Borden is targeting a page called the Lead PH. B, we do what we have learned to Dubai. No, and go ahead and create a new file call. It deletes dot GHB on then in this file where we'll just copy the most essential things which are really just the require for the Devi connection. So really and truly there two ways to handle delete. Some people actually do a prompt. Some people navigate to a page showing all the details of the record, their bloated little and ask you once again if you're sure you want to delete on some people want to click delete then that they just run the command and refers to a page on the deletion would have happened. So we're entity. That third option, Andi, I challenge you to put in functionality that would actually have delete Load a page that shows all the details off the record about to be deleted on, then have another button that once it's clicked, it will actually action that delete Kinton. How you get that donors to use a form because on submit, you could go to a post page where you actually get the i d and proceed to delete. But for no, I am just going to proceed with the simple my third, where I'm just going to have my PHP block. Andi, I'm going to require my connection to the detail BCE on. Then I'm going to check if the get variable exists. So if it doesn't exist to remember, we check for the negative. So that's actually according pattern. We check for the negative first, take some action on the action on quantity or moment mixed up my braces here. Action. I'm going to take if the i d. Sorry if I d doesn't exist in the get parameter. I'm just going to print error on Remember that this is going to be part of our final touches where we're going to create some construct that is more consistent with the error message that we don't have to keep on typing it over and over. But otherwise, if it does exist, so that's else, then I'm going to get that idea value and place it inside off a variable called I D. Then what would be nice is to call the crowd function that will remove the record on, then redirect to our list. So I did some comments here so that as it puts in the cool, we know exactly what's happening. But the point is that when it hits this speech, we just want to get the i d. Run the court to delete it from the database and then redirect wants that operation was successful. So we need a function in our crowd up in our crowd class that handles deleting. So we have insert, we have edit on. We have some gets so on. Anything gets, I'm going to put the delete, so I'm just going to write up the public function. Delete attendee on this function is taking a parameter called I d Onda. We will be Skelly be doing very, very similar things. Once again, you see that all the patterns kind of repeat themselves. So we're declaring an SQL statement where we're going to see elite from on the table. Name is attendee were attendee underscore idea is equal toe are place holder i d After we write up or SQL statements, you know that we need to prepare its understand to copy that copy and paste, so don't have to re type on that. I'm going to do the same with the binding off the I d parameter. So most of those things we're doing our in over get attend the details, Right? So these stop three or these two in addition toe the execute on, then we can return our results. All right, so well, we didn't wouldn't be clear results, So we really don't even need toe return anything at that point, we'll could just return True. So once again, it's always a good idea to have our try catch. So I haven't been very consistent with that, but in our finishing touches, we will definitely tighten up on those things. But for no I'll just say try on do all of what we just outlined the snow on. Then once that is done, we return true on. Then I'm just going to borrow the catch freeze from another function on. We want to tear up the same operation to catch any zero that may occur in between while we're turning falls. So then I can see back on my delete page that we want to call or should objects? Well, we want to assign the results. The all come off our form shown call toe the delete attendee function in our fraud objects . So I pass in the i d parameter as we got it from the gets on. Then we see if result Then we redirect So redirection is hitter Onda we open and close parentheses And then inside of this report a text which has location, colon and view records that PHP because you want to redirect toe our list stuff reports. So if if results are successful, then we want to do that else. We want a print, that generic aero message. All right, so let us try this operation. The point once again is that once it hits the speech, it's going to connect to the database. Get the well check if there is a value coming over in the quarry string. If not, then he's going to print an error on. Then we're going to see if there is a value. We want that value. Then you want to call the delete function on. Then we want to redirect The view requires once that's done so it would almost be transparent to the user is complete. And the page should refresh toe after this operation happens rather quickly so we can just go back over here. I would just look with three fresh and they were targeting this rule. So let's see, before court works, we pressed. Delete Andi, I have a spelling error here, so I'm going to go back into my crowed function on and I have attendee with one t in my SQL statement. So that was the error. Someone to try again on notice. All the your looks. It is the lead. PHP i d is nine, but I'm going to go back and I'm going to start over some clicking. Delete on. There we go. It's removed. So it didn't prompt it didn't show the details off the record once again, That's that The simplest, quickest way to delete a record on off course deletes are not reversible. So if we wanted a least putting a prompt because sometimes users me miss click. They think they're clicking at it and treat delete. But we don't want to just remove it upon an accidental click. We can actually just use JavaScript and insert a quick prompt into the peace without when it's clicked. There is bare prompt, a default prompt so, you know, confirmed the quick action. So back in our court, back in the view records page, I'm going to edit the uncle Time for the delete on. I'm going to add a JavaScript function called on Click on the value that will be assigned to this will be returned and then confirm. So this is, ah, javascript function called confirm where you actually pass in some text here. That would be like a promise they be something like, Are you sure you want to delete this record? All right, so the expectation is that when they click this link, they're going to be prompted through some beef, all of javascript. So this is almost built into the browser, writes Amy, look different from Brazil, bro's. Or but the fact is that on click, it will show the confirmation prompt. Are you sure you want to delete this record on. It won't go any further until the difficulty. If they could cancer, then the operation is The quick operation is canceled so it would not navigate to the next page. All right, so we can test that. I'm going to refresh after making that change on. Then I'm going toe accidentally. Click the lead beside Mary Poppins on Look at that, it's asking. Are you sure you want to delete this? Reports. I can always cancel on cancer the operation. And if I click okey, then Miri's required would be deleted. And ironically, deleting records is pretty much the easiest and quickest thing you can do on recovering from. It is probably the most tedious and time consuming procedure, but that's it for this video on That is So you go about completing your crowed operations. We did creates. We did read. We did updates on this one is delete
13. Final Touches: Form Validation, Error Messages, Success Messages: Hey, guys, welcome to this video where we will be applying some finishing touches to our application meeting will be cleaning up some cold, cleaning up some off the visual elements and just look handsome little key concepts that could help us be a bit more modular. So I'll get straight into it. And the first time thing that we want to do is to add more, try catches toe our code. We would have had some functions where we didn't put in all the track catches. So I'm just going to go into my crowd, file my crowd class and you notice that some functions tried and caught exception. Others didn't. And so what I'm going to do is just wrap each one inside off a track at someone to start with, get a Cindy's and remember that all we need is a word. Try on then, Considering that we already have tries and catches all over the place, we're really just trying to catch the same type of exception, which is the PDO exception. Second, just really write the word try and copy and peace to catch on just weeks my Indian t shun on. I want to do it for this one function. So I challenge you to go ahead and do it for the other cell, completely others off screen, and then we can reconvene. All right, so that I would have completed it. Andi, every function has that try catch? No, some of them me returned. Mixed results. Meaning you're returning results as well. Results from a query on, then in the catch here, returning fall. So the thing is, because PSV soul kiss Orwell type insensitive, rather or loosely typed, it doesn't really matter. So if I checked if results, that would actually return a true on, def, I'm returning false than it would literally return force. They can still kind of use that The fact that it's not returning the same data type, right? Um, but on the other side, you can actually check the variable. So if the variable has data and it's not returning anything negative, then it would be a true regardless off the content on, Well, the function would return falls either way. Now that's the first and possibly the easiest thing that we have to do. The next thing that I want us to look at is setting up success and aero messages so we can go to our success that Ph b ah file. And here we would see where we were echoing a success message versus a failure message. No, What I would want to do is actually just kind of have a generic arrow message on the generic success, Mrs. So without being toe specific. Because then based on the page, you're going to have to re type this every time. On we saw that we can off ran into a little blood, our got lease, and we just started echoing arrow instead off echoing actual error messages. So what I'm going to do is create and includes while filing the includes folders are click on the ankles folder on. I'm going to call this one error that PHP on. I'm also just going to create another file. Same time success, well, success message. So let me qualify it as message because I already have a file called Success, Not Petri. So success message that PHP on. I'm just going to right click on error on Rename it to error message dot PHP. Now these two will just have on each dinner block where I'm just printing. Well, I'm actually going to go to Bootstrap. And instead of using the H one tags by themselves, I'm actually going to prints on alert. So both sharp has alerts component on this is what they look like. So I want our green alert whenever it's a success and, ah, red alert whenever it is on error. So I can just copy their code and I look for the one that's a success. So by no hope, where are comfortable? The fact that most things in bootstrap that are green are success. The color The naming is pretty much consistent across all the controls. But I'm just going to take this thief class. Place it inside my success message that PHP file on. I'm just going to reward this, Andi, if I want, I can probably make it on each one, but I'll just leave it a Leave it as default. So success messages going to just give a generic success to say operation has been completed. Andi, I'm going to see an error message. I just use this one to danger. So it's the same alert constructs just with danger. Instead, on the error message will say operation and confident error. Please retry. All right, so you can put your own messages. But the fact is you want some level of consistency instead off. I mean, if you want custom messages per screen, that's fine. That's totally fine. I'm not saying it's wrong or bad. I'm just modifying my own so that I don't have toe continues to be typing all these things . So what I'm going to do is when I would have printed an error, actually, just include the file that gives me that are a message. So right here, I'll just include Andi, I would say includes which is a folder slash error message dot PHP. All right, So the expectation is that if I navigate to this speech and this is the edit page, if I never get to this speech on, there is no I d. I would have printed an error. But instead, what I expect to see is the error message well, coming up as an alert. So let's test that theory. So if I go to my page and I click on the edit, then the expectations that we see the formed it, it's Mary Poppins is record. But then the Aramis. It should come up if there is no I d parameter meaning somebody just went up here and type in the URL to the edit page but did not put in the query string. So when I do that and press enter, then I see my ERA message coming up on This is what we would have embedded in our page through our includes. So what I'm going to do is go through each file that would have printed an error on where I would have had an error message. I'm going to replace it with my include. So then off making these changes, I would have made the change to edit edit post op, Ph. B View, Ph. B even myself, says speech, where I had been printing error viewer record with No other one was skipped on delete. All right, so all of those pages would have been updated on in the same way that ended this for the error. I just do that for the success message file, so I just go back to well, wherever I would have had success and I would have had success being printed on this screen where you have been registered. So just replace this with success message on anywhere else. You would have been printing a message to say something was successful. You can go ahead and just include that file. The next thing I want to do is to prevent people from actually navigating to this speech without the presence off the choir strings. So one way to do that would just be to say, if there is no choir string. If I d doesn't take this, this redirect back toe some page that everybody would see. So if they tried toe type in edit dot PHP by itself, then it checks. It sees. There's no question, and it just re navigates toe. At least view records, a PHP. So an easy way to accomplish that would be to go to our page like Edit on did in this part where well, we just change Toto print the error message. But I could easily just say header on inside of that. We just put the location colon on, save you. Records start PHP are So what this would do is if there is a navigation attempts to this page, which is our edit page on the header. Well, the under idea is not in the query stream. Then it just redirects. All right, So we could actually do that for, you know, for the edits. Who could do that for a post Anywhere that you're concerned that people should not end up without having the cart informations under that for they header Andi Well, for delete. So somebody tries to go to the lead page to the lead page without putting in the query string. Then we will just redirect. All right, So those modifications have been easy enough. Another one that I would want to do is put some space between my felt navigation bar and the content that comes immediately after. So you'd have seen with the error message that it was actually seemingly attached to the nut bar, which I don't want, So I want a little space there, So I'm just going to go to my header file on. What I'll do is at the bottom off the nav bar. I'll just put a PR chug right. Remember that this is rendering on a per page and it's rendering the top part off the issue Mill contents or enough bar on. Then I wanted to also render a space all the time. So what's going to happen is that when I navigate between these pages, we know CS space, so the table is no longer seemingly attached to enough bar. I would have said that good website design would have all speak every page reachable from every other peach. So in other words, if I go to the details speed, I should be either be able to go back to the listing page or to any other page on DSO. What I'm going to do is add buttons here that would actually allow me toe head back to the list or edit this record or delete this record from this view. So I'm going to go to view dot PHP on. Then we will modify over html toe, reflect what we require. So in my view, Ph. B page at the end off that card, I wanted three buns to be printed, so I'm I'm just going to bar these three buttons here from view records on what they are. Well, one is back to least one is any 23 leads. I'm just going to borrow them on me. The changes accordingly Go back to view, piece them underneath. Or maybe I can. I'll place them under the dif so we have the card and then we have the three buttons asking you which direction you wish to go. So the 1st 1 I'm going to see this one should be back to list. And I'm going to meet that one BT and in four. So it's a light blue like a teal. All right, so this one would say about two list, which means that the H ref should be view records, not Ph. B on viewer cars does not require any form off idee query string on so we can remove all of that from the eight riff on. We just have the button to go back to list. Next we have the edit button on de everything else can hear me in the same except I'm using the variable results in this speech toe represent any off those values. So I'm going to just change this are to result so without changing the Alami explains. So I'm printing on Uncle Tug on the trip is reddit dot PHP on I'm passing in the quarry stream, just like I would have done from the view record speech passing a query string on the value for the i. D. Variably inside of that query string is going to be that 10 d I d. Now the reason I changed it from our t the word result is I copied the button. From here on in the viewer records file are is the designated variable. However, in our view file, we see that we're using the variable results everywhere we're referencing a value. So I'm just going to make sure that I'm echoing results with the subscript attendee I d into that query string area on. Then I'm going to repeat this feat for the delete, which is right below on some echoing result. Attendee i e no. When I refreshed my page, I see my three buttons And if I hover, I see that this one goes back to viewer cars. This one goes to edit on this one from's for a delete. Andi, I'm just going to add a br tag in between. So one br tag after the div so that it's not, you know, attached. All right, so there we go. So these are action buttons so if we click view, we can always go back to the list. I click view again. I can go straight to the edit If I go back to the list and notice are edit. Page doesn't have a button to go back to the list. It only has something to save changes we may not want to see. It changes all the time. Probably just clicked it to see if it worked. I don't know. I want to go back to the list so you can feel free to add that back to list button. So the edit record just the same. Andi, if we go back to view and I pressed the lead that is going to promise me and ask me if I'm sure if I click OK, then we would have removed Mary from over database. The last thing I want to look at is form validation. No, we didn't put much, if any validation in tow. Our form, the validation that would probably be there is based on the input types that we would have selected. So if I go to my index speech, which is my registration form page, I well, I did not enforce which values would be required. So I can actually using HTML five just right in. The attribute required on what this will do is actually force the browser to see that that field is required. So if I click submit, it will prompt me to say, please follow this field because it is required so I can go further. If it is not, Phil does. All right. So that's the easiest way to enforce some form of validation and informs. I'm just going to be the first name on the last name required Onda. Well, this one is kind of implied because of value is always going to be present instead of for select but at least an email address. So we need at least your first name. Your last name of specialty and any military you don't need your phone number on. Those are the required fields for this form. So if I had put required in all of these places Onda save and then refresh when I tried to submit, Okay, first name is required. I tried to submit again. No, it was on to second the last name on. Then I tried to submit again and email address is also required on I'm just going to go ahead and fill in this person's data. Andi, meet them a part of a system just to make sure that we didn't break anything in our cord to submit on. There it is. So operation has been completed on mere waters has been added. Now there is another level off form validation, which is services. What we're doing here is client side validation. But there is also service at validation where we can actually scrutinize the values a bit more. Andi, conduct all operations right there would have done what you call a self posting. Meaning instead, off posting toe a different page, we would have posted to the same page that the forms information is being submitted from on . Then we would have carried out all off operations in that same page. However, I won't go through that for this exercise, at least, right? No. You should be proficient enough to build a form on build out. All of the crowd capability is required for this form on. You should be able to well, do it end to end and incorporated with bootstrap. I hope you've had a fantastic journey open to this point. If you have any difficulties, feel free to let me know on that the next video we will look at adding all of for work. So I get hoper posit Torrey.
14. Upload PHP Forms Application to GitHub: Hey, guys, Welcome back. Let me start all this video by congratulating you for making it this far. By this time, you should have already completed your THB forms application as well as all of the crowd operations that are required for supporting it on in this video, we will be uploading all of for hard work to get up. So just to review, we would have built this entire application and we know want to put it into a remote repository. No. While I'm doing this at pretty much the end off all of for work, it is actually advisable that you do this as early as possible because as we were working on it before, machine crash would have only had one copy of it and that would have been on our machine. There's also no tangible way to track all of the changes that we've made the files on. Any changes that may be our colleagues or co workers would have also made so get being a source control mechanism is an excellent way to track all the changes and keep ah spear copy off your project at all times and especially with the availability off remote repositories such as Get tub. We can have multiple copies off our project as we work towards the end. Now, in this video, we're going to use ghetto for desktop so you can just find that on by No, I'm assuming that you would have already installed and configured that in Get up, Best up. I'm going to goto file on. I'm going to add local repository. Now I'm going to navigate to the project that we have So our project is in season Ishi docks are wherever you installed some but in HD docks is where you should have had your attendance projects or Justin to select that folder on. Then it's going to warn us that there is no repository in this folder. Would you like to create one? And we will just take that Created on dso We get this screen where we can just go ahead and click create. Once that operation is done, our get repository is created locally and then we'll be asked if we want to publish it. Toe, get up So you can just go ahead and click Publish repository. We maintain the name and I'm with the antique The part that keeps the school private because I wanted public. So everybody who has followed these lessons can have access to the court. And then we publish Repository. Now I go to get hope Web on drug in, and I will see my repository listed right here for access so I can just click it on. Having done that, I will see all off my files nicely put down on this remote storage space while we're here, though, I'll just use the time to add a quick description to the project so that when you come across that you know exactly what it is. All right? So we just added our I decided a description real quickly. Andi, I think that's it. That's it for adding or project to get up to. Remember that from here we can track all changes. So I didn't make a change, and the change I didn't make was to add the button for a buck to list toe our edit pitch. So I'm going to meet that change, right? No, on what I'm going to do is duplicate this button on with the TECO Bt and block because I don't want it to stretch right across But then this one I'm going to have back to list to the left on going to the save changes to the right. And this one is going to be BT and default. Onda. Well, well, actually this should all be an uncle Tug. So there he just changes quickly. All right, so change the button. Completely toe on Ankara tug On day the truth will be back toe the listing speed riches, viewer cars or PHP on. I'm using the class beating and Bt and default. So it retains the look off a button. Onda, What I'm going to do is save all of this on then refresh the page and that's what it's going to look like. Someone toe have my edit page on then my link to go back on day the other one to see if changes are so. I just made this change. What's going to happen? No is that it is tracking these changes on once again. This is the benefit off using ghetto. It is tracking these changes. So when I bring up, get up. This up is showing me what was changed and what it was changed to Andi. I can commit this the master Master being that final branch that you know it's production ready. Andi, I can no push the origin. No, I'm going to have a different tutorial on how to manage gets using branches on promoting court. But in this situation, since we're the only ones building this, we can always push the master. You know, that's not the best idea at all times on DSO having done that, commit on push toe origin are pushed to get up. If I go back to my repository and I refresh then looking at the edit page will show that I just didn't edit 32 seconds ago. All right, so once again this is a great collaborative to where everybody can see what changes are being made along the way. Having done all of this, the next step is to publish our websites toe Hiroko So stick our own as this chapter is coming to a close
15. Create Heroku App and Remote Database: Hey, guys, In this video, we're going to be looking at deploying our registration application on Hiroko and also setting up remote my SQL database. Before I go any further, I think it's worth stating that my machine actually crashed. So the machine that was being used for all of the tea tourism pointed at this point actually died sometime this week on. So I had to recover another machine and also all of the court that was being done in the previous tutorials. Onda, thankfully get up, rescued me. So I was able to go on, get up, flew in the database. Sorry, the files back to my machine on provisional new machine with all of the court and all of the tools that were being used. And so this is the new machine that were on. So if there was never a compelling reason for you to use source control than let that guide the advantages to using it now in this video, we're going to be on heroic. Also, if you don't have a ah heh Rogo account, you can always check my earlier video setting up a free heroic or cones and then that should get you up to speed with getting a heroic account, and then you can follow the rest of the steps as we go along. For those of you who already have a heroic or cones, this would be a return trip to your Haruko. And you would notice here that there some zed signs, which indicates sleeping beside my previously reported deployed ups and you probably have them also. So the thing is, when an up because they're under free plan and up tends to go to sleep, Perico puts them to sleep because where they're free, Andi, they weren't necessarily in use. So if I attempts toe navigate to my PHP, that's trying Primer Dash Trev up. It's going to take a while to come up on. That's because it was asleep. So what had to happen is the dino itself had toe reawaken. And when I refreshed the PG notice that it's no a week, all right, so that's going to happen if you realize that you're trying to never get to your up and it takes a while, it's because because there's limited to no traffic coming to that application off the Dino's get kind of deep revision until they're requested on. Then they would awaken and serve the content. All right, So I'm just letting you guys know that that is why you would see those visits anyhow. Or mission today is to deploy our new applications. I go to new create a new up on, then we're calling this one attendance cash register. Remember, you don't have to have the kind of the same name as being here is an example. Already have attendance that's register as an up on Hiroko. So they're telling me I can't do this one. Someone to call this one. PHP dash attendance. I bet nobody has that name out there. There we go. So you can always copy this name and just put something else in there to make it unique to your situation on. Then once you're done with that, you can go ahead and click. Create up. No. With our up created, we can go ahead and scroll down to deployment method where we can just click connect to get up. I'm already logged in to get up in my brother. So it's knows it's me. If not, then you might need to put in your get tub credentials. And then once that is done and you're connected, you can just go ahead and search for the report. And I believe that my report was called Tendons are search on. There we go and then I connect and then once I connect, I know have the option to automatically deploy or manually deploy someone to choose manual deployment. Andi, I think I explained the reasons for that. In a previous video on DSO, I go ahead and click Deploy Branch and then give it a few moments while it runs its magic on. Once that is done, we will see something coming up deployed to Hiroko with a green tick and then I can go ahead and click view, which were then load my page. But then look at this. If you do or call our index speed should have a form Andi or View attendee should have a table off. All the persons would have filled out that farm. Neither is loading on. The reason for that is if you go back to our chord in visual studio. If you look at our index page, we see that it is calling Khan. And if we look inside of far can file Khan is actually trying to connect to our local host database on our machine. Well, this I p really means local toe water, A machine it's on. So that means if I put it on your machine and you I didn't have this database than it would have been an error. So likewise, there is no database, local toe Hiroko that hoses any off the connection details being aligned here. So all we need to do is set up a database that heroic who can see across the Internet and can quell these problems so heroic Oh, actually allows you to add a database. Ah, it'll be a server off sorts. You can go to configure Adan's on Look for clear db my SQL The don't sides of clear db my SQL is that even though it say's free here, you have to provide your credit card information on then actually, provisioning the database isn't really free. The database hosting itself is not free, so we're not going to take that route. But instead we're going to look for an alternative. Andi, I actually found a website recently on it is re morte, my SQL dot com on day actually offer free my ask your postings. Ofcourse, free comes with limitations. But for the purpose of getting through this video and showing you how you deploy a fully functional, data driven application to the Internet and specifically Hiroko, then we will use it if we're doing it. To see Pinelli would be a much easier ordeal. But because off Hiroko and the cost constraints, I just want to keep this video as cost free and effective as possible off course. If you're going to production, then you could proceed with clearly be keep everything more homogeneous on more stable by just having everything inside. Heroic cool. So in remote my school dot com, we can go ahead and click Log in. Which off course will ask you to create an account or I'll again say can go ahead and fellow this basic later and create an account. Another tool you're probably going toe want is my SQL were pinch, which is it's a desktop application that allows it to manage remote my SQL databases and the relevance off my using the word remote is that peach FEMA admin manages the local databases that are local to your zump installation. When you have workbench, he can connect to that one. Or it can connect one across the room or across the Internet on considering that we're actually going to be setting up this room or to my SQL that which is across the Internet and we need a tool that can actually connect across the Internet. And so my SQL weren't Binge is a wonderful tool for that. So you can go ahead and go full of this girl, which is in the resource is for this video. And you can just navigate their and click download and have that installed on your machine . No, I've been created our remote, my SQL database account. What we want to do is go over to databases in the stash sport view on then. Well, we can't just let me get through all these advertisements and go ahead and click. Create new database. They go, dear. They would then show some information showing us boats are created. Database the user name the database user name the database name. Sorry. The password. The server on the port. So with that information, we can go over to our workbench, and I'm sure you've installed it by the time you get here and then we can click this possible. But now I have two cards here because I had work pins installed before. But if you didn't, then you probably end up with one card and that's fine. But then we want to create another one. So we went to go to the plus sign on. Then we can call this one remote SQL. So that's just a connection name so that when we see it, we know what it is for. On then the host name would be what they call the server. So you see remote my SQL love calm, which we can just copy from the website and paste inside off workbench. Off course, we don't want any species to come up, so we can just remove any space is at a space before my are on. Then we go back and the user name is this block off text someone to copy that. Go over on going to pay us that in the user name on then our password is that block of text . So I'm just going to go ahead and repeat that procedure. But for pastor graft is a store involved. Give it the password. Click OK on, then the final. Actually the database name, Which is that name we copy that is it defaults, Kira, we can test the connection. And having done that when we see this messaging successfully made my school connection that we know that we're actually connecting toe remorse. My SQL Not because it's called remorse, my SQL, but remote means it's not on your machine. It's not local to you. So it's on the internet and we're connecting to it. Successful star click OK, and then click. Okey on then I see my remote s your connection here can double ticket on. Then this will grant me access to their servers. No one you're connected. Then you'll see the database that left and you see an empty square window on. Then you may see this Mitchell message tables could not be fetched. I'm not entirely sure why, but if you re first you see that it's remains connected and they try to fit on. The state can be fish. But that's fine. That's off little consequence. What we're about to do, you know what we want to do is migrate our database. So we had our database in our development machine. I know. We need to put it over into the production machine. So what we need to do is find our database and we know that we built it using my sorry PHP my admin. So we go ahead and go to local speech. FEMA admin on. Then you will see a 10. Ddb, remember? Had to rebuild my database so I won't have the data that you guys have. So when I migrate, man, it's still going to be an empty database. You guys are going to have your data, but it's to see him set off steps. Next we after clicking the DB we goto export. This was then launch of it a window asking us a bunch of stuff about templates. We don't have to go through all of that. We can just go ahead and click former. So under the four months you see sq A so we can leave that because I want to do is export our tables and their contents on sq a file so I can go ahead and click goal, and then that will actually give me on sq a file which I can open up in any editor. So I went to my downloads folder by default. I'm just going to right click it and open it up in no part prosperous because I don't want to open it up in any heavy software. So once it's in no part cluster second, just copy its select all control. See to copy on. Then I go in tow workbench. And in this antiquity window, I pieced on. Then once I've pasted, I can actually no just click the execute, which is this lightning bolts looking icon here in the toolbar on. Then this will run a set off commands. So, like I said, I have no data in mind. So I Your your query, your query file that is generated is going to look a bit differently from mine. But essentially, the net effect should be the same. And then once that has run and you only see green ticks below in that that what I have a few pero files here airlines here. But that's fine. Those are related to the fact that the tables could not be finished. But once I refresh the schema or to this right and left inside. Then I see my two tables lifts that. So even though it says tables could not be finished, I still get my tables. And if you had contents than you would have gotten contents. So that's step one. Know that we have replicated or data beast or more to my SQL server. What we want to do is go back to our visual studio cord. Andi, open up. Our con dot PHP file on actually changed all these values relative to this new database. So what I'm going to do is take a copy off all of this. Since that has all of the connection data, I'm going to comment out the development parts and I'm going to put the noticed in development connection on. Then I'm going to put in another comment down here and see remote it abi's connection. All right, so that means whenever I'm ready to work on my local machine, I can just un comment this and come in this part on vice versus in this situation because we want toe work remotely, want to deploy remotely. I'm going toe change out these values so host would be and we can just go back to remote. My Eskil dot com on reuse these values. So server is the same as host, so it's remote. My ask UAL not this I p address anymore, but remote. My school dot com database would be this block off text no longer attendance TB. The user name would be that block off text andare pastors. You know what? We have a password this time. First, we didn't have a password, and I said that I would be fine for development, But for security purposes, they always give us a password when we're going remote. Now, another cool thing about this is that no, that I set up our Moz database on. I have it here on my local development. You know, in my local government file, I can actually collaborate even easy more easily with my colleagues because the database is no longer isolated to my machine. So I did see the remote database would be on a machine that is not your computer. Because if all of your working on this project, then it will be hired for everybody to be able to pitch in for database changes. If the database is only on your computer However, if it is at a remote or a central place that everybody has equal opportunity to connect to , then it's easier for everybody toe work with the database. So by deploying your database toe some remote server, whether it's across the Internet or maybe it's on a network where you are at your school or your work, please, if it's on a network machine there. The fact is that everybody can use these connection details and have visibility on this same database, the same data that everybody's seeing on Dwork even more seamlessly. Now, just to drive for my point, I'm actually going to navigate my local host applications. I'm just going to go over to my brother, go to Loca host on Goto. It is attendance. And then when this index fire Lord's properly realised that the connections are working, everything is in order. You notice I don't have any air of expertise because my data bases empty. All right, But remember that we had set up this drop down list to actually read from the database, so I need to go back into my database and refill this table with the different areas off expertise that are available for selection on then would see them. But the point is nowhere connecting to our database. If I view attendees, there's nothing there. But that's because once again, my data bases empty. If you did this and you will never you migrated all of your data and all of the objects, then you're going to be having an entirely different experience from me on Do you will be seeing all off your attendees, so I'm quickly going to quickly just put in all of the specialties. So where bench allows me to just click on the table on day? You know, this icon that launches a select query from the database on, Then they give me an editor. So I'm going to put in Web administrator on the database administrator on I. I'm sorry. I'm not remembering the other ones, So I'm just going to put networking engineer Andi. I believe the other one was other. I know that I've entered all of those values. I can just go ahead and click. Apply to the lower right corner on. That would actually generate an insert into statement per value, which I can click apply on that gets executed on all of these are no in my specialty stable . So when I go back to my form and I refresh and it connects to this database, then you'll see that all of what I just put in, including the new one networking engineer all off them are no being pulled. So it is successfully connecting from my local host toe the remote server that we just set up. And so the expectation is that once I deploy this to Hiroko, he will be looking at the same data beast that I just set up in my local development. So we're going to go ahead and redeploy someone to go to get hub on. I'm going to push the changes and hear all the changes get Toby showing Arsala machines that we just made in our con file. So I'm going to come into master, and I'm going to see added remote my SQL connection details on. Then I'm going to commit that to master on, then push. And once that push has been deemed successful, I can go back to my up in Hiroko Goto overview. No, Sorry. Deploy my bad on, then go ahead and deploy branch once that branches deployed successfully. Then I will click view on. The expectation is that our farm lords Onda we can see that a query is being done successfully and I'm going to put in the first attendee. So this is my actual up here on the Internet. So this would have been Reno presented to the user for the first registrants. So I'm going to register first for my I T conference on my deeds off birth is actually the 28th but then the year is wrong. Well, we leave the year on then I am a Web administrator, more or less. My email address is indeed that on my contact number is not fully accurate. But we work with that, and then I click Summit on. Then we're seeing that the operation has been completed and those are my details. If I go to view attendees and we remember that we did a select Scurry to get back these details so it's reading directly from idea to based. And if I go back to work, Ben's just to verify if I look in their 10 de table on die, click this icon here that will launch all of data in there on the slate quarry. Then I see that my application is really talking to my remote database Somewhere in the Internet on that is all you would go about deploying an application to our more database and to Hiroko No. Once again, it is ideal in ah real production situation that we keep everything inside off. Hiroko. It's easier to maintain is easy to manage on. It's easier toe. Well, everything disconnects better. But then we see here that we're hosting in Herro co hosting our coding heroic about hosting our database on remote. My SQL dot com on this concept could have been applied basically with any remote hosting agents, So everything doesn't have to be one place. The database can be one place and the court another place, but we know how to connect them. Once we have the cart, connection details are so I I'm challenging you. Just repeat these steps, deploy our application on If you run into any difficulty along the way, feel free to let me know, and I'll be glad to help. Thanks for watching
16. Setup PHP Authentication Database Table and Logic: you guys in this video, we're going to start modifying our applications such that unauthorized users won't be able to see certain parts off for website. Now, the scenario is that this registration form for conferences, public facing everybody should be able to come on a register on move along. However, nobody should be able to view attendees and meet modification store databases at will. So what we want to do is kind off put in mechanisms such that you have to get authenticated to make sure that you're authorized to see the speech. Andi, do any further modifications to the data now back in our code? Um, we'll just start our modifications with our con files. So I'm going to switch over to my development database, um, distant comment about the remote database connections and on comments, the local database connections that I'm creating a new file inside of my DB folder on I'm calling its user dot PHP and then use it up each. Be no has on start up with a Ph. B block on. It has a class called user. So class user on, then we're just going to go over to crow dot PHP and borrow the first bit off court, which is where it initially initialized a private variable called DB and then use the constructor to give it the values. So this is generic. This needs to be done regardless so that we can keep the same object or into dynamic across all our files on. Did I just borrowed that? Put it there on the next mission will be to implement some functions. So the first function I'm going to put in is public function. Insert user. All right, so inserts user. Well, that's function one. I haven't defined the user table yet, so I'm not going to proceed with that just yet. But I want another phone shown. That's well, maybe get user. Alright, some just swear framing of this class for no on. We'll just leave it at that. So I don't want to insert use. I want to get user at some point, but what we also need to do is actually define or user table. So I'm going over to PHP my at mean taking my attendance db and creating beauty with quality users. Andi, I only want three columns you can put in more if you want what my three columns are really going to be i d which is going to be my auto implementing primary key. Andi, I need using him on the password, and these two will be var char and I'll give them a size off 50. All right, Once I've defined those that can just go ahead and click save create my my new table on then No, I know I need using human passwords. So whatever columns you put in, I'm sure you put in more, but at the very least, you should have using him and password. Or maybe use email address on password, but you need at least these two have ah, sensible user till our so know that I need I know what columns I need. I can better define my functions. So I'm going back over to my phone, shines and insert a user. I can expect that I'm going to get the user name on the password off the user to be inserted. So I just put those in use any imposter word. Those are my two parameters on, then to into an insert. I can go over to crowd. Now we can borrow the same logic that was used to insert our attendees understand to take this entire block off court on. Then we're going to make some slight adjustments relative toe our new context. First change I'm going to make is to the court so we can retain the verbal name SQL. But then the quarry should be inserted into users, since that's our new table name and inside off or user class. So inserting to users on the columns that were inserting into our user name on the password and remember ideas also implementing so we don't need to provide a value for i. D. I don't have all of these. Maybe you do, so you can tweak it accordingly. But I don't so remove them. All right on then, values on the values are the parameters that go into the values are using human passwords. I'm writin the colon and putting back the same using human password on. Then I'm going to remove all off the extra parameters to be boned on. Just make sure that I retrofit all of this school to be used. The name on the password. All right, so that's so you can reuse Cool that you've already we already did quite a bit off, quoting instead of crowd, I know we need similar court instead of users unders. We're using it, Andi, Essentially, what we're doing is the same thing. We're just inserting a user so this could be used when somebody's registering for your website the same way that we accepted all of this data from somebody. Sorry, insert into attendee were accepted all of these theater points, and already it was inserted into the table. That's essentially the same thing you're doing with the user. So any time you have a database interaction where they put something in the database, let this be a general template as the whole it can be accomplished. Next up, we have get user, and this first gets user. I want to get the user with a using him on password that much, whatever is being passed into our parameters. So I want the user name and password to be passed into the apartment is, and then I want to check if there's a user with that somewhere to go about two credit and see if I have any a similar function to this and I do in the form of get attendee details granted, it was on the i D. But the concept for me is the same. Someone to copy this cord on the east. It's here and then we just modify. So we say Select star from on going to kind of clean out everything after the table said I wouldn't have a confusion so it selects star from or users table were on then The first part of the wear condition is the user name should be equal toe the parameter user name. And so we want this use or whatever user it finds most have. I use the name that much is whatever is coming into the parameter Onda password matching whatever's coming into the past the department or so password column as a value that much is the parameter password on then I already have the prepare command. I have the buying power Rambo to spending for I D. So I don't want to buy and for idea I wanted to bind for user name on the value should be looking for is using you from the parameter on also for password on the values looking for is from password on. Then we executes then we fetch the results. Meaning if a user is phone we fetched that user on. Then we store it in results and will return the details about that user toe. Whatever was calling that function. Now, having don't know that I'm going to introduce one more function and this one is going to be get user by user name. So I'm going to on the knees. Get user Just introduce a new function. Public function gets user by using here, and I'm passing in the using in value Oh, our parameter on. Then, inside this function, I'm going to kind of do the same. Try catch on. This time I'm just fetching for user names. I'm going toe just we write this scored quickly on then what we're doing in this one is where selecting the number off users where the use any much is whatever is passed in the parameter. So let me say that again, or select Parisienne Select won't star and I mean you could use a column name there, but we just say star cones has no meaning. Return A column called gnome, with our value off the cones from the user stable, were there is a user name. That much is whatever use the name is coming in through our parameter. And then, of course, everything else kind of looks just like that. Get user function territory executes, and then we fetch the results on store it. Return it, art. So the purpose of this one, this dysfunction, is to make sure that we don't enter tool users of the same user name. All right, so this is really it could return a true or false. What? I'm just going to leave it as is on. I'm going to kind of modify this this this insert user function toe always call this function first on, then, based on what this function returns taken action. So if he returns anything other than zero meaning zero would mean that there is no user by this user name. Then it should go ahead and insert if it returned something other than zero. Meaning there is a user by that using here, then we just don't do anything. All right. Someone to retrofit dysfunction on. What I'm going to do is introduce a variable, and I'm going to call it results on. It's going to be equal toe this so because we're in the class after user, this keyword so that we can access the sibling functions on When I say this arrow, I'm going to call, get user by user New. All right, So my intelligence self made a little bit there on then inside off this parameter and passing in the same user name that the requests just came in to create the user record for All right, So what's went up in his once I call insert user passing and using him on password. It's no going to meet this phone Sean Call, which is supposed to check the database Azzawi designed on here to see if there's any user with that user name and return home. Many. So then what we need to do is see if our results and result is what storing that won't right, You've result. Onda results would represent the whole roll, which has ah, column called gnome. All right, remember, we said we wanted to return some results set with a column called Gnome Worth of value Off phoned. So if if the value inside off results square bracket gnome is greater than zero, which means that there is a user by that user knew. Then what we can do is this return falls. All right, so we just returned falls if that user exists, And essentially we don't want to do anything without falls, because ofall serious, but ah, false here is bad. So we just returned falls on. Then we just see else we want to proceed with all of four beautiful, wonderful court that is actually creating our users. Just fix the farm. Utting indentation there. All right on then, at the end off all of that. One second. What's going to happen is that we call in certain user passing to user name and password. It's going to first meet that check to see. Okay, give me back home and users have this user name. If there is at least one, then we don't want to do anything. Otherwise, we want to proceed with the user creation. Now, one more modification I want to meet to this. Dysfunction is the way it handles the password. So the pastoral as it stands, would be whatever pillion takes the user types into the form when they're logging and so sure it's obscured from over eyes because you know that in HTML that's come up or some simple scallop instead off words, but then wanted passes over here. It's playing text. It's really just, you know, as you typed it when we start in the database, we don't want anybody to be able to look into the database and just see Oh, that's what the pastor it is because we didn't obscure it. So we want to introduce over call encryption, which will obscure the way the password looks in the database and make it fire more difficult for somebody to sit down and figure out what to your password is. So I'm going to above the else before I even get into the SQL statement, I want to define a new verbal on. I'm going to call its new on a square password on the new password will actually be like a concatenation off password on what we call assault on. And that's salt, as in salt. All right, so assault is like a random block off text that we used to further up obscured what the password might be. So sometimes people define a static salt, and then they use the static salt toe upend our pre penned every password. So every time somebody typed in the password, it's used as a qualifying agents. Um, I want to be a bit more dynamic someone up in the user name. So this password text are so by using the user name toe up end to the password, it's far more dynamic. There is no static. Salt is somewhat off at the salt, which is used. The name changes every time it's a different user on. Then, even if they use the same password to password should never look alike in the database because the salt would have been different. Because we're you know, every user name, at the very least should be different. So having upended them on seeing new password, the next thing I'm going to do is actually encrypted, because right now it's still playing English. It's just two words, so I'm just going to say MD five, and I'm wrapping this concatenation text inside off the parent disease on this is an encryption function, so it's probably the most prop popular. PH be built in encryption function there, quite a few. This is very popular on It is probably the least secure, since it's so popular, But off course you try your best to me. Whatever it is you're encrypting as dynamic and foolproof us possible. So they're moving forward. It's harder to crack. So let's go through this again. I'm defining a new variable called new password Andi, I am hushing are combined text off the password on the user name some combining using him and password in tow. One continuous string on. Then I'm using MD five toe hash it on. Then I'm storing all of that inside off new password, which then means instead of passing password as the Param, I'm going to pass new underscore password. So this is what will get stored in our database. And this is what needs to be often today authenticated against each time going forward. So then, of this exercise, I'm feeling pretty confident that we can insert a user. We check and make sure that this user name doesn't exist before Andi We, if it does, does not exist. Then we can go ahead and create a hash password and insert this new user with its brand new secure password. Now have a few more changes I'm going to make and I'm going to meet them to the conduct PHP file where I'm actually going to Winston. She it first requires understand, require once users dot PHP. So I'm just requiring that new DB file Andi in that same Vietnam went to declare a new object off called user off type user. And it's getting the C M PDO object that curdles getting, which is why we re used the constructor bit off cord from crowd because we expected to be here the same way that crowd behaved. All right, no conduct PHP is included on every page, which means that if we need to access something for the user, we can access it. If we need something from crowd, we can access it and every other objects that are class that we build that should interact with the database will also could also be included right here in the conduct PHP and utilized according to their different patterns and different ways. Often in this, this is just one of the paths off least resistance so that we can understand the foundation off it. Now the last teams are going to make is I'm going to call the function insert user, which is inside off my user objects. Right? So we just declared the user objects on same time. I'm going to put it to use, and I'm going to call the insert user, which takes a user name on I'm putting this as admin onda password off. Well, password. So it's a very simple, um user. So our admin users admin imposter know the reason putting it right here inside the con file is that the confines gets loaded on every peach. When we go to the index speech and the Khan forgets loaded and it loads all of this, it's no good to insert the admin user insert. Users want to call the phone, son, get user by using him to make sure that there is no other user by that user name on then, Well, it's going to be our first user, so there will be no other user by the user name admin, so this will get executed. You will go to another page and it reloads and it recalls dysfunction. What will happen the second time when it passes in admin as user name on. Then this reviews that there is. I use a by that name. It will just return falls and do nothing. So this user should get created at least once. Well, should be there once. Andi, if he gets deleted once the website is Lord and he will get recreated so you could do it one time and then commented Oh, sorry. Could leave it because we already quoted in a defensive manner. So this user should never appear twice in our database. All right, so if I make all these changes and save and then go ahead and re lord or form man see whole our changes fear, it's understand to refresh this page. And as you can see, everything loaded, nothing seems to have happened. But if I go back to pitch me, my admin Andi, reload my user stable. Then you'll see that we have a new user called admin on. If you look at that pastor, the looks, nothing like the word password. All right, so that's our hushed password. And if I never get to another page on, come back to homes, remember, each time we never gets loading Khan and it's making that function call. If I go back to my user stable, I'm still only seeing one instance off admin. If I delete admin. Andi, I just removed. That means so there is no user by the name at me, and I go to another page on day I go and refresh. Add. Minnery appears with a new I D. Yes, but the seam add mean using name and password. All right, so that exercise was just to show you how you could go about putting in at least one user so that when we start locking down our pages, we actually have a user to use toe log in.
17. Setup User Login and Authentication in PHP: Hey, guys. In our previous video we created or user stable, we created some mechanisms that are close to at least create the user on doll on, find a user with a matching using human passwords. So what we need to do know is actually start creating our log in forms so that we can create. I use their session Onda a low authentication across other pages on actually start imposing restrictions on or Peaches. Now step one is going to have me create a brand new file, and I'm going to explain what the purpose off this fire is. So I'm going to create a new file inside of my includes folders. I click includes creating file on. I'm calling this one session dot PHP No, in order for us to handle. Ah, let's see toe pervious user information across our website. Once you have love being, we have to use over call a session or cookie or their certain super variables that are there. So a cookie stored on the person's machine a session is stored in the server for under server. So we're using sessions. Andi In order to get the use off sessions in Peachtree, we have to run a directive on Doyle. It's really a function. And that function is session. Underscore. Start. All right, so the reason I put it in its own file just for this one line is that we can include this file everywhere. So I'm going to go ahead and include this fire this file on our header dot PHP, Pete. So instead, off includes that us. Fine. Ended up PHP on the first thing that should be done on the header dot PHP is include our session dot PHP file. All right, so I did that and I included some amount off some some comments, so you know exactly what's happening there. All right, so we have a recession that pH be included on our hitter file on once the header is included somewhere session dot PHP is implied. Onda, we can utilize over session variables across our website. Next, I'm going to make some modifications toe to this. Um, first, to fix that spelling error. Andi, I'm going to make some modifications to this love bar where I want section toe the right. Another spring it up. So over here, we want a button that's is logging on if the person has loved being, then we want to change it to, like, a welcome message and the Lago. But so I'm going to me that modification here. So you know what I accomplish that we need to modify how the lives look. Admittedly, I didn't find this very easily on the bootstrap documentation. Somewhere is going to do it here on. If you want to go on double, check it and you find it. Then please show me the page. But I'm going to do it. So I know it's done in bootstrap for after much rigor and experimentation. So I'm going to modify this first day of class with nav bar enough. And I'm went at another classes says Empire Dash auto. So this is my way of saying I want you to always be on the left hand side on. Then I'm just into copy and peace because I need a similar construct. But then this one is going to say M l dash auto aren't on. Then if I just reload this page very quickly, we're not done. Number three loading, So you can see. Then we see here that we have are two original links. These are the m r audible than we know have to the right. Inside the same tooling is because, well, we copied on piece of them. So we know we have a section to the right, and we have a section to the left on. No, we can dictate what happens on the right hand side of four PHR. Someone to remove Well, instead of removing, understands, modify. So I have the link that says home. But this one I'm going to make it see lugging all right? And then I went to make it. Never get to log in that PHP. So if somebody is not love being then they should see the button. So the rights that is Le Guin on. Then I can refresh on. Then we will see the log in button and if we try to navigate or getting an air because that does not exist. So we need to go and create our log in button. So I create a new file at the root on this one is going to be log in that P H p no for long . In that ph. B, it's going to kind of share quite a few things with the other pages. Understand? To go to maybe edit on. I'm going to copy the first part of the PHP block place. Over here. We change the title on this is logging page or will user speeds Logan on? We're including the same files on I don't need any of thes query sergeants. Remove those real quickly. All right, so that's the top off our log in page. No, I'm going toe expedites this video and I'm going to just ah, go ahead and create the form. And then we come back and we dissected together. All right, so I went ahead and created this form pretty quickly. And I'm going to walk you through some off the differences between how this form will behave and hope our attendance registration form will behave. So, firstly, I'm echoing the title because, you know, I'm just going on each one tug or the each one time would take Centre has the echo statement in it for the title. And so over. Titles being printed at the top user log in. We also see it here in our title bar. Next we have Oh, are sorry about that. We have our form with an action that looks entirely different from what we have seen upon to this point, but or method is still equal to pull, so we'll get back to the action in this form. I use the table to help with my layout. So I'm using one TD for the user name or for the label and one for the the text box. Onda. The value here has quite a bit of text, which I'll get back to on. Then I'm putting in some some share a message with the army and I use that. Let me take it all from no for no to avoid any confusion. I kind of got excited when I did all of that. I'm sorry, but I'll put it back and we can go through together on then. I'm using another role for the labels on the controls for the password, and then we have or submit Button, which is beating primary beaten block under a little forgot password link, and this is what it renders. All right, so that is what all of that court renders. And this is our new lugging form. No, What we need to do is where it up so that when the person clicks lugging, it moves to appeal. Those verification on then takes an action. I don't know if you've ever noticed, but when you click when you're using a log, inform you cannot behaves differently from most other forms with a little forms. Move on to another. PH, like we did with our registration on success speech aren't in terms off the log. Inform it. Kind off was sit on the CMP essentially the same information you just tried to enter until you get it right. So that is the reason for this particularly unique looking action code, and I would explain it. Firstly, we have a super verbal called server, which allows us to say PHP self meaning that I want this page. The action should be this same page, aren't so inside off action. If we go back to our index page, we had success that PHP, which means move I had on go to the speech. I want in on the circulation, regardless off what this speech is called. In this case, it's PHP. It's logging that PHP, but which the server PHP self will just say. Reload this page and do the posting action on this speech, which means that we have quite a bit off quarter rise on that is actually very possible because we can actually reload the same page on do other verifications that we did on the success that PHP. We can do it on the CMP to the form and then taken action afterward. So that's what we'll be doing with Log in On because it will be reloading itself. That is why I have this this block of text inside off the Value section for using him. We're seeing if the request Methodists post, meaning that if the pages loading because off a post action, as opposed to meet us clicking the link, then I want to reprint whatever using name was entered. So that's why when you click submit, you may see the password section blank haute notice. Pastor doesn't have that same code, so the pastor sexual Blanco, but you use anymore email address or whatever it is is still in the textbooks waiting for you to enter the card password. So that's what we're trying to accomplish here with this score. Um, I just realized I didn't finish explaining the PHP self. So where were posting back? Toe the page with the pH? Be self. But then you notice that this entire super verb is wrapped up inside off this function. HTML entities on what HTML entities does this kind of strip the text that is going to be produced by this it strips it off any special characters and potentially militias characters that would alot Attackers still kind off inject whatever malicious school they could want. So this bites of has the pH piece of the server pH piece of has some vulnerabilities that can be exploited. Andi ph. B. Has given us this function to kind off reduce the possibility off that exploitation. All right, so know that we have a better understanding off all of the school. You can always just replay this and you can posit and go ahead and rewrite your form. So there you write it just like me, or you have your own way off writing it at the key parts that I want our that you get the action section rate and whether the value section is actually optional. We'll make sure you get the action. Six generators, what we're about to do this sparks is very vital. All right, so our next task is toe check. If the data was submitted by, ah form post requests. So, like I said, we could navigate to the to the page just by clicking log in. Or if I click the submit button, it will also reload. Say, noses. I click. It is reloading the page. That's because of the PHP self. So I need to check if it was a long in button that was pressed or if I just navigated there . So this is another way off checking that we did it in the index page where we checked. He set or did sorry in the success speech, Right? Sorry about that. So in the success page with checked is set post submit. So that's one way of green. Another way of doing it is using or server super variable, some, actually just in the bar of this entire if statement, as it does a very similar thing. Except it doesn't rely on the presence off that that that value. So this one relied on the father beers are post variable called submits coming over. If there was no submit than this would have failed. However, this is far more generic. Where it's seeing is there opposed? I don't care about what variables may or may not be. There was their request method called Post, and we already told it that the method is equal to post, which means once you click, submit it will trigger a post. Andi, it is going toe. The action is itself. So no, we're checking. Is it being loaded because the submit button was pressed on then if it was, that means somebody was trying to log in, which means no, we need to take some amount of action. So the first action I want to do is get the user name on day. I'm going to use to so string manipulation functions there, some using str to lower and true. So what I'm doing is removing all white spaces from the user name that is coming through the post on. Then I'm making everything lower key so that the chances off the user name matching in the database are much higher when you kind off, you know, manipulated and massaged. So it looks as as clean as whole. You'd have stories in a bit of Eastern on, then that's being stored inside off using him on. Then we take the password, which we want to take exactly the way it was typed. So I'm not going to try toe fix the password in any way, shape or form, because this person should have type the password. The exact we that it was entered into the database the first time. Then I'm going to build a new password. All right, so I'm going to introduce any variable. I'm calling it new password, and we would This looks very familiar. We're hashing the password on use any combination like we did inside off our user dot PHP on. The reason for this is that if we're about to check the data business, see if the posture discourage and we can't be checking the database against the password that the person would have entered in the log inform our So if I goto the log in former no , I typing, admit I'm literally going to write in the word password because that's what I set my pastor does. However, we know in the database that the text password is not what is stored there. That is what the story there. So whenever we're going to check after I click, log in and I'm going to check the database. I need to make sure I'm checking the hush off. Whatever was entered against the hush off, whatever is stored, you don't want to do it, Universe, you don't want to try and the are on hash. We switch over, discard. You don't want to decrypt the password, and then try and check it against the plain text. You want a hash? No plain text on. Then check it against what was stored. The two will. The two hashes should be equal. And only then you will know that this user exists. So having our new password, the next thing is that we want to call our user objects. All right. So, user arrow on, then the function I want to call is get user. All right, so let me just make sure I'm making the right function call. So it's get user with the user name and password. So, in Logan, I'm calling. Get user with using name and password on. It wouldn't be password, but it would be new password. All right. So as the function doesn't look here If it's getting the plain text or it's getting gibberish, it just wants to know what he got. Two parameters. One for the user name and one for the password on. Then it will go ahead and do its verifications where it checks to see if there's a using him with that user name and password with that password, which would be the hushed password that were passing in at this point. All right, so off course it's returning a result. So we need toe. Same result is equal to, um there is whatever comes back after this function car on. Then after we meet that function call, we need to see if I understand to see if not results. So went through this already. We can district the negative first. So if not results meaning it's going to return was going to return falls if nothing and results. So I can I can probably modify this up it. So if not results, then we want toe echo. If we can error. So understand, to put eco developer danger use anymore. Password is in carats. Please try again on. Then I'm going to see else. Andi, my else is going to have me set off a session for the user. So the session is already created. Way started this session when we said start session. Know what we need to do is set up variables inside of decision. So our session actually gives us some super verbals that allow us to set values so I can super variable session. All right. I noticed that all the superheroes have that dollar sign on the score on then our caps just on the server decide the post and get etcetera. But Sessional owes me to dictate what subscript I want someone to say. User name, session using name is going to be equal toe the value off whatever user name was just authenticated. All right, so this is a part off the authentication. So it's the using him that was very fired. Rather So it's verified that it's in the database on the card pastor does provided so we can create a session with that user names. So this session variable will Alois the store to use any in so that we don't have to go back to the database anytime we want to use the name. But we can just reference decision wherever we want in our court. All right, so we create one for the user name on. I'm just going to create another one. Sorry. On when to create another one for the i d so session I d is going to be equal to on. We just got back results, so I can actually just say results on Look for the subscript i d and store that inside of that. So once again, our session variables are like a temporary holding era for anything that we want on. We can let them persists throughout users sessions. All right on. Then, after we verified the user and we set up the session, the next thing we want to do is reall redirect our user from the log in page on someone to see Loki Shun Colon and then my destination. Let's just save you records dot PHP are. So once this user is verified on decision is created, then we can redirect them. So the view records that PHP. All right, So know that we've done all of this work. Let's see if our cold, you know, it's test our theory as we always do. So we have the log in page. And the expectation is that when I stop in admin Onda password then click logging that we should be verified against the database on, then redirected toe our view records that 50. So when I do that and click log in, we were redirected. So it happens that lining fast. You didn't even know that all of the core that we just wrote actually came into play. So what happened is when I click Duggan, he reloaded itself. The page reloaded itself on then at the top of the page or the sea. Is it opposed? Yes, it is. Our ads Get me the user. Name the password. Hash the password Check if there is a user on. If there is, then we when we do all this fancy nous So on the flip side, I'm going toe. Go back to logging on. I'm going to put in a user that doesn't exist. Alright on then. When I click log in, we see that the page loads. We get this alert. Seeing used a name or password is incorrect because we put in that statement that if not results meaning we didn't get back anything positive from this call, then we just want on alert that is using a more possible using parts. Generally speaking, you don't want to tell them which one is encouraged because you don't want to solve half proposal for them, right? So we just give them that alert on If you notice that the user name value that was typed in remains inside of the text box on that's because off what we did here with the value if it is opposed action then reprints the user name value that is there aren't so that is part one off our battle solved. Now, another quick model we need to do to me is to change this logging. So I have lugging. And if I'm love being this textiles and changes, I want to contextually show the word logging or the link to logging versus hello user name and a logo. But so I'm going to go back to my header that PHP file on guy actually started that process already where I have the lugging link. But what I'm going to do is wrap it are owned on if statement that is going to check to see if the session exists, are not so we know how to check if something exists. Um, first, then went to fire up a little Ph b block. All right, we know we need those for your statements on. Then we see if on we shouldn't know that to check your something exists are not so I'm going to say not is set. All right, so I'm checking if the session does not exist. Alright, so I'm going to see session Super variable Onda. Uh, I have idea of using him, so I know that my decisions that I'm creating inside off my logging are using him. Andi, I'd be all right, so I can check against either one. Because the only time these get created is when somebody has been verifying in the database . Andi gets passed along. All right, so in the header file, I'm just going to say if there is no session by the name off idee and I'm not going to qualify this a bit more, I shouldn't say idea what to say, user, I d. All right, So there is no session by the name user I d or whatever you call it your session, Then I want to take an action. Someone to kind off butcher of this speech being called? No. So this statement is going to say me the chick. All right, Andi on. Then I'm going to print my anchor tug. So if there is no session, I want to print the anchor tag. And then after close that after open up another PHP block. Just the clothes that that open brace. All right, so this opened This girl's brings much is this one, so that's just going to be wrapped up. So there are many ways he could that you could have just called this line by itself. That's fine. But then if you have quite a few uncle tiles and so on echoing, they might get tedious. So that's why I didn't choose that route. So if the session does not exist, then display the log in on then I'm going to see else and then open up another. If will calibri early brace block which understand too close. Same time because I don't want to forget. All right, Ana, put in the wrong. All right, so the else is goingto happen between this speech B block on this speech. We all right? So that's between 9 30 It online for two right now. What do we want to display? So once again, we're checking. If the session does not exist, then we want to display the lugging. If it does exist, then I don't want to display the log in button. Instead, I would probably display the anchor tug on. This one is going to say log boat about PHP. So it's a law goat on Kurtag for it on going to display some block off text and I'm putting it in a spun Spanta is a nice way to just inject some text somewhere on. I'm going to say hello on day. Using him that I want is going to be stored inside of the session using names to remember that in log in, we created a session variable with using him. So instead of me having to go to the database every time after this to find out what the user name is, I can just use this value toe, represent using him, So I'm just going toe span Hello on den. Put in a quick PHP. So that's a great thing about PHP blocks. We can just insert them almost anywhere on dime saying hello. And I went to echo whatever value is inside off the and the session using him on the I understand to put an exclamation saying after this. So, in other words, it should print Hello. Hello? I mean, with an exclamation side Pirates and all of that would be inside the spun on the spine would be adjacent to the law global. So we haven't created the logo peed just yet, But let's see if that makes any difference. Understand? To re fresh. All right, um, going to try and log in. I mean, password again. All right, so we see that we're getting some amount of action. It's not elegant, but we see Hello. We see admin and we see a logo put. All right, so we can go back and style Hello and admin. But I'm going to create the logo button first just to make sure that we have our bases covered. So I'm going to I have loved in the PHP understand? To create a new file. Call it logoed start PHP. Andi just went ahead and put in the court. So the lago dot PHP is really a simple page you don't need any header and footer and so on , so that the cool thing is that we created that session that PHP we have to include the session. All right, so I can just included directly without having the header there aren't on. Then I need to call the function that's his session. Destroy. So we started session session start, and then we have toe destroyed. So it's irony that we have to start the session that destroyed. But the good thing is that once a session, it started. If this is called a second time, will know it was started. So that's why we have to call it here. So it knows that okay, a session is already in progress on. Then it will destroy whatever station is in progress on, then just relocate Ari directors. So the index ph are so if I go ahead and click law goats than expectations that this entire section is going to see Hello, admin. Sorry, It's going to see a log in once again. And there we go. So log in is restored, we click log in and we go ahead and put in a user that exists Admin password log in and then we get a context to a 1,000,000. So that's a nice, quick way to get up context. Your menu on that menu will exist for every other page until we click Love boats. All right, Alright. So back in the header file, I just made a quick change where I replicated that same on Qatar. Glink. So instead of having our span for one said and then the appetite for another said, I just included the same uncle talk and I put that span takes inside off the anchor tug on then in the e True if I just made it on us with the hashtag sits a dead link. All right, so then when the many lords, then we see it's it looks more sensible than it did before, All right, And even for cricket, nothing happens. There is a link, but love boat still works. All right, so that concludes our first or second part off setting up our user authentication after this organ to start. Actually, Tung Ping Doan on which pages require log in Ondo, which ones Don's
18. Control User Access in a PHP Application: Hey, guys, in this video, we're going to add some Coto our pages to make sure that we check to see if there is an active user session. When somebody tries to navigate to that page, if there is, then we allow them to go to the page. If not, then we redirect them to log in screen. Now, on our website, we have a few pages that would want this kind of restriction on on day all start with well , the view attendees. So view records that PHP on Dover. Add Andi delete and update pages. I don't have any attendees here in my database right now, so I'm just into when and quickly at one. All right, So I just created a new attendee on. I just did that so that we could see the view at its end elite. So once again, none of these pages should be accessible as view records that PHP viewed our ph. B reddit dot PHP and deletes that pH be The only way somebody should ever get to those pages is if they are authenticated users on. So we're going to make some minor chord changes to accomplish this on the first thing I'm going to do is on includes. So because it's the same kind of change that we're going to be doing across multiple pages instead of rewriting this scored each time because the court is going to look pretty much the same way. I'm just going to create a new includes file on. I'm going to call this one off on the score. Check that PHP. All right, so in off check off course of this customer, we start off with our PHP tags and inside this off check, I'm just going to do Ah similar if statement what we did in our header dot PHP files so in are headed out. PHP file. We kind of didn't your statement to see if there is a session that exists on this. We'll see if the session for user idea exists or does not exist. Then display the log in button. Otherwise display the hello user name on Laval balls. So, in this situation, I'm doing a pretty similar thing where I'm just going to see if on going to check for my session. Andi, I can use user idea can use user name, so I just use user I d since of the used last time. So if Andi I should be seeing, if not is set So if this session does not exist because you eventually we're checking to see if it doesn't take this. So I'm saying, if not is set if that If whatever doesn't exist on the thing that we're looking for the existence off is or a session with, ah with a subscript called user i d if that does not exist meaning that this has not been created at any point. That means the person is not love Been so I'm going to say header and then set the Loki your son to be log in dot PHP car. So this is just doing a sympathy. If the center statement, if there is no active session with that user name with that session I d or its subscript rather then we just want to relocate Teoh our log in page and know that I have this set up in the off check. I can no include its anywhere that I need this check done so I can start off with viewer records. Andi, at the top off this speech after I require hitter on Require Didi Conn and remember that header would have included decision starts or whatever amable to do have to do after include hitter because header just bring up the whole quickly. Includes header that PHP my header has the session starts. Anything having to do with a session I have to do after my session start which is in my session that PHP So I'm just going to see back in my view records file after I include the header which would have included the the decision starts. I'm just going to include with I wanting once our require someone to require underscore once my off check on by off on the score Chick pH bees were just going to see. Is there an active session? Read Eric. So that means after I include the header and all those wonderful things is just going to check quickly and say, Is the person who is navigating here authorized or right, or is that personal well being? If the person is logged in, then while nothing happens if they are not meaning, if this variable does not exist, that session subscript does not exist, don't want to relocate them to the log in dot PHP peach are on. Then I can do this same check on pretty much every other pit, so I can just sort of view that PHP and I'm going to go ahead and include it right there. Success. That's fine. We're already deals with success we want to restrict and it some just including it onto the header each time I want it on delete. So nobody should try and believe a page or our record someone to do that first. So remember, the Leeds was very simple. We didn't put it anywhere, really. Give it a header and a footer. You just have that peers doing that operation and relocating. But then the fact is, nobody should be typing in bleat passing in an I d on. They're not authorized to do that. Right? So we're putting in this off check before it even checks for the presence off the I D. If you remember Hole, that's core was written. All right, so we edited Delete way changed it It, um Andi, I believe Owen view and view reports, so that should be enough. Let us refresh the page. Andi. Okay. I'm getting an error Oh, my. That was about Miss take off mine. I'm sorry. I did not try to includes slash. All right. So the errors that the final part was wrong. So I just Go ahead, Andi, update all of the files that I would have put the bag. According so have the includes slash off check and not just off check. All right, So once they do that, then we should be able to refresh. And then we see just by refreshing it went straight to the log in screen. So let me believe from stretch. So I'm I'm here on the public form And then and went attempts to click on view records. The off check would have known that I am not loved Insiders redirected me So logging that PHP If I type it in directly in the URL it's the same effect. If I tried to go toe edit that PHP, it's the same effect because off check is running on each of these speeches. If I long in and I put in my admin user and the password, then I will be redirected on. Then the off check would know that I am loved in each times I tried to do something right then, if I love boats, even Norman view records when I click Love Boat, it's just going to redirect me to index on. Then restrict me once again from getting to the pitch until I log in. That's a nice, quick and easy way to kind of shut down who can see what's on your website. You can push the envelope a bit by maybe including rules on, then in the same off check you Ken. Well, you probably wouldn't write that if you went into rules. You probably wouldn't write all of that court here, but in off check, at least you're checking. Is the person logged in on? Then, if they're logged in any kind of further logic to say, OK, watch role. Are they in as a user on? Then restrict them accordingly. All right, so that's a nice, quick and easy way to kind off. Restrict who gender what's on your website
19. Send Email Using SendGrid for PHP: Hey, guys, in this video will be setting up an email dispatch for our i T conference registration system. Now, the context for an email dispatch is that when somebody feels all this form or they get is a a success speech, you just get a pay. Justices Hey, thank you for registering, but they don't really have any tangible proof. And the easiest way to give somebody tangible proof or filling out an electronic form would be to send them an email confirming that they have completed action successfully. And so in this video, we're going to be configuring our attendance application. Such a. When somebody clicks submit, they will load the success speech and also dispatch on email to whatever email address they would have provided during the registration process. Now, when it comes to PHP, there quite a few options you have. You can actually go into your PHP that I and I file that is phoned. If you go to some and in the Apache row you to config on, then you would see the PHP that I and I file on in this file. You can actually set up some SMTP settings. So in the NFL. I just search for S m T. P. On it brought me to where the male function can be configured. You can set up that server name on the ports, which is by default 25. But what you really want changes local host toe on actual SMTP server on maybe went into other configurations on that would allow you to dispatch leverage that SMTP server to dispatch emails. Another way would be to use the send me a library, which is a popular PHP library. But once again, this is kind of unique to your Zampa and you're leveraging on SMTP server like maybe G meal or yahoo if not one that is, you know, local to your context In this tutorial, however, will be using this third party library called Send Grid, which is actually an add on that we can get in Hiroko. So remember I mentioned that we want to keep our application as homogeneous. That's possible or, you know, a single using a singular technologies as much as possible. So send grid is ah is another on that Haruko offers that allows us a few emails I'm on for free. So for the purpose off this listen will be using that free service just like aware using Heracles free service on. Then we'll see how we can integrate with that third party email slash SMS dispatch software . You know our PHP application. Now, if you haven't already done so, then you need to get composer, which you can get back frozen, too. Get composer dot org on There's a previous video seek and his head back up in the videos where I show you how to install and set up. Composer There's not much to it. You kind of slick don't Lord, and it will give you a ah file Execute herbal, which you can just run an install composer on your application. And then, having done that, you can always just go to the command prompt on Just type in the word composer Andre. And if you get this, that means it's not installed. So I just remember that this is my recovery machine, so I'm actually going to go ahead and install it for you to see. So we back at get composer dot org can district on Lord Andi If you're on a Windows machine , just get that windows installer. Go ahead and run that executes herbal file, and it's pretty straightforward. You just need to click next. Next. We'll ask you where the PHP environment is on. Do you really don't need to do much except click next, and once you're finished, you just like finish on. Then you can bring a pure CMD and tap the command you may need to re started if you had it open before. So once you re open it and you type composer, then you will see Well, once you're seeing this, then you know it's installed properly if you don't see this than there's something wrong. All right, so now that we have composer installed, what we want to do is use it toe, acquire the code that will help us. The access that third party service called send grid, which is a part off Heracles. Um, I don't offerings. No, the purpose of composer is package management. All right. So from time to time, you may need to use third party libraries that are not built into PHP. Andi, they you know, they're accessible over the Internet. But then, instead of going and hunting them down and trying to integrate them what composer lows you to do is just run a few commands here in the command line. And actually it was Go out. Source. The latest version off that third party delivery. Ondo. Nobody's into your project. So to get this process started, we need to find our way over to our product, which is in C zam, at least in my context. It seeds some HD docks on, then inside off HD docks. It's attendance. All right, so know that I mean attendance. I'm going to type the command composer require on the name off the library is send Grid will just say that sin, greed slash send grid And then once we press enter, you can watch some magic happen. Now, if you see if you get this kind of result where it is installing and 100% that means that that process was completed successfully. If you bring up your project in visual studio code or if you bro Stewart and explore, then you see that some new fast have appeared. So you'll have a composer dot Js on file, which is Well, this is our package management files, so essentially what we can do it Each time we you use composer to acquire 1/3 party library , it will add a line to this file saying that this is a library that we know half All right, So if I wanted a lesser version, I could change this version and then run a composer file, and it would automatically go and source the version that I wanted and, you know, discard this one and acquire that one. So composer makes sourcing libraries on versions very, very easy. So we get a composer, Jason file. We got a composer dot lock file. You have to spend much time trying to understand what's in this. And then we also got a vendor folder, which has another folder with all off. The dependence is for our our send grid activities. All right, so know that we have don't loaded the library's locally. Let us head on over to Hiroko on add this library to our ups, understand the type in here. Oh, cool. Not com on. Go ahead and log in. All right. So I'm going to select the up that we're working with, which, in my case, it's PHP dash attendance on. Then we go ahead and configure ad on So on the overview PUC installed Adan's and you can click Configure Adan's On. Then we can Search Send. Great now. Once that comes up, we can district provision in on Douala No sin. Grady's apart off your hero Go up. But we still have a few steps, so we have to click on send grid which will launch a new tab. Andi, I love us to go into the send grade dashboard. Now the senator dashboard will actually ask you toe verify your activities here. Identities. Andi Well, we can skip to the party. And if it doesn't, then if you go to settings on, then FBI keys, which is what we're really interested in, it will prompt use IR I already have on a p I key, but it will prompt you to verify your email address. And then that may take a few moments. You wait on that email and you verify yourself, and then you can follow these steps again. So once you're in centigrade, what you want to do is goto settings on a P I keys on. Then I'm going to discard this a p I key on. I'm going to create a brand new one just for us to complete these activities. So on this page where it says getting started with a P. AKIs, you go ahead and click create a P I key, which is in the top right corner. This will launch a little mortal asking us what what exactly you want so we can give the A P I key and name someone to just call it email. Something simple doesn't have to be a fresh name on. Then I'm going to use restricted access because I really don't need every single feature that they have the offer. I really just want the simple stuff to send an email. So I'm went toe thick, restricted restricted access and then under access details, I'm going to drop down mail send on, then I will select meal sinned on. Then I'm also going to select email activity and I'm going to click that one. So once it becomes blue, you know that you have selected it on then at the end off that I'm going to say create and view on then this speech here displease my a p I key so you can just click in this space to copy its by clicking. It will copy it, but they do say for security reasons they won't show it again. So make sure you store it somewhere safe. Otherwise, you're going to have to repeat that step if you lose this key because they won't show it to you again. So either copy it and stories here, or if you forget that you have tow, discard it and then do it over. All right, so once I have that copy, I can click done. And I'm just going to store that somewhere safe for future use art. So Backdoor project. I'm going to start off by creating a new file on. I'm going to call this file. Send e mail dot PHP on, Then I'm going to go ahead and put in my customary PHP tags on the first line of this fight is going to be a require on. We're going to require a new file that would have come with or composer called Also Lord, which is found in our vendor folder. Know that we've included over composer files and this file will actually give us access to certain Send greed specific files, which you'll see a Z go along next, I'm going to create a class on. I'm calling this class and email just like the file this class. I won't have an instructor, but that construct, er sorry. But instead I will have a singular function, which I'm going to make static someone to say public static function. And I'll explain what static means in a bit on. Then I'm going to call this function, send mail, not send email, but send meal. No, a static phone shown pretty much it does not require us toe in Stan. She it on object on object off type, Send email. All right, so if you remember, every time we use the class, we had to see classes or some object is equal to a new instance of that class, and then we can access the functions when we make a static function instead of class. We don't have to do that whole new part, but we can just call the class name and then use a local a scope resolution operator to just access the Function directory. But we're going to see that as we go along. No, to keep our phone son dynamic, I'm going to put in three parameters. We want the two, which is where this email should be sent to. We want subject on. We want content. No, The reason I'm doing it this way is that I want this function to be generic enough that it's used in multiple, you know, operations. So maybe when we create a new user, we dispatch an email to that user who has been created. When we create. When somebody registers for our event, we can use the same function to send off that email so I don't have to write the court every time we need it. What? We just call it a static functional. We take the two as a perimeter we can send over and you subject based on the context. On Dwell, the content of the email can be dynamic. Also now, in this function, the first thing I'm going to declare is the is a variable for our key. Now they're probably more elegant ways to do this. But I'm just using apart off least resistance, so I'm just defining the key in this variable on. Then I'm going to use this key to send the access in grids process are part Fung son. I'm going to use this key to access send grids, send email function, but the first thing that we need to do or will the second thing you need to do is build on on object off type new on. We're going to use a backslash and say Send grid, or is it a forward slash? I'm not sure which one that is. What Sin grid slash meal slash meal open and close parentheses. So this is a class that we know have access to because we included our auto loaders Ph. B. So we can no access this glass, which came over through our composer installed off. Center it on. No, this email object is an instance off our sin grid meal. Klaus. So next we can say email annuities are narrow and then we sit the from So you say, You know what email addresses this coming from You can pretty much put anything there, so I'm going to put my own email address. So if if we were running a real conference, then you probably put the email address off the conference or the Enterprise putting on the conference, and you can put on something here to see the name off the person or the enterprise friend. So if it was a no reply, but you didn't want no reply to show up, you could still write I t Conference Enterprises. But it's a no replies that I t can conference dot com, you know, just for argument. See? All right, The next thing that we want to do in email is set the subject so we can say Set, subject. And then right here we can just pass in our subject. Whatever coming is coming in as a parameter value here, we can pass it into the set subject on. Then we can do the email object again and then add to where we're adding to. So whatever we're whatever value came over as the email address to be dispatched for the email to be dispatched. Tow was in this parameter on. Then it gets added to the emir object, using that phone son, and then finally, we can add content. No, the content variable requires tool parameters. The 1st 1 is the type of content so I can see text slash playing. If I'm just sending, you know, like a regular sentence or regular string if it was supposed to be HTML than I would have to see, like text slash html instead. All right, so you can choose what kind of content you're sending over. Just not if you choose text slash playing, but then you send additional content. Then the channel will not render in the email, so people will actually be getting tugs on each one times any charters. If you chose text slash html and sent over a shingle, could you will actually form at the HTML such that it gets rendered in the person's email just the way you intended it to be rendered. But for this example, I leave it a stakes slash plane on I'm going to then give it the contents that would have come over in that phone call. The next step, having built over email object, is to call our send grid class. So I'm going to create a variable. I'm calling it Send really can call it the variables anything you really want. But then it's new, and then it's slash send grid at Scepter s Capital G on. Then we're passing in that key value. All right, so you see a number of examples online. Some people create what you call an environment file and then put the key in the file and then load that through the modified auto. Too low that environment, key in tow, Arson grid. So their number off ways to accomplish this. What? Once again, I just want the nice Quick, quick, quick. So I'm just helping you to phonics to this service quickly. So as usual, we put in our try catch to make sure that and if anything goes wrong, we catch that exception and we can debug it. So I'm just going to leave exception as generic. I won't these special guest. What kind of exception? We're looking for Andi. Off course. What we want to do if we catch an exception, is echo the exception on the message. All right, so this in this one of a bit more verbal someone to say. Email exception. Email exception. Cots. All right, so then we print the message and return falls, so whatever it's called in dysfunction, otherwise we want to try on. I'm going to call a variable called We'll create a variable called Response on. Then this response variable is going toe have send greed iroh on the phone shine, Send on What we're sending is our email object. So let's go through this again. We have our key because this key will give us access to our FBI. So don't share this key with anybody. And by the time you're doing, this tutor of this key won't exist anymore. So I suggest you get your own and do it yourself instead of trying to copy this one. But you said the key on then you build the email object by in San sheeting a new object off sen grid meal on meal on. Then we set the from we set the subject. We said the tool and you can add as many tools as you need so you can have multiple, too, Um, cause with different values hopefully safe, you have to send the multiple persons at once. You could at as many as you need to on. Then we add the content, and once again the content can be clean or can be. HTML is understand. To copy this on comment, it'll just to show that we could send html also on, then we Instead, she it send grid as a new object. And then we call the function send and understanding that inside off response on what I'm going to return is the response Object. No, Http has different response courts. The most common ones are 404 which is when you have ah, request that was not phoned or 200 when everything was okay. I think sin grid was sent back to to, which is also an Okay, that's like, that's a good response. Tohave. So we will be looking for a tool to response upon a successful function car or our action from this function call. All right, on. If we get anything on that until two, maybe 300 or 404 100 is better quest in case something is wrong. So we'll be looking out for all of those things Now the next step is to modify our success paid. So I'm just going to go and find success that PHP on dine need toe off course, make success aware off for a new kid on the block which is send email a PHP so I will require send email under the DB con. I'll just change that old send email that PHP on then, having done that, I can just modify the court that says his success meaning has the attendee Bean added to the database successfully. If yes, then we will show the success message. But I'm just going to dispatch that email right before. So no, I can say Send e mail. All right. Suzie Intestines is helping me along on then my two Coghlan's, which is that operator I mentioned earlier on that we can just call the function send meal . So this is me doing calling that function in this class without needing toe. Create a whole new object. Andi operate that way. All right, so that's the advantage of our static class. So the send meal form Chantix three parameters, it experimenter one, which is the two. So I'm just into passing or variable email, which is where we want to send the email toe it takes. Let me just remind myself quickly it takes a subject. So I'm going to write the static text you have successfully, all right, that she instead I'm sorry. So it's welcome to AT T Conference 2019 for the subject on then our content will what text you want? You have successfully registered for this year's I T conference and notice Have that back slash onda a posture well, single court here because without the backslash, it would kind off miss up the flow off my string. So when I want to put a single court inside off text, that's starting and ending with single puts, I can use my back slash. All right, So the expectation is that once somebody has filled out the form on, they've moved on to the success speech that we find that success well, fire that email onda that would be transferring to the user. They would just get that success message. Anyway, One thing I'm going to fix, though, is that specialties because specialty showing that I d. Based on the data base, I'm actually just going to fix that quickly before we test this. So to quickly fix that, I'm just going to go over to crowd Andi. I'm going to find the function. Well, I think I would have to create a function to get specialty by i D. So I have one that says, Get specialty someone to quickly create one just to get the specialty by I d. All right, So I call this one, gets specialty by I d Pass again and I d parameter modified the SQL statement a bit to see you get from get start from specialties were special. The idea is equal to just I d on then I did. Although the wonderful things that you do where you prepare, bind all parameters and then execute your statement, Andi Then in the success speech, I modified it Obits toe include the phone shown called right under the East Success That's really much. Or where you put this as long as you're you're passing in earlier calling the function and you're passing in that I d that it needs on then the final change. I mean, it was instead off echoing the posts like we were before. I'm no echoing the name column coming back from that result that was stored in specialty name after making that function cop. So those are the changes. I just mean, So we're going to go ahead and fill out this form on, or we can just give it a quick re fresh so that everything is nice and fresh on. Then I'm going to go ahead and fill out this form. All right. So Archibald Douglas has putting all of his information. He's, ah, system administrator on his email addresses. Loan procedure one. Any night at gmail dot com, whatever it is. And then we go ahead and take submits so we can see here that at least our quarries running , cause we're not seeing the one artistry or the number. But we're actually seeing the word system administrator on this would imply that the operation has been completed successfully. No, you can give this a few moments, but what I wanted to do is go over to send grid on, verify that the email was despite so you can go to sin grid on. If you were at the dashboard before then you can go toe activity. So if you click on activity to the left, then it will bring up all of the emails that were dispatched by the cones. Using your a p I key, Andi will show you home. Any emails you've dispatched for the month under that are cones. So I have quite a few here because, well, I use the service. So, you know, I would have quite a few, you may not see as many, and if you don't see any as yet, then you can refresh until you see it. In addition to checking your email, you may also want to check your email because it would probably go to spam. So you want to if you don't see it at first sight than you want to check spam just to make sure that it was really dispatched. Andi. If all of that feels, then you may want to check your firewall because there are times when firewall settings will prevent the emails from being dispatched from your local machine for maybe I should say that a different way. Sometimes firewall settings prevent the A P. I call from being successful, so that has something to me before. But I usually get resounding success. When I attempt this through. We will, having hosted application on Hiroko. All right, so by way of confirmation, here is the email that was just sent. It's seeing welcome Toe Eddie Conference as a subject that we set it sent to my email address on the text that we set in the email. So that's just confirmation that it worked, and this was from my local machine. Once again, you can always check your firewall settings or published a heroic or to make sure that it works. So to wrap up this video, I'm going to publish the heroic ooh published to get a published A miracle on Just test it once again. So the first thing we're going to do is bring up, Get hub for that Stop. No, this is a few videos since I've published anything seriously, that I have quite a few changes 182 to be exact. It is always recommended that you make as you make changes and you confirm that your changes work. You check in your court so you shouldn't never get to the point where I have 182 chickens to me just because you weren't checking in your court. All right, so that's my recommendation. But for the situation, I'm going to make one big check in on. I'm going to have a huge summary where I added user authentication since the last chicken on die also added e mail dispatch feature. All right, so that's that. That's the chicken that I'm about to make on I'm going to push that to master on. Give that a few and then after I push it the master for my local reporter push toe origin. So it goes to get up itself on. I just realized I remember that it that prematurely I need to update the con Fess, Remember that Khan is looking to our local development database. So I'm just going to come in that oh aan den on comments the settings for our remote server . Alright, so they're more elegant way somebody Just what? Once again, this is a basic course. I'm not trying to make things too complicated So we just un commander more database connection on. Then we can just go ahead and change that and you see that gets off for this up kind of gives you a little takes. The artists understand to work with that orbited con put come into monster and then push the origin on. Then we bring up herro coup where we go to our up I'm already in the opsec and go to the up , then go to deploy on. Then you select the branch Will the branches early must and the new deploy branch all right , so we can give Hiroko a little time to cool off, cause after deploy, its not accessible the same time, at least not on our free plan, so we can give it a few moments. But once you go ahead and view on, I'm seeing that I haven't ever here at. That's because I didn't copy over my user stable from the last few videos. So I'll just do that quickly in the background. All right, and that's done in order is restored to our application. So let's try and fill oat a new user, and I'll just reuse the same arch ball. Less data that I had, since that user would not be in over remote are in my remote database on Submit, and I see where the operation was completed successfully. And if I check, send grid and refreshing a couple times on a couple refreshes later there we have our email that was dispatched to our new registrants, So I'm back in my court and I'm looking at the success that PHP page where we called the email function. The fact is, we could kind of personalize this email we could say Welcome first name last name. You know the person's name toe conference. We can include virtually any information that they just submitted. We can actually include it in our email just by, well, con fascinating are using interpolation to get that variable into our string. Another fun fact is that sin greed is powered by twilio, which is a powerful estimates dispatching service. So if we wanted to dispatch an SMS to them, we would have gotten there, therefore number in the process. So all of those options are no available to you through this third party service. Andi. It's all easy to duel using or newly acquired Ph B skills and heroic cool hosting capabilities. I hope you guys enjoy this video, and I'm very interested to see what you guys will use this new superpower to accomplish.
20. Upload Profile Pictures: Hey, guys, in this video will be modifying over attendance application toe alot, potential attendees to upload a picture. The scenario is that when somebody sending for a conference, you want to give them the option to upload a picture, maybe a self portrait, so that maybe we can put it on the i. D for them when the conference comes. If they do not upload a picture because it's completely optional, then we want toe. Make sure that we put a default picture instead. To accomplish all of these objectives, we need to modify our form or database and are supporting court. Let's start with the form. So the support of file upload we need to include a file upload control from on our form. So I go to the court for our form, which is index dot PHP. And then I am just going to duplicate one off the existing form groups and then make the changes accordingly. No, for a form we still need over label on going to see four avatar on what it will print is upload image, and then I'm going to put in brackets optional so that they know for sure that they don't have toe next. The import type that we need to use is off type file and then or class. He's not going to be formed control this time, but I'm going to use our custom class given to us Bible trip, for that's his custom bash file bash Input. This will kind of give us a a slightly remixed version off a file input control. Also, go ahead and change the idea and name Toe Avatar. So that's everything on. I don't need this attributes area described by salty. That's oats on. I don't need this label underneath it either. On. Then, I'm going to include a class custom file in our def. So instead, off form group on when tohave custom file Onda Finally, I'm going to have a label below. So all of this is just required for the custom files Understood. Duplicating this label on goingto have four. Instead, I'm going toe Have class equals cost, um, dash file dash label. So if you actually look up on the internet for bootstrap custom forms, you actually see some references to these kind of custom labels that I'm talking about or classes rather on the the text inside off this label will be choose file. So you see where this label falls into place when everything is printed. So the class that sorry Dave, is custom file as a class. Then we have a label because you know, every form control has its label. And then we have the input off type file class cost on file input with an i D and name. Now let's see what that produces. All right, so that's what it produces and notice. We don't have the top label appearing here, and also when we click, it's not working. I'm clicking it right now. It's not working. So I got a bit overzealous. And firstly, in a custom situation, we don't have that label here, at least not for the file and put somewhere to take this label. Haute. Since we already have that, Bottomley will seem to file on then for this label, I'm going to have to put in the attribute for avatars. Remember that all the labels have the four on the control that therefore, based on the I D. So if ID's of attire, then choose who I choose. File is four or avatar control and since we have no real visual cue. What I'm going to do is modify. They set up similar toe our phone where we had this small text being printed that showed a little help. So I'm going to just put underneath it something to say. The file upload is optional so that people who don't wish to blow the final know that they don't have to write on. I'm going to change this to maybe text dash warning so that it pops a bit more. So when I refresh, I'm so no text off warning doesn't look very good to understand, to say danger. So it pops on the screen and they're so far abilities, options. So that may not be the best choice off because the second arrow he can find a better one if you in your context. But I leave that for no on that. Just put a slight br in between these two controls. So that s Oh, that looks a bit cleaner on. Then notes when I click, it brings up that fire search. Ballack. Now the fire search dialog is going to be displaying every single post civil file on these air. Some script plaza on this machine. I don't need script files being uploaded as avatars. I need images, someone to make one more adjustment to this control. So the input on went to add an attribute that says except and then equal on. Then the value for that will be image slash star. So that means I'm limiting the type of fires that this control will accept. Toe image types with its JPEG PNG. You can always list them out if you want more limitations. But I just leave star. And so when I refresh and try that again, notice if I go back to my desktop I'm not seeing any of the script files because, no, I'm only limiting it toe image files. So no, that's step one is completed, and we are allowing the person to be able to Bro's, their computer selected image and submit. We no need to modify what happens upon submission. So to do that, we need to go to our or action page, which is success like PHP, and makes a modification. Still, our post processing court here will be introduced to a new super global variable called files, so I'm just going to make some space here on. Then what I'm going to do firstly, is I'm going to define a directory Orwell a target location for anything that is uploaded. So I firstly created a director called Uploads that has created a new folder. Did that our screen. So I created a new folder called Uploads. And then this is where all of my uploaded files will be going so you can go ahead and create that folder and then you can continue along on, then till to set the target directory. I'm just going to define a new variable on. I'm going to call it a target on the score the i R. Short for directory on. Then I'm going to set its value to be uploads. Sorry. In string uploads slash. So my target nurture is my uploads folder. Next time went to build a string that will represent the name off my file. So to build the string, I'm going to first start off with the target directory. So that's Dallas and targets on the score directory. And then I'm going to conduct attorney that onto the name off the file, which I'm going to get by seeing this name, which is a PHP function on it receives some file path or find him so inside off base name. I'm going to then reference my super global variable called files and then finance has to worry sub scripts. The first Ari subscript would be the name off the control. So just like with Post, only wanted the value coming from the first name control. We reference it by name. So in this case, we want the file coming from or file upload control, which in this case, we called Avatar on then the attributes off the father we wish toe have. So in this case, I want attributes Name. So the attributes is just like when you right click Ah file. You know, Windows system on. Then you see the name the date created the size. All of those are five attributes. So in this situation, I want the name. Now that's have the name off the uploaded file. The next thing to do is to actually move the file somewhere to say moved uploaded file, which is a PHP function on. Then This one takes to permit is itics. What files should be moved as well as the destination. So to me. This a big cleaner. What I'm going to do is I'm going to create another very book and call it original file are five, and that will be equal to the original name coming through our post. All right, so art file is storing the file, the name off the AB attire or the name off the file coming through our of attire control on . Then I'm setting the Tirlea character to be uploads on. Then I am determining that this is my destination. So really, actually, I could have called this one destination so much really in that destination, because what this will do is act as the destination path for this file. So by building it to say, uploads slash the name off the file on the I'm preparing it to be moved by this function. When I see, move, uploaded, file and see dollar signs, original file and then comma dollar sign destination. So this function, once it's called, will look for the original file coming through our submit action on then create it at this destination that we have specified come winter and a cursory test. But obviously, if I submit, the form has been to attempt to do a database Inter, which I don't necessarily want someone to show you. A little d bullying trick where you can actually use a function called Exit that actually stops the execution at that line. So in other words, when I submit the form it will get this far. Once it hits the exit, nothing else will happen. So I don't want you to submit anything to the database just yet, So I'm going to use this to pause the execution on just validates that the file is moved as specified. So I'm going to go back to my form, refresh Philip totals, and then I'm going to choose a file, and I have to hear our student of stealing details someone to to students on. Then I'm going to click submit and, well, nothing happened. Hold. I validate this. I would have to go to my file system, or I can just go back to visual studio cord on. Look at the contents off the upload folder on Nothing happened on. Nothing happened because I made a mistake here. So the original file really should not have the name, but rather the TMP name that's the attributes off the fire that we want, so I'll explain what that means. Name is what we see when I see student that PNG on the fire system that's the name over pH . B is not trucking it by the name off the file, but it generated ah, temporary name for transit between the post action. So for us to be able to actually move the file or interact with the file itself in any physical matter, we have to reference it by the TMP on the score Name attributes. So in the orig file variable that's I created here. I want to say files of attire and instead of saying name with just a t m p underscore name , So move uploaded file will be moving. Whatever file has that temporary name toe a more permanent destination on our fast system, someone to see if these changes and then I went to just refresh. So it actually just submits everything to the form again and click. Continue on. Then we're going to go back to our uploads folder on defy Look inside or close for you see that a fire has been created by the name student that P and G. No. One thing we don't want is any tour registrants uploading the same image with the same name on one over writing the other. It's always good practice to rename your file to something that is unique on that. When you're storing it in the database, you are always sure that you're getting the CART picture for the courts person. So in a situation where you have people creating user profiles with unique user names, then you probably want to rename the image to the user name or to some unique I. D. That you're also starring in the database. For this person. In this situation, the little really unique things about any registrants will probably be their email address on maybe their contacts number. And I think contact is probably more unique than email address in this situation. So I'm goingto be renaming the files that they upload toe have the name that looks like they're full number now. In order to do this, I'm going tohave toe change the way I set up the destination by setting whatever custom name I need here. But then I still have to add back the extension off the fire, which I don't know what extension it is since I'm looking for images which can be PNG J pick or G I f. So instead of trying to force an extension, I'm going to have to dynamically determine the extension. But then I can on the fly determined the name because I'm collecting the full number from this variable. So what I'm going to do is create another variable under orig file and I want to call this one txt short for extension. And then I'm going to use a peace be function path in fourth into which I'm going to pass the files. Super variable, sometimes stopping this. But I'm passing in the name, not the TMP name. Because the TMP name is only known to PHP, it only makes sense the PHP So I want the name which is going to be student dot PNG on. Then I am going to use path in four extension as the next parameters, so path inflows and to get certain information about any file that is given to it on the information I want is the extension on then that will be stored in my txt variable. Next. I want to modify my destination toe instead off passing in the base. Name off the file. I want to pass in whatever value is inside contact as a variable coming back through from my form on. Then I'm also going to con Katyn, eat whatever value comes back in my extension. Variable after finding off the path info off whatever fire was uploaded. So then the destination will have uploads slash whatever for number the person entered dot Whatever extension that file hat on, then the move uploaded Will store that file with that new name inside or uploads folder, someone to run a test again with the same file and see the result. So, no, we choose the file on. I'm choosing student at PNG once again on Then I submit on Remember that we have the exit, so nothing really happens, But we can go back on, then we see. Okay, so I messed up the quarter bit, but at least we're seeing the phone number on the file extension. But what we're not seeing is the dots, because windows needs that dot in order to determine the file name. So I'm just going toe modify the way I wrote this altogether on. I'm just going to put in braces on day, create one be contiguous string with interpolations. So no, my destination looks like the variable target directory, which is upload slash than the value coming back from the contact docked. Whatever value comes back from extension someone to see that change. And I'm just going to re first re submits and re tries that operation and then when I go back, I am no seeing the full number docked. PNG. I'm just going to remove this file from the folder. All right. So, no, for each person who registers whatever full number they enter will be used. The Brini more file on that file extension will always change based on what they put so know that were successfully uploading fast our server? No. We need to modify the rest of the court so that the data bees knows about the uploaded fast . So the first thing I want to do is modify my database using Petri. My admitted to add the new column. So I'm going to my database into my attendee table. And then I went to modify the structure by saying, I want to add one column after specialty on the score i d. So I'm setting off this column toe. Have the name of a tire path be off Time of arch are 200 on dime sitting it to be no level because once again the picture is optional and then we click Save on, then that new column is no added. No. I need to modify my court to make sure that I'm passing in the file path off for uploaded file to the database, which would be in the column avatar path. So we know the destination because we build the destination. So we need to make sure that we're passing destination in tow. Our insert attendees function on by extension. We need to modify the function. So I'm going to pass state destination, and I'm going to add it as a parameter to the insert attend the function, and then I'm going to go over toe my crowd file where no insert attendees is on. I'm going to make sure that I have representation off that new parameter. So it's expecting a new apartment. They're called destination, or I can call it Avatar Pa. So remember that we don't need to call them the same thing as long as they're in the right position. So about our path will no be represented in my quarry when I come down here and I say comma of attire path on, then if I'm putting that there than I need to have my parameter, which is being bone. So that's colon of a tarp path on. Then I just make another line here on, but make sure represent avatar path as both a parameter in the quarry onda parameter. In the function, I'm going to execute the test where I fill out the form, select the same student that PNG file on attempt to submit. When I do all of that, I get my success message on my take note of the contact number. Of course, I didn't change it, so that just goes socially that it will not try to duplicate the finally will always replace once it sees ah file with the same name. So make sure that whatever nimir giving the file for each user is very unique. However, if I go to my database and I look at my attendee table and my data, I see that the new column. No has values. On the path is uploads slash whatever the file name WAAS or has been changed toe now final step Just to complete. All of this would be to display this profile picture whenever the person submits successfully, as well as whenever one off the admin users views the profile for the user. So that means we need to modify over success that PHP page as well as, oh, we're view dot PHP page to make sure that we're seeing the image as it is being referenced on the persons database record. So what I'm going to do in my success that PHP paid is go down to the section where the information is being printed on. I'm just number. Listen to keep it simple, I'm not really going to focus on getting it to aesthetically pleasing. I'll leave you to experiment and do that. I just want to show you how to display the image. So since we're on the success that Ph. B page we know the destination is going to be somewhere in our project on, we just need to set up on image tohave an image tac toe have the value as whatever path is being stored inside of this destination variable. So I'm just going to go down to the sexual where I want to display the image on going to see I m g SRC is equal to on. Let me just leave that blank for no on. Then. Remember that whenever we want to put ph be content inside often. Html tag. We need to have our PHP tag on eco whatever variable it is that we want. So I'm just going to see inside off these quotation marks a PHP tag and I disclose it at the same time on. Then I'm echoing my variable destination. Remember, destination is the path. So this will say something like uploads slash file name Dr Extension on. When this echoes that value, it will be the image source value. So if I do that and I refresh after submitting, then I see where my file is. This is the student found that I upload it once again. I'm not too concerned right now about the placement. You can go ahead and fix it how you need it. But I'm going to just show you that whatever you do in the AMG tag affects that image. So if I think it's too big, I wanted to be, maybe have a width off 200 by on the height off 200 I can say 200 on. Well, CSS is the best way to accomplish that, so I would actually just a style style is equal to on then set the which so be maybe 50% instead, Andi height to be 50%. So when I do with that and I refresh you see that the CSS would okay, that's not much of our resize. But you see my point. Whatever you do on the ancient sites, only sit this 20 on 20 and then you see a bigger resize happen on. Then I have some bootstrap classes that can help me to make it look a bit cleaner. So there is a bull shop class that sees rounded so that one gives it a little Rhone border on. If I do that, then refers, well, the effects off road that aren't very visible, someone to try another one and see rounded dash circles around. It kind of takes the sharp edges off the corners, but rounded. That circle actually creates a circle circular representation off the image. And there you go so you can actually go ahead and use bootstrap and whatever other tools on make your image look nice representation. Now, this was a success, but I need to repeat that feat on the view dot PHP p it's understand to call back on, but actually just them to copy this scored because it will be doing something similar on on the View page. I'm going to Rio put my I'm g tug in my court. But then you notice that on the view page we were referencing things by seeing results on the subscript. So that means I can't call destination on this side. Instead, I'm going to just reuse this bit off cord on. Instead of echoing first name as image SRC, I'm going to echo avatar underscore half, because when we did our select query, we selected star and star brings back all the columns. So one of the columns that we get back is avatar path, which is storing the path toe or image inside the beta bees. So that's it. So we can dynamically lord the image for the person who is logged in so I'm just going to put this statist someone to go on on, log in. I know that I'm in. I'm going to go ahead and view one of thes. So if I view one from before, then you see the images broken. So we don't want that off course because this is broken. Um, well, this is broken because this record existed before we started putting in images on There's no image to display. So we don't want that someone to go back. And I want to find one that we just entered on. There we go. So the image is being loaded not because it's being stored in a database, but the path to it is in the data. Bees attached to the person's record on the file is present on our system. So we know where to reference for what you know. The last thing that I want to do is make sure that if nothing comes back for that person, meaning they didn't upload an image or the register before the feature was available that we displace something off a default avatar for them regardless. So I'm going to adjust the court that is printing our of attire. And I'm going to just put in so called Turner operator inside this string so that we can determine if whatever is being returned from the data bases empty. If it is, then we want to print something. If it is not, then it's fine. So there is a function in PHP that's is empty, which takes ah parameter, which is probably going to be a stringy can. Binary can be anything but empty is basically I just seeing Is there any content inside off this variable on? Then it returns a bulletin which is a true or false. So the Turner operator that we're going to put instead off a whole if else just to know what to echo is we're going to see question Mark, and then we see if it is empty. So this is going to return Shore Falls. What comes after? The question mark is what will be done if it is true. So if it is true that it is empty than what we want to do is put in the value up lords slash on day, I went and found some generic, um, image. I'm calling a blank, that being PNG So I want to reference that blank file if that If the string is indeed empty , so blank dot p and g So this will act as my default um of attire for anybody without an image reference in the database. So if it is empty, then I want to echo this value on then we have a colon. That is, if this does not evaluate to true, What do I want? And what I want is to echo the actual path to the image. So right here in our image SRC we're doing a quick if else are making a quick decision to see echo this value based on the turn Eri operators old So the Turner operators seeing I want something that is going to return to our falls in this case, is the string empty or not? If it is, then I want to echo this or return this value so that we can be echoed if it is not, then echo the actual avatar particles from the database. So if I go back to my listing and this is one with it, so I can refresh and I see that it's still keeps that image if I go back to the list and go back to one of the first registrants who didn't get to upload an image. Then we see that they get that default. If I go to any other one that didn't upload an image on this person, will this person is giving me. Okay, so I'm getting that default Archibald default. Right? But then this attendee with I d four is not giving me that someone to look into this. All right? So by looking in the database, I can see what is happening. So the attend you with the i d. For those have content in the avatar path over. The problem is that the file name is basically dot g pg, whereas their own is that PNG? So, in testing, I change the extension and change of fast around on dso. The first fewer distress that we tested it with would have a dot PNG, but the rest have the correct that JP. So that is why you are seeing that broken image. So it does work. Um, but then it's still a concern because then it's seeing that it can't find the file so well , we can We can determine if the file exists. You can probably do some research, but I'm not going to get into that. I just want us to get over the hump off, uploading a file on having it displayed. So I'm going to just run through this with an entirely different person, this time on a different full number so that the file is different. So this is a different full number on I went to choose the file on. I'm going to go on the Internet and find a different file. So far. I phoned a file off with a fox, an image of a fox. So that's the other tire that this registrant wants on. Then when I click submit, we're seeing the Fox. And if I go and view attendees and I view that persons profile, then I'm going to see the Fox as their avatar. If I look in the data base by, just refresh the database, then Archibald Douglas was inserted with a different file with a different file name
21. Final Touches: Hey, guys, congrats for making it this far In this course in this video will be just be doing some final touches toe our application this far. Now let's just review are up quickly so we can see what kind of finishing touches would be required on the first thing that comes to mind is the father we haven't really put in a sensible footer upon to know on then. The other thing is that we need to publish this publicly so that our registration form is available publicly on. We need to get through those activities in this video. So the first thing that we're going to do is make a more sensible looking footer. So up until now, we have our includes file and we had our footer, not PHP file on in that file. We just had a live and we called it footer on. We just echoed copyrights on what we did to kind off create this density in the footer and the rest off the page was, and other sports the inexperience. Here we put in some br tax that kind off pod the space between our footer on the rest of the page, know The reality is that bootstrap actually kind of gives us classes that alot us toe have fixed footer. So the bottom of the page so we don't need 10 are so br tax to put space between the content on the footer on. Secondly, we're going to be using bootstrap toe, actually make our foot or look like something sensible. So I'm just going to delete this footer tag firstly, and then we're going to rebuild it. So I'm deleting it, and I'm going to probably retired some of the court that I just deleted. But either way, we can do it together. So remember that in the footer dot PHP file that this closing beef tug would correspond with the opening def tie in our header. So if I go back to the header, then I see that you have Dave container. All right. Remember, the container is what gives those those that margin like space or the space between the left, most edge and enough bar start or the contents. That's what the container does are enough bars in the content. I want to put the foot the footer insider content also, as you can see, it really is inside of that container talk, brother. So I am going to inside off the footer right above that closing day for my continue, It'd I am going to create a div, and we actually had it just know. But I deleted it. And that's no problem. We're doing this together from scratch, so I'm going to create a div on. Then I'm going to give this def the I d footer on then. Inside the same footer. I want to give it a few bootstrap classes. So the first class I'm going to give it is P minus tree. And I'll explain what each class does that I'm going to give it MB miners to on DBI g dash Andi, just go back and reference the header to see what kind of BG we use. So we used b g dash dark. So is a dark theme. Well, while we use the dark nav bar, we really use the color BG dash primary. So in the footer, I'm going to use B g dash primary to set the color off our footer on. Then I went to see text dash white to make sure that we get white text on, then the clincher is fixed dash bottom, so this in particular will make sure that it is going to go down to the footer section off our page or are stay at the bottom of the page. So fixed bottom means that no matter how much or little content is under page itself, the footer will always be at the bottom of the page because upon to know, we're allies that if there is a little bit of content, it kind of rises open if there's are lots than it gets pushed on, which is why we need that to be our tugs. So this class will kind of force it down to the bottom and then combined all of these kind of create a nice blue footer section or give with a footer content at the bottom of the page on. Then, inside off this footer, I'm going to put some more official text where I'm going to see that the copyright I T conference attendance system on de called the date aan den So that p tag with text. Dutch centre should center line some amount of text in our footer footer Div Andi central and off course and print the date that it is being viewed. So I'm just going to see if this and then I'm just going to go back and refresh my page on . If you look at it now, you'll see that we have a more sensible king footer. Now, let me explain what each class does. So the first class p three is talking about the padding so we can just go back to our p a drag click and go to inspect elements. And then we can kind off, take an antique, each elements. I'm just inspecting the footer itself. And then we can take an unclean Kontic, the classes being referenced here. So text dash white is they're saying that the color of the text should be white and I think , why looks better against blue background and blue. What off course you can customize yourself. P Dash three means that the padding is applied. So if you look at the difference when we click fighting versus tickets off, you see that the parting changes each time so I can leave that on then the MBI dash to talks about the margin to the bottles. If I antique that you noticed that that white space that's at the bottom of the footer kind of goes away. So actually, off all these classes, I would probably want to just remove the M V two. So I'll just go back, remove the nb tool on you can off course, find all of these references on the bootstrap websites in the documentation on you can customize your footer hall. You would like it to be no notice that my foot or kind of stretches right across the page instead of being confined to the container, right? So the container would be along this margin that I'm moving my most, whereas the foot Terry stretching right across. And it is a common design pattern toe, actually, have I noticed that the footer as I scroll the footer Dusty's there, right? And it's a common design. Patrons have the knave buyer stretch right across. You never content itself is kind off within the margin. So I'm actually going to just do that on about this. Modify the header section on doubt, eat this entire love boat off that Dave. So I'm just going to put that entire now section out off the continent live and I'm going to put it above that continue narrative. So still of minor of everything still works as it should. But then when I refresh you see that my nerve will stretch right across the page on then. Further to that, if I wanted the actual content off the knob inside the container, then I can actually just go back on bond. In this live, I can see containers of collapse in of bark dash collapse on. Then I can just see container on down once I do that. Sorry. It just personal wrong buttons there. So I say container and refresh, and we get something that s so the links themselves are moved in on the logo. Estilo. So I mean, you can me that decision. He wants us to look like that. That's entirely up to you. Um, but I kind of look like the look off having it kind of spread across the top right across the people. So the header is rent across on the footage right across on. I cannot like that Look. So that's the first touch that we're making toe over application. Next, we want to just upload it to Hiroko. So we're going to go ahead and check in our changes in to get hubs and then synchronize with herro. Cool. On and off course, we made a change where we updated the profile picture. So we need to update our database also. So I'm I've opened up my workbench on guy connected to my remote database, and I already have the data base here with a little bit of data. I don't want toe remove this entire database or remove all the tables and have to rebuild them. So what I'm going to do is just manually go into my attendee table on at the columns. I can distract like attendee on. Then I can go toe altar table on. Then that would allow me toe Add any column that I wish. So I'm just going to go back to Loca. I am served my be FEMA admin on. Find the field on, recreated accordingly. All right, so one that loads. I went to that in the We'll have 10 dance on the score. Db database attendee table phoned avatar path and see that his large are 200 it alos no. So I'm just going to copy the data type. So I mean, you don't have to copy MPs. I'm just doing the second speed up this process, but I'm going to call it of attire. Well, was it of a tire on the score path? So I already built the court to look for that field in my death. So obviously, whatever I'm whatever change are making to the production needs the mimic what it's looking for in depth. So it's a bar chart 200 on once. I mean those changes I can apply, but be it's my remote database on. Then once the database is ready, then I can do my uploads. So I'm going to go to Vicky Top, and I'm just going to check in these changes and I'll just say updated Hitter and footer Andi. I would have already updated the fact that I did the profile pictures to get up, so I wouldn't Well, that's why I only have these 21 James Fatso, depending on what you may have done in your get up, then you may want to add more to your comments, but that's what I have done. So I'll just commit to Master Onda push and then once that's done, we know we can go to Herro cool on log into our application on. Then we just go to deploy on. We go ahead on deploy brunch. Master, once that's done, we can off course just view our application. So our application is here, and I'm just going to try and fill. Oh, this poor are selling Philo the forum Just going to choose a profile picture on. Then I'm going to click, submit on this, receive everything, works and there we go on test hero Cool on There's our fox know if I log in just the view all off the application are the applicants who will be attending the conference and see the information that they uploaded. And I see here that add mean logging is not working. So I went over to the database and I see that I literally have the user admin and the word password to remember that we went through some amount off rigor trying to harden the way the password appears on D. What should really happen is that we should have the admin user Yes, on the password in ah, hushed form. So that is not happening here. so I don't want to do is just remove this admin user on whether I can just remove all these users. I don't need them at this point. And then I'm just going to refresh this speech on then do remember that once we load the page that the Cory should run or the Oh, sorry. I heard that sort of corn should run, but what I didn't do was apply when I deleted. So let me try that again. So with workbench, when you modified the data set like I just did, where deleted them. I need to click apply, which will actually generate that quarry. So I'm just going to click, apply again. And then the choir would ever been run successfully. No. When I refreshed the page and it loads that con filed, then it will go through. And it went through because it resubmitted admin and password toe the form, which also appeared in my database. When I read when I rerun that quarter to select star from users than I see my admin user here with the hushed password. All right, so if you run into that difficulty, which I'm sure you won't, But I'm just showing you what can go around. So at this point, these air the attendees in our database and here is the most recent one, which is test. Hiroko is an administrator. And then, if I view, then I see that profile picture. If I go back to the list on Dive you another one who was pre profile picture than I see that it's only loading that default picture like we would have designed it to do on our local deaf environment. So that is how we go about building are kind off into end application with PHP and using or PDO libraries to help us perform crowd operations on, then get up for source control, Onda change tracking and finally, Hiroko for Web hosting and we're teaser, Mort, SQL Server and Sin Greed for using email services. So at the end off the day, that is how you go about building at PHP application from scratch up until inception on, then styling with bootstrap for on hosting it on the Internet