Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.5 Deleting Data

One of the easiest things to do in a data-driven application is to delete data. You'll learn how in this lesson.

2.5 Deleting Data

In this lesson, we are going to implement the delete functionality. And we're going to do so using the old school deletion process. Which is we will have a link that will take us to a confirmation page, which will then allow the user to either cancel or actually delete the record. The reason why we are taking this approach is because for whatever reason people still disable JavaScript. I don't understand why. And I'm personally of the opinion that if somebody disables JavaScript, then well, they can live in a broken web. But, my opinion isn't always correct. And so we need to supply functionality for everyone including those people. So we first of all need [LAUGH] a link in our table. So I'm gonna add a header cell, not gonna put anything there, but we do need something in the data cell. So, that will be where we will have our link and thanks to bootstrap we can make that link look like a button using the button classes. As far as the URL is concerned, it will be delete.php. And we will have the id query parameter. We will do essentially the same thing that we did with the edit link. And then our text will be simply Delete. Let's see what that looks like. That's huge, but it's there, it works. So we're gonna run with it. And then we need our delete page, which really is going to be a lot like our edit page. So let's take the edit page. We'll just copy that. And we will rename the copy to delete. And of course we need to pull in the delete.code.php. So let's do that. Once again we will just copy that file. And then let's see here. We don't need our form. We still need to check if we have a model, because we still want to display some information, like do you want to delete and then, we could add the person name there. So, let's go ahead and do that. Are you sure you want to delete and then we will have the, I guess last name followed by the first name. So, let's do that, so that we have last name, comma, first name. Then of course, this is a question. So we need a question mark at the end. So we're good there. We still need our hidden input field. But now we need two buttons, one for deleting, one for canceling. So we'll use this one that we have for the delete. Now, we do need this, we need a name here. Let's give that delete and, yeah. Mm-hm, we have some different options here and I'm trying to think of what would be the best way. Because when it comes to this type of form, when we have two buttons It's only going to use the name of the button that we click on. So we will have a Delete button and then we will have a Cancel button. And, if we set these two different values, then all we have to do in the Back-End is just check the value of this choice. That's a horrible name, but, we just have to check that value and then, go from there. The other alternative is to have two different names, and then give them whatever value that we want, the value doesn't matter. So as long as there is a value there for the delete or for the cancel button, then we can check to see what was clicked and then make the appropriate decision there. So I guess let's do that. We'll just have a value once again, the value itself doesn't matter. What we are doing is just making sure that we have something there, in the form data with the name delete, or cancel, and you will see that being done. Now of course the get code is all going to be the same. So let's refresh, we should see Are you sure you want to delete Smith, Jane? Yes, okay. That looks horrible, but it works. So we're gonna run with it for now. So we just need to primarily focus on the post and we still need our id. But now we have the delete, and we have the cancel, and we will change those fields names. And we don't need email here so we can get rid of email. We just need the id, the delete and cancel. We will still check if we have an id. But now let's do this. We will check to see. If we have a value for cancel, and if so, then we will redirect. So, we will redirect, will say return, and we'll be done. Theoretically, that's all that we should have to do. Because then, everything else assumes that we are going to delete. I guess we could, just have an else there, but that's another level of nesting that we need. I try to avoid nesting as much as possible. All right so, let's just we have plenty of names to try to test this. So we'll see. Now the SQL statement for deleting a record is very simple. We want to delete from our people table, where the id, is the id, very simple. All right, yes and yes. Okay, so I guess this is the moment of truth. If we click on Cancel, then everything should be fine. We are redirected. Jane, Smith is still there. So let's delete John, Doe because we don't have an email. We don't have any way to contact him to get his email. So what's the use of having him? So let's delete him. And he's gone. For whatever reason, I don't like Jim. So but yeah, Jim, I do email every once in a while. So let's cancel. Okay, so, our functionality works. We have the ability to add, which we should add a link somewhere. I guess we could do that very quickly. Doesn't have to look pretty, just that we have a link there. So we'll have a div and inside of that div, we will have our link which will look like a button. We'll make this primary, and that will be add.php and then Add Contact. So there we go let's refresh. Once again it doesn't look pretty but it doesn't matter. We have functionality there we can always come back and make it look pretty. So we can Add people, we can edit people, and we can Delete people. So we have a fully functional contact book. It's not perfect. And there are certainly things that we need to address in the code. And we will do that in the next lesson.

Back to the top