‫All right.
‫So now we want to be able to update the data of a post.
‫And there's basically two parts to this.
‫One is the edit page which will be the form similar to the create form.
‫But it'll fetch that post and it'll put the data in the form for us to edit.
‫Second will be the actual update where we submit that form and update the database.
‫Now I want to have a button that goes to edit dot PHP.
‫So this is a page that you can create.
‫And the sample content is in the starter starter code for this lesson.
‫And it's basically the same exact thing as the create form, except it says update blog post and there's
‫no PHP.
‫We're going to add that after now in the post dot php where we have our delete button, I want to add
‫an edit button above it, but before I do that, I'm going to make the button that's in the delete form.
‫I'm going to make that full.
‫So I want it to to be 100% width.
‫So we're going to add width full or dash full.
‫And that will make it go all the way across.
‫Now above the delete I'm going to add my edit button and say edit button.
‫Okay.
‫And this is going to be a link.
‫Actually, it's not actually a button because it's going to go to that edit dot PHP, but we want to
‫format it to look like a button.
‫So let's say RF edit dot PHP.
‫And we also want to send the ID along.
‫So we're going to do id equals.
‫And we can use our shorthand syntax and get the post id.
‫Okay.
‫So that will send the ID and we'll say edit for the text.
‫And then I just want to add a bunch of classes in here as well.
‫So we're going to do BG green 500 and let's say text white let's say padding.
‫So P four pi two and then rounded I'm going to make it block level because a link or an a tag is inline
‫by default.
‫And then w full.
‫And text center.
‫Let's add a little bit of margin on the bottom.
‫So m four and then on the hover state we'll do BG green dash 600.
‫And on the focus state we'll do outline none.
‫All right.
‫So let's save that reload.
‫Now we have an edit button.
‫So if I click that it takes me to the edit form.
‫And if you look in the URL we see id equals three.
‫So we can use that in our query.
‫Now before we can think about submitting this form we actually need the data right.
‫We need the three blog three or post three data.
‫So we can pretty much copy what we have in post dot PHP because we're just fetching that specific data.
‫So let's do that.
‫Let's grab what we have in post dot PHP and let's go to edit dot PHP.
‫And at the top.
‫We'll go ahead and paste that in.
‫Make sure you add your ending tag here.
‫And there's not really anything we have to change.
‫We're getting the ID from the URL, we're getting the data.
‫We'll put it in this post variable.
‫And then what we can do is just come down here into the form.
‫And for the input for the title.
‫Let's add a value.
‫And we'll set that value to the post title.
‫Okay.
‫And then for the the body which is a text area, we'll go in here and let's set the set that as the
‫post body for that particular post.
‫And if I reload now we get post three.
‫If I go back if I go to post two and edit there we go.
‫All right, so that's working.
‫Now we want to do the submit.
‫And we're going to do something similar to what we did with the delete.
‫Because when you when you update something it's usually a put request or a patch.
‫So we're going to add a hidden method just like we did with the delete.
‫But it's going to be of the type put so in the form in the edit form or update form, let's go to where
‫the form opens.
‫And let's add an input with the type of hidden.
‫And for the name we're going to do underscore method, just like we did with the other one.
‫But we're going to set that to put instead of oops instead of delete.
‫Okay.
‫And then I'll copy that down because we also need the ID.
‫So for the name.
‫Let's say ID, and then for the value we can say post and pass in ID.
‫Okay, so now we have those two hidden fields.
‫All right.
‫Now where we want this to submit to, you could do a couple of things.
‫You could create a separate update PHP file and do it there.
‫But what I'm going to do is submit it to this same file.
‫So I'm not going to add any action.
‫And then we'll just check to see if it's a put request.
‫So up here let's see where do we want to go up here.
‫Um.
‫Yeah, we'll just go after everything here because this is all just fetching.
‫You could even put this in a separate function, but we'll just we'll just leave it as it is and let's
‫say check for.
‫Form submit and I'm going to check for that.
‫That put.
‫So I'm going to say is put request.
‫So same thing we did with the delete.
‫And we'll set that to underscore server.
‫And let's say request underscore method.
‫So if the request method is equal to post.
‫Which it is.
‫But we also want to check for that, that hidden field.
‫So we'll say and.
‫Um, and then we'll open up some parentheses and say post.
‫We want to check for underscore method, and we want to make sure that that's there as well.
‫So I'm going to say.
‫And use the null coalescing and then use some just an empty string and we'll say if that is equal to
‫put.
‫Yeah, that should do it.
‫And then we'll check for that.
‫So let's say if.
‫Is put request.
‫Then let's get the title.
‫I'll set that.
‫We'll use HTML special chars and we'll get post.
‫Title.
‫And we've got to make sure it's there, so we'll add on that.
‫And then we'll do the same for the body.
‫Potty.
‫All right, then we'll create our query.
‫So this will be set to update posts and we want to set the title equal to a placeholder.
‫Body is going to be equal to placeholder.
‫And we want to say where the ID is equal to a placeholder.
‫All right.
‫So that's the update query.
‫Now let's create the statement stmt and set that to our PDO instance and prepare method.
‫We want to pass in our query.
‫Then we want to set our params so we have three params to set this time.
‫So first we have the.
‫Let's say let's do it in the same order.
‫So title.
‫Title.
‫Then we have the body.
‫Set that to body and then we have the ID.
‫ID okay, now we need to execute.
‫So we'll take our statement and we'll call execute.
‫And we'll pass in params and then we'll redirect.
‫So header.
‫Location is going to go.
‫Index dot PHP and then we'll exit.
‫And that should do it.
‫So let's try it out.
‫I'll say this is actually let's just reload this.
‫So this is updated post two and I'll just say post two update.
‫So let's submit.
‫Okay, so nothing happened.
‫Let's go back to posts.
‫Okay, so it looks like this wasn't fired off.
‫So server request method equals post.
‫If we look in the form, we have method post, and then we have the input of method set to put.
‫So up here.
‫Are checking to see if post.
‫Oh, I put all caps here.
‫That's I think that's what's wrong.
‫All right.
‫So we'll do this again.
‫Let's say updated post to submit.
‫There we go.
‫So now updated post two.
‫So now we're able to create read update and delete blog posts.
‫So this is kind of the fundamentals of working with PDO.
‫And working with a database doesn't have to be MySQL.
‫You could use Postgres.
‫All you're going to change if you use Postgres is in your database config.
‫You're going to change the driver.
‫This right here, that's pretty much it.
‫You might have some other credentials, but that's pretty much it.
‫All this stuff is going to still work.
‫All right, so now that we have gone over all, I wouldn't say all.
‫I mean, it's hard to get all of the PHP fundamentals, but most of the ones that you're going to use
‫and we've looked at, you know, super globals and how to work with the database.
‫Now, I think that it's time that we jump into an actual project and we start to look at creating creating
‫a custom router.
‫We start to look at creating middleware and dealing with different Http status codes and things like
‫that.
‫So we're going to kick it up a notch.
‫All right.
‫So I will see you in the next section.