Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating an edit subject form

From: PHP with MySQL Essential Training

Video: Creating an edit subject form

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.

Creating an edit subject form

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.

Show transcript

This video is part of

Image for PHP with MySQL Essential Training
PHP with MySQL Essential Training

131 video lessons · 34457 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 4m 8s
    1. Welcome
      1m 0s
    2. Using the exercise files
      3m 8s
  2. 15m 6s
    1. What is PHP?
      3m 52s
    2. The history of PHP
      2m 51s
    3. Why choose PHP?
      4m 10s
    4. Installation overview
      4m 13s
  3. 54m 53s
    1. Overview
      2m 33s
    2. Working with Apache Web Server
      6m 56s
    3. Changing the document root
      7m 24s
    4. Enabling PHP
      6m 16s
    5. Upgrading PHP
      3m 30s
    6. Configuring PHP
      10m 3s
    7. Installing MySQL
      5m 46s
    8. Configuring MySQL
      7m 24s
    9. Text editor
      5m 1s
  4. 31m 25s
    1. Overview
      3m 27s
    2. Installing WampServer
      5m 46s
    3. Finding the document root
      2m 24s
    4. Configuring PHP
      8m 12s
    5. Configuring MySQL
      5m 45s
    6. Text editor
      5m 51s
  5. 19m 12s
    1. Embedding PHP code on a page
      6m 43s
    2. Outputting dynamic text
      5m 55s
    3. The operational trail
      2m 27s
    4. Inserting code comments
      4m 7s
  6. 1h 18m
    1. Variables
      7m 50s
    2. Strings
      4m 38s
    3. String functions
      8m 54s
    4. Numbers part one: Integers
      6m 27s
    5. Numbers part two: Floating points
      5m 25s
    6. Arrays
      10m 0s
    7. Associative arrays
      6m 37s
    8. Array functions
      6m 33s
    9. Booleans
      3m 50s
    10. NULL and empty
      5m 15s
    11. Type juggling and casting
      8m 27s
    12. Constants
      4m 43s
  7. 27m 37s
    1. If statements
      6m 0s
    2. Else and elseif statements
      4m 16s
    3. Logical operators
      7m 30s
    4. Switch statements
      9m 51s
  8. 42m 15s
    1. While loops
      8m 41s
    2. For loops
      5m 59s
    3. Foreach loops
      8m 16s
    4. Continue
      8m 28s
    5. Break
      4m 8s
    6. Understanding array pointers
      6m 43s
  9. 37m 25s
    1. Defining functions
      8m 25s
    2. Function arguments
      5m 32s
    3. Returning values from a function
      7m 33s
    4. Multiple return values
      4m 53s
    5. Scope and global variables
      6m 2s
    6. Setting default argument values
      5m 0s
  10. 20m 18s
    1. Common problems
      3m 47s
    2. Warnings and errors
      8m 36s
    3. Debugging and troubleshooting
      7m 55s
  11. 57m 57s
    1. Links and URLs
      5m 33s
    2. Using GET values
      5m 35s
    3. Encoding GET values
      8m 41s
    4. Encoding for HTML
      9m 26s
    5. Including and requiring files
      7m 40s
    6. Modifying headers
      6m 45s
    7. Page redirection
      6m 43s
    8. Output buffering
      7m 34s
  12. 1h 3m
    1. Building forms
      7m 28s
    2. Detecting form submissions
      5m 59s
    3. Single-page form processing
      7m 57s
    4. Validating form values
      10m 40s
    5. Problems with validation logic
      9m 54s
    6. Displaying validation errors
      7m 23s
    7. Custom validation functions
      6m 28s
    8. Single-page form with validations
      7m 25s
  13. 28m 5s
    1. Working with cookies
      2m 49s
    2. Setting cookie values
      5m 55s
    3. Reading cookie values
      6m 1s
    4. Unsetting cookie values
      4m 51s
    5. Working with sessions
      8m 29s
  14. 48m 39s
    1. MySQL introduction
      6m 43s
    2. Creating a database
      7m 41s
    3. Creating a database table
      7m 42s
    4. CRUD in MySQL
      5m 48s
    5. Populating a MySQL database
      7m 32s
    6. Relational database tables
      6m 40s
    7. Populating the relational table
      6m 33s
  15. 56m 4s
    1. Database APIs in PHP
      4m 51s
    2. Connecting to MySQL with PHP
      7m 45s
    3. Retrieving data from MySQL
      8m 47s
    4. Working with retrieved data
      6m 12s
    5. Creating records with PHP
      6m 58s
    6. Updating and deleting records with PHP
      9m 6s
    7. SQL injection
      3m 5s
    8. Escaping strings for MySQL
      6m 45s
    9. Introducing prepared statements
      2m 35s
  16. 35m 58s
    1. Blueprinting the application
      7m 19s
    2. Building the CMS database
      5m 14s
    3. Establishing your work area
      4m 38s
    4. Creating and styling the first page
      4m 22s
    5. Making page assets reusable
      6m 36s
    6. Connecting the application to the database
      7m 49s
  17. 32m 49s
    1. Adding pages to the navigation subjects
      5m 58s
    2. Refactoring the navigation
      6m 7s
    3. Selecting pages from the navigation
      6m 2s
    4. Highlighting the current page
      5m 26s
    5. Moving the navigation to a function
      9m 16s
  18. 1h 45m
    1. Finding a subject in the database
      9m 48s
    2. Refactoring the page selection
      10m 52s
    3. Creating a new subject form
      6m 55s
    4. Processing form values and adding subjects
      11m 20s
    5. Passing data in the session
      9m 16s
    6. Validating form values
      9m 40s
    7. Creating an edit subject form
      8m 30s
    8. Using single-page submission
      7m 44s
    9. Deleting a subject
      9m 44s
    10. Cleaning up
      10m 37s
    11. Assignment: Pages CRUD
      4m 30s
    12. Assignment results: Pages CRUD
      6m 10s
  19. 39m 26s
    1. The public appearance
      8m 52s
    2. Using a context for conditional code
      11m 37s
    3. Adding a default subject behavior
      6m 9s
    4. The public content area
      5m 51s
    5. Protecting page visibility
      6m 57s
  20. 1h 3m
    1. User authentication overview
      4m 3s
    2. Admin CRUD
      8m 41s
    3. Encrypting passwords
      7m 26s
    4. Salting passwords
      5m 42s
    5. Adding password encryption to CMS
      11m 54s
    6. New PHP password functions
      3m 13s
    7. Creating a login system
      11m 28s
    8. Checking for authorization
      5m 48s
    9. Creating a logout page
      5m 40s
  21. 2m 4s
    1. Next steps
      2m 4s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed PHP with MySQL Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.