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.
In the last movie, we created our edit subject form, and we populated it with the values from the current subject. Now, we're ready to see how we process that form. And we're going to do it a little bit differently than we did it last time, because we're going to do the processing on the same page. The form is going to submit to itself. Let's take a look now. Edit subject.php is what we were working with. And let's see where the form in going to submit right now. Right now, it's still submitting to create_subject.php. So we want to change that. So now that it submits to edit_subject.php. And very important. If we're going to update the subject, we need to know what subject we're updating. So we need to just put a little bit here to say subject equals and then the subject id.
Just like we did when we were going to this page. We had a link that went to the edit subject page, and we need to keep track of that subject id all the time. We could put it in our form. We could make a hidden field in the form. Some people like to do that. I prefer to have it in the URL. And that's going to be important, because this page is going to serve a dual purpose, right? It's going to work both ways. So now, up here, we want to do our form processing. So that's going to take place after we make sure that we have a current subject, before we start displaying the form, we're going to do our form processing. Let's, as a starting point, let's grab create subject.
And let's grab what's in here. And let's, we know we're going to need validation functions. That's going to be an addition. Let's add that to the top. And then, let's grab all of this processing that we did up here, all the way down to the bottom. We don't need to close the connection. But everything else before that. We'll copy that. And we're just going to come in here and paste it right there. Now we can go through and start editing it to make it work for our new situation. This is still the same, if it's a submit, then we want to process the form. Don't worry about what it does right now inside there. Let's jump down here, and if it's not, before, we said, well, this is probably a get request, if that was the case, and we redirected them. We don't want to redirect them back to the form. The form is right here below.
So we'll just say well if it wasn't a post, if it wasn't a proper post at least, then we're going to go ahead and redisplay the form again. Right? That works well. Now let's go back up here and take a look again. Processing the form works fine, we've got our validations, I'm actually just going to move these below the validations.. And we'll do is set. There we go. So if it's not empty errors, we were redirecting before back to new subject. We're not going to do that anymore because our page is right below here.
So instead, let's say if errors are empty, then in that case, we want to do all of this form processing. Right. When loaded, validations pass, so we'll perform the updates. So if errors are empty. Then, perform update. And I'm just going to take this final curly quote and move it down here to the end. And then, let's just take all of this, and I can just indent it. There we go. So, if errors are empty, perform the update.
There's one additional field that we're going to need for our update, and that's the ID, right? And we could just use the current subject ID. I'm going to go ahead and just set a new variable, current subject ID. And that's mostly just to remind you that that we need this value, right? Because we need to submit that when we write our update statement. Now, the query that we'll do is different. We're not going to be doing an insert. Instead we're going to be doing an update. What does that look like? Well, you can go back and look at database update in your sandbox if you want a reminder.
I'm just going to paste it in here. Update subjects, set menu name equal to the menu name, position equal to the position, visible equals visible. Make sure you've got spaces at the end of these lines if you're in fact building up this query this way, where id equals, and here's the id. I also added another line, which we hadn't done before, which is called LIMIT 1. Like some of the other examples we've been looking at, it's a good idea if we want to make sure that it's only gona update one subject, that we provide LIMIT 1. It just sort of lets me know that it's only going to do one, it's a safety net for us. So then the query will take place.
Remember though, that we don't just check for the result anymore, we also check to see, did it change something. So mysqli_affected_rose is going to tell us that. It takes the connection as an argument. And if that's equal to one, if we did in fact affect one row, then we'll know that is was success. If was success, let's go ahead and keep this. Let's go ahead and use session, and we'll put in subject created and redirected them to managecontent.php. Just going to go back up here and make sure that I have my session included, I do.
And then let's come back down again. If it's failure though, we're not going to have this session message Instead, we'll just set a plain ol' message. Message equals, subject created, failed. Now, why do that? Well because once we get down here we have that message available to us. We don't have to call this function that retrieves it from our session anymore. Instead we can do something like this. Right, if the message is not empty. Then display the message. Right there, we don't have to pull from the session. And I put a little note here.
Just to remind you of that fact. Now could you put it in the session? You could, the session file is there, and you could read to it and write from it anytime you want. So you absolutely could still put it in the session and then read it back from there. If you wanted to use that same function that we had before. I just wanted to just show you that you don't need to, that we can actually just pass things straight down the page to our form because we're working on the exact same page. Now for our errors, we can display these errors, but we don't need to retrieve them from the session either. So we can just take that away and errors is now going to contain everything we need, without ever going to the session. One thing I'll tell you about it that's sort of nice is that sometimes, after a closing curly brace, like this, you lose track of what they are. You might want to just put, you know, this is end, and then what is it the end of? You come up here and just grab this bit of code, come down here and just paste it in.
Now whenever I see that, I'll know that this curly brace is the ending of if is set post command. So you might want to make a few kinds of those posted notes for yourself. Alright, let's save it, and let's try it all out. Let's go over to Firefox, and here I'm on my edit subjects page, so to begin with, let's go to About Widget Corp. Edit subject, and I'm going to just make it something different. I'll take away the P. I'll click Edit Subject Subject oops subject created, that's the wrong message I forgot to do that, that's going to be subject updated subject updated failed but it did work.
You see here about widget core change click on it we'll go edit subject and now it worked. I'll do it one more time.. Subject updated and it did change here. Now you could go back to this page if you wanted, if that made more sense to you instead of going back to a default page. Let's try setting a postion. Let's try setting position equal to five. Let's edit subject. Look at that. It dropped down here to position number five. We go back, change it from five back to one. Edit subject and now it's back up here at the top.
Now let's try to just get some errors. Let's take it away, let's have no menu name at all. And it's subject. Nope, menu name can't be blank. So we still have our validations, and you'll notice that we're on the same page. It rendered the same page to us immediately afterwards. So that's all there is to it, to this idea of single-page submission, or having two pages for submission. You can do it either way that you'd like. It's really a matter of personal style and preference, whether you want to have it broken up, or whether you want to combine them together. You should try both of them and see which one feels more natural to you.
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.