Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
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.
So how did you do with your first big assignment? Hopefully you were successful and you were able to create pages CRUD, that's similar to what we did with the subjects CRUD. Now for any reason, you ran into any problems or you just want to compare notes. All of the code for this is in the exercise files. And I'm going to show it to you now. And if you want to you can pause the movie to see how I did something. Copy it down and get it working for yourself. I only made changes to four files and those are the same four files that I mentioned in the last movie. The first of those is manage content.php and I made two changes. I made one in the manage subject area.
I added a new section where we list off the pages that are in this subject. So do that. It's pretty simple. I used my function that I already had called find pages for subject to get a set of subject pages. And then went through each page in that set. And outputted an li tag. And inside that put a link that would just take me to view that page. Notice that I used URL encoded HTML entities on each one of those. Then underneath that I've got an area where I have a new page link. So new_page.php, and very important, notice that I'm sending with that page the subject ID.
Subject equals and then you only encoding the current subject ID. Now you can call this something else if you want to call ID. Doesn't really matter what you call it but it is very important that you send it because when we create a new page, we can't just create it in isolation. We need to know what page does this subject belong to. And since this is a get request, the only way to do that is to send it in the URL string. I made another change to the page, which is down here on the manage page section. You'll see that I just added a link for editpage.php.
This time I just sent the page that I want to edit. I don't need to send the subject this time because I can deduce that. Once I find the page in the database, I'll know what its subject ID is, it's stored in the table along with it. So that will allow me to edit the page. Alright, so now let's go take a look at the newpage.php. The new page.php, this is very similar to what we did for newsubject.php but that was a two step process before. We had a page that presented the form and a page that processed the form. This is single page submission.
So I took those two pages from before and folded them together to make a single page for creation. And then made the edits that I needed as well. So we make sure that we have a current subject. We make sure that we were able to find a selected page and get the current subject. Because I used subject equals as the query string parameter, I was able to use find selected page because it uses that automatically. So that was nice, it let me do that. And if I don't have a current subject? Well then we'd go to another page. If the form has been submitted, then of course we're going to process the form. I'll just fold that up and see that below that is where we display the form. So let's just take a look at the form first.
It's pretty straightforward. It's what you would expect for creating our page. It's almost identical to what we had for creating our subject with just a couple of small differences. One is of course we have our content area. So that's what I put for the content area, just put a br tag in front of it followed by text area, name content and I gave it rows 20 columns 80 with nothing inside of it. And I also changed my cancel link slightly, so that it'll take me back to that same subject page that I was just on if I decide not to create a new page after all.
The other change I had to make was in the code for position because with the subjects we were finding all subjects. But here I want to find just pages for this subject. So you'll just want to be careful about that. Okay, so that's the form. Let's go up and look at the submission. So I added content to my required fields. I did not give it a max_length. I said it could be as long as it wants. That's fine but it does have to have something. And then notice that when I go to do my creation, I make sure that I have a subject_id, very important, we need that subject_id to create it.
Because that's going to be something we're going to submit now as a value to our table. We need to store the subject_id. In addition I have content and I escape it because it's a string. So we want to make sure that we escape it from MYSQL and I have my mysql_prep function that does that for me. Notice that I put single quotes around the content but I don't put single quotes around subject ID. Because this is going to be an integer and this is going to be a string. And then once it's done, it redirects based on success or failure and has a message that it stores in the session, just like we did before.
So that's it for new page. I'll show you now edit page. Very similar to what we did for editing a subject. If it's been submitted, we do this action otherwise we have the form down here. The form is almost identical to what we were just working with, finding pages for subject. Make sure that page count for editing a page is the exact number of pages that are existing. Whereas when we're creating a new page we add one to that value. If you'll remember we did that with subjects too. And then, of course, now that I have my content, I actually have content displayed.
So I want to make sure that I use HTML entities when I echo that back out into the text area. Alright, let's look at the submission. So here's the actual processing of the form. I don't need to worry about the subject ID this time because I'm not editing it. That's not something that I've decided to make editable. You can't change what subject something belongs to. So I don't even worry about it here. But I do worry about the other values including content. And I make sure that I prep that value and add it to my required fields. And then when I actually do the update, you want to make sure that you add it here as well.
You have to be careful about adding things like commas at the end of one line whenever you add a new line. Otherwise you'll get an error there. And then last of all is delete page.php. Of course, we want to make sure that we have a page. So it checks to make sure that the page is there. And then, of course, it just does a simple deletion. It doesn't matter what the subject was. It doesn't care what the subject was. It's just going to delete the page and send us back to the managedcontent.php page. So that's it. That's all there was to it. It's just those four pages. So if you need to go back and make edits to your own. When you feel like you've got it all working.
Then we're going to be ready to move on to the next chapter.
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.