Viewers: in countries Watching now:
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
We completed the first two parts of our admin area CRUD, read and create. Now we're going to build on what we did for create as we implement update. Like create, update will have a web form to let us edit a subject but there's one main difference between the two. With update we're going to need to first find an existing subject in the database and then pre-populate its form with our values. With create we didn't need to do that because there wasn't an existing subject to worry about. So in a sense update is going to combine the work that we did for both read and create.
We're going to read it from the database and then we're going to display the form that we had for create but with values populated in it. I want us to make one additional change. When we coded our create action, we did it as two separate php pages. The first page displayed the form and the second page performed the form processing. This time I want us to create a single form that will submit to itself. The form processing will just live at the top of the PHP page before the form displays. So as you might guess this form is going to be called edit subject.
And I'm just going to go into Textmate. I'll make a new page and I'll just save that as editsubject.php. I'll put it in widgit corp and inside public. Now our page is going to need a lot of stuff but the one main thing we're going to do is have a link to it so that we can get to this page. Let's do that from our manage content page. So we're looking at a subject page. We want to have a link down here that will then let us edit it. So let's say a href equals and then it's going to be edit subject and it's not just enough to say edit subject.
Were going to also need to provide the subject id. Alright, so it's a very important part of this, is that it needs that subject id. So we have current subject. That's what were, were using already. And we're just going to keep calling it subject equals and so we'll need to then drop in that value right here, php, Eeho, the current subject not the menu name, but the ID. Okay, so that will then give us that edit subject page and in the query string it'll have subject equals and then whatever our current subject's ID is. That's what we'll put in there.
Let's let's just try that and see if we get a, a basic page working. So let's click on our Widget Corp. There's our edit subject link, we click on it and we get a blank page for our edit subject. But that worked. And we do have subject equals one here, ready for us to find that page. Okay. So now we have that. Let go back to edit subject. And what do we want this page to look like? Well it's going to be very similar to the work that we did for new subject. So let's just go in there. I'm just going to copy all of that, and paste it in here. We're going to want our session.
We're going to want our database connection, our functions. I'm going to make just one quick change here. I'm going to take find selected page and do that up here above the header. So then, it's going to find the selected page for us. It's going to set it equal to current subject and current page. It's doing that for the benefit of our navigation. We could leave off the navigation on this page. But that's fine. It's going ahead and finding our current subject for us. That's a nice little bonus. Now because that subject ID is really required here. We can't really view edit subject if we don't have that ID.
I'm going to just add in one additional check here. Right at the top of the page. I'm going to say if we don't have a curernt subject, well then redirect. For something went wrong, we were'tn able to find the current subject so therefore either the ID was missing, it was invalid, subject couldn't be found so it'll just redirect us somewhere else. You could include a message or something like that but you get the idea. We have to have a subject ID. We have to be able to find the subject in the database if we want to be able to edit it. Okay, so let's just bring up that page here. Take a look at what we've got. Let's reload that.
And there we go. So we have our basic, same form that we were using before. It says create subject on it, so we want to change those things. Instead of saying create subject, we're now going to say Edit subject. And down here at the bottom, same thing. Instead of create, we're going to say edit subject. And I think it's also nice if in addition to just saying edit subject, we say what the subject is. Use some php tags, echo current subject.
Menu name. That one I'll display the menu name at the top for the current subject. So edit subject colon and then the name. Now we also don't need to go through our form and we need to drop in values. We need to populate it. Lets come back over here and look at this. Just do save, reload the page, edit subject about Widget Corp but my menu name's not displaying here. And the position is not really displaying. This is a default position, it happens to be going to 1. Visible's not displaying yes or no either.
So we need to populate those values. So let's just take what we just had for menu name, we'll copy that and let's come right here to value for the input field, paste it in. Save it, go back and reload the page. And there it is, our mini name is now pre-populated and ready for us to edit. It's a little trickier though when we get to the position and to visible. For the position what I'm going to do is take this option, and I'm going to break it up a little bit. So, option value. So, here's the value, that's all fine. Then I'm going to put a double quote here, echo. Now that's the exact same thing, I haven't changed anything, I just broke it onto two lines.
But the reason why I wanted to do that is because if this is selected, the way that the HTML spec looks, the way that it works would be that we would echo out just the simple text selected. We would need a space though, we want to make sure that there's the space before it. So that there's a space here after this double quote and that would tell us that it's selected. Now this is going to make all items selected currently so it has to be conditional. If this item is the current item, then output this little bit here. We know how to do that. If current subject position is equal to and our current position is count, aright, that's how we know what that is.
Current position is equal to count then we're going to echo that selected out. Now, there's one other change we need to make, before when we were adding a new one we were outputting numbers that went from the current number up the number of subjects in the database plus 1. When we're editing, we're not doing that. We want to have it limited to the number of subjects that are there, right. We don't want to have one additional one. So we can take that part away, so don't let that confuse you. Okay and save that. Let's come back and notice that we have six subjects in our database, or at least I do. I'll reload the page and now my position is one through six. Now let me go to products.
Click on Edit Subject, you'll see that it's two. If I go to Test Subject, Edit Subject, it's 6. So we've got that working. Now we need to look at Visible. So visible works the same way but a slightly different syntax. After value, the way we would do it in HTML is we would just write checked if it was checked. And that would indicate to HTML that this radio button ought to be selected. But of course, we don't want to just write that in there. We want to make it dynamic. So let's put a little bit of php in and in that php we'll say if the current subject for visible is equal to 0 since that's the value here, so if it's equal to that value. Then echo checked.
Now, you could put this all on a separate line, or something like that, break it all up. I've just done it as a single quick statement, like this. And then we're going to want the same thing, we're going to copy and paste that here. But instead of visible b being equal to 0, it's equal to 1. So, if it's 0, checked. If it's 1, this'll be checked. Save it. Let's reload our page. There it is. It's marked yes. It is in fact visible. So now we have a form that's ready to edit our subject. We've gone out to the database and done a read to find out the current values and we've pre-populated our form with those values.
Now we should be able to make changes to those and click Edit Subject to submit them. In the next movie we'll see how we process those form values on this exact same page, the editsubject.php page.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.