Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We've now taken a look at three out of the four types of CRUD actions. We have create, we have read and we have update, the last one is delete. And delete is simple by comparison, there's no form or anything like that to fill out. We just simply honor the request. The user says delete it, so we delete it. So we're going to do this on a new page Open up Text Mate and let's just save that as. This is going to be in Sites inside Widget Corp and then inside our Public folder. We're going to call it delete_subject.php.
Alright, so there it is, there's delete_subject.php. And what is this page going to do? Well it's going to need to have the session. It's going to need our database connections. It's going to need our functions. So let's grab those from any of these other pages we were just working with. Open up new subject. At the top here. So let's just grab all of these. It's not going to display any HTML. So we don't need to output anything. We're simply going to process the request and then redirect them somewhere else.
Now I think it's also a good idea to check to see whether it's actually in the database or not. Strictly speaking you don't have to do that. You could just simply say, alright, whatever ID you give me, that's the ID I'm going to try and delete. Alright, but I think it's a better practice to actually go to the database first and do a query, say is it there and if it's there, then we do the delete. So if you remember under edit_ subject.php, we had a bit of code at the beginning here that does the find selected page and then checks to see if it has a current subject or not.
Now we actually don't need to find the selected page because that was really what we were doing for navigation. Instead we can just write our own here, it's not hard to do. We can just say, current subject equals find_subject_by_id. And then we'll pass it in, GET subject. So that's it. That will go to the database and look for it. We have that nice function for us that we can use here. And then, if it's not set then we'll redirect back to manage_content.php.
Now probably it will be set and in which case then we want to perform our delete. So for the delete, let's write our query. I'm going to go ahead and set id equal to current_subject id. It's not a step that's strictly necessary but it just reminds me and reminds you that we do need this id while we're doing the delete. And then our query, just going to be delete from subjects where id is equal to and then we'll use that id there. Limit 1. Now, you could break this up onto multiple lines. It's so short that I'm just going to go ahead and put it all on one single line. And then we know how to do our query result.
I should just jump over here and grab this bit here. And we're going to want mysqli_affected_rows also for delete statements. So lets just go over here. So there we go, so we'll perform the query. The result, if it has a result and the affected rows is equal to one, then we'll know that it succeeded. If not, then it failed. And before we were doing this. If we had a success, that's fine we can go ahead and keep that. And the only thing is we switch to using just a variable for failure. Let's say subject deletion failed and then we'll redirect them back to the manage_content page.
Let's go ahead and put them on the subject that we're looking at. Here we can't do that because the subject doesn't exist anymore. We just deleted it so we can't show them that subject but here we can. So that's it, that's all there is to deleting. We just load in all of our basic stuff, find the subject, make sure that it's there, construct our query, execute the query so that it deletes it. And then check to see did it happen or not and redirect them to a page accordingly. Let's save that, there's one other thing though, which is a question of where do we want to put this delete link. How's the user going to actually delete it.
Now you could put it a lot of places. You could put it just on the page here called manage_content, right? We could put it so that every time we have a subject just like we have edit, we could have another one that says delete. I think because delete is such a destructive action, I like to give it a little extra safeguards to that. You could put it there, certainly. I'm going to put on the edit subject page and I'm just going to come down here to the form for editing subject and right after, we have the edit subject and we have cancel. I'm going to add another one here  .
That's just going to put a non-breaking space. And so right after we have Cancel, let's add another link that's going to be for the deletion. So this is going to be delete_subject and of course, it needs to know what subject it's going to delete. Let's go here, Delete subject. Okay, so which one is it going to delete? php current_subject and id. I need to echo it, don't want to forget that.
That's it. Okay, so now I should have a link to it that's right there. Let's try that out. Let's go over here and let's go to test subject, edit subject, and now I have link here for delete subject. Let's try it, and there it is. Subject updated I didn't want that, I actually wanted subject deleted. Let me just go to my delete page and just change the message, subject updated, subject deleted. But it did delete, you can see that it disappeared. And same thing if we take sample subject, we could make it disappear just as well.
What should happen to those pages when the subject is deleted? In most cases, we probably would not want to have orphaned pages in our database that don't have a subject to belong to. So then we have to make a choice. Do we want to delete these pages automatically whenever their related subject is deleted? Or do we want to disallow deleting a subject until all of the pages below it have been deleted first. I'm going to choose the second one and disallow it. And that'll force the user to remove the pages first. It may not be as convenient but it is the safer option that ensures that we don't delete too much accidentally. Either one is a perfectly valid choice though you could either have it happen automatically or force the user to manually.
Step through each one of those. So let's add that real quick to our delete_subject.php page. Now the place that we want to do this is right after we find the current subject. But before we actually perform the delete. So what we're going to do is pages_set equals and then we know how to find pages for a subject because we wrote a function for it earlier. Right, find_pages_for_subject. And then we just need to pass in a subject ID. Well, let's grab it from right here.
Current subject ID, we'll just paste that out. So that will then return, a page set to us. So it's mysqli_num_rows, or pages set is greater than 0, then we'll know that there are pages left, right? If we count the rows that were returned and it's greater than zero then we know we have some. So what do we want to do in that case? Well, let's just go down here and let's grab our failure message and our redirect, and let's paste those up here.
But instead of saying subject deleted failed, we want to say "Can't delete a subject with pages". That'll be our message and then we'll redirect them to this page. But, we don't have ID yet. ID is something that gets set here. Instead, we just still have current subject ID. So, we'll have to use that there instead. Alright, let's make sure I've got my semicolons all right. Alright, let's test it out. And remember that this redirect_to function will halt execution of the rest of the page.
Because it contains an exit after it sets the headers to the new location. So that will stop it from actually performing the deletion. And just do a redirect instead. So let's try it. Let's go here. Let's just try services. Edit subject. Delete subject. Are we sure? Yes, we are. Nope, can't delete a subject with pages. So now we get our error message instead. So this is an important point. When you have records that are relying on the presence of another record, for example, a parent record like with our subject. Then you will have to decide what you want to do with the dependent records if the parent record gets deleted. So that's all there is to subject deleting.
We've now seen all four types of CRUD. Create, read, update and delete. We've been able to apply them in our application to our subjects.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99158 Viewers
56 Video lessons · 112432 Viewers
71 Video lessons · 81245 Viewers
131 Video lessons · 39016 Viewers
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.