‫Now we're going to look at deleting posts from our little application here.
‫Now this is a it can be a little confusing because from the browser within PHP or I should say within
‫our HTML, we can only make post and get requests right.
‫We can create a form and we can say method equals post, method equals get.
‫We can't say method equals delete.
‫So we need to kind of spoof this a little bit and send a hidden value saying, well we really want to
‫make a delete request.
‫All right.
‫So let me show you how to do that.
‫We're going to go to our post dot PHP which is our single post page.
‫So let's say we go to this post for and I want to have a delete button down at the bottom.
‫So down here.
‫Let's see.
‫We want to be within the container.
‫And let's see.
‫Are we going to go?
‫Forget where exactly I want to put this.
‫Let's go right under this, this rounded div which ends right here.
‫So we should have two divs underneath.
‫And we're going to create our delete.
‫It's really a delete form because we need to be able to send a request.
‫And we can do that through a form.
‫We can't have a link.
‫So let's create a form.
‫And I'm actually going to give this.
‫Well, first of all, I'm going to give it an action of delete dot php.
‫I'm going to send this to a different file to do the actual delete.
‫And for the method I am going to I'm going to put post here.
‫Okay I can't do this.
‫I can't say delete, even though ultimately I want to make an Http delete request, but I can't do it
‫that way.
‫So I'm going to say post.
‫But then what I'm going to do is have an input with the type of hidden.
‫And for the name we're going to put underscore method okay.
‫That's the name.
‫And then for the value we're going to say close that up.
‫We're going to say delete.
‫And then what we can do is in the script where we basically catch this, we can check this value this
‫method.
‫And if it's delete then it'll be we'll make it a we'll accept it as a delete request.
‫Now we also need the ID, we need to send that along and we're not doing a link.
‫So it's not like we can pass in a query param.
‫So we're going to have another hidden input.
‫And for the name we'll have id and for the value.
‫Well, go ahead and just output.
‫I'll use a shorthand syntax and say post and then the id.
‫All right.
‫So that will send that.
‫Then we want the actual button, because right now we just have two hidden inputs.
‫So let's say button and we'll give it.
‫We'll give that a type of submit.
‫And I'm just going to add some other stuff here.
‫We want to name of submit.
‫And we're going to add some classes here as well.
‫Let me just move this up a little bit.
‫So for the class let's do BG dash red dash 500.
‫Since it's a delete button we'll give it a class of text dash white.
‫Let's do p dash for P-2.
‫And then what else?
‫Rounded?
‫On hover.
‫So on the hover state we'll change it to BG Red 600.
‫I know this stuff doesn't really matter guys, but I like it.
‫I like things to look at least decent.
‫I mean, technically we didn't have to do any CSS for any of this, but it's just super ugly if you
‫don't.
‫And we'll just say delete for the text.
‫So now let's reload the page and we should see a delete button.
‫Actually let's push it down a little bit.
‫So.
‫Let's see for this.
‫This wraps the whole thing right?
‫Yeah.
‫So this right here the rounded LG I'm going to add me.
‫Let's say m ten.
‫That's a little too much.
‫We'll do maybe eight.
‫Of course you can change it to whatever you like.
‫It's all subjective.
‫So now if I click that, it actually sends a Post request to delete PHP, which doesn't exist.
‫So let's create that.
‫Delete dot php.
‫Now when we create our application it's going to be different.
‫We're going to have we're going to have classes.
‫We're going to have routes that map to specific class methods.
‫And it'll just be much more organized than having all these separate files for everything.
‫This is just to get you to get you to understand how to use PDO and work with the database.
‫So let's see in the delete we're going to.
‫First thing I'm going to do before I forget again is require the database.
‫Then let's say is delete request.
‫And I want to check two things.
‫I want to check to see if the server request method is post, which it is, because we're submitting
‫the form with the method post.
‫But I also want to check for that underscore method, that hidden field and see if that's equal to delete.
‫So let's say.
‫Server and we want to check the request method.
‫Underscore method.
‫And say if that is equal to post.
‫And let's say if the post.
‫Um.
‫Yeah.
‫We want to say if the post underscore method is equal to to delete.
‫All right.
‫So this is delete requests will be either true or false.
‫So let's let's just do a var dump.
‫Of is delete request.
‫So first of all, I'll just go to the page.
‫Right.
‫So if I just go to slash delete dot php.
‫We get false.
‫So it's not the delete request.
‫If I, I just want to test this out.
‫So if we don't have this hidden input here.
‫So if I reload and submit is that.
‫Uh oh.
‫So basically, what's going on here is.
‫Method isn't included here.
‫So we're getting this undefined array key.
‫So we have to remember when you use a post or get value you need to check for it first.
‫So what we could do to kind of keep this structure is wrap this.
‫Wait a minute.
‫Yeah, we could wrap this in parentheses.
‫And then.
‫After this method right here.
‫After the brackets, we can use the null coalescing operator.
‫And I believe we should be able to do that.
‫There we go.
‫Yeah.
‫And we get false because method is not equal to delete.
‫Now if I uncomment this and then I come back and reload and hit delete.
‫Now I get true.
‫So that's where we want to delete.
‫We actually want to do the delete.
‫So here let's say if.
‫Is.
‫Um, I forgot my.
‫So if is delete request then let's get the ID.
‫So we'll say ID equals and let's say post.
‫Because remember we're sending the ID and the hidden input as well.
‫So we want to get that.
‫Then we'll create our query.
‫So let's say delete.
‫From posts where the ID is equal to a placeholder of ID again, you don't want to put that just that
‫money sign ID here, you want to use the placeholder and then you're going to use a prepared statement.
‫So stmt and set that to PDO.
‫And we want to prepare pass in our query.
‫Okay.
‫Then we want to add our params array.
‫And we want to say where ID we're going to use the ID variable.
‫Then we want to take our statement and we want to execute and we want to pass in the params array.
‫Okay.
‫And then after that we'll just redirect.
‫So let's say header.
‫Location.
‫Go back to index, PHP and exit.
‫So that's our delete.
‫All right.
‫Let's try it out.
‫We'll come back here I'm going to reload.
‫And remember this is post four.
‫So I'll click delete and we get redirected.
‫And you'll see post four is now gone.
‫And you can add confirmations through JavaScript or whatever you want.
‫You can add success messages.
‫We're going to do that stuff in the real project.
‫But that's how we can delete.
‫You can create a form.
‫And of course you could do this through JavaScript, you know, making an Http request using the fetch
‫API.
‫But this course is strictly PHP.
‫I don't want to get into any JavaScript.
‫So this is how you can do it with straight PHP.
‫Of course frameworks also make this easier.
‫Laravel and so on.
‫All right, now the last video in this particular section is to update.
‫We're going to be able to go to one of these posts and update the title and body.