Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Updating Data With a Form: Retrieving the Record

There are two steps involved with updating data. The first thing we need is to retrieve a specific record from the database to display in our form. We'll write that functionality in this lesson.

2.3 Updating Data With a Form: Retrieving the Record

I've often said that editing and updating is a lot like creating because we essentially work with the same information. We just do slightly different things with that information. Like for example, an edit page looks a lot like the Add Page, we have a first name, a last name and an email address. The only difference might be the button text will be different. And there might be some hidden things behind the scenes. But for the most part, it's the same information. It's really what we do on the back end or inside of our PHP code. That makes the difference and it's just basically calling a different SQL command and that's it. So the first thing I want to do is modify our table. I want to combine the first name and last name so that they are in the same column. So that we can have just a single link that we have to click on. In order to go to the Edit page, so I'm still going to have it last name and first name, but there will just be a comma separating the two. And then we need our link. So let's go ahead and add that. Now we need to know what user we click on. And the only way that we can do that is by using the ID. That is the The whole purpose of having a primary key is so that we can identify a single record in the database. So we're going to go to Edit.php and we're going to pass that ID, through the query string we can call it ID. We can call it whatever we want person ID entry ID. But Id is just nice and simple. So, we will use that and then we just need to output the ID from our model item. And there we go. So if we look at this in the browser, we should now have our links and they point to the correct place. So if we click on this, we of course, see that the objects not found it's a 404 because that edit dot PHP does not exist. So let's create a new file. And let's just copy Everything that we have inside of add.php and I guess we could have just copied add dot php to begin with but this is going to be just fine as well. Let's go ahead and change the file that we are requiring we will still follow that same pattern of having our, Processing code inside of edit code dot php inside of the app folder. Let's also go ahead and start our PHP block there. So that's now if we refresh we have our form. So now we need to retrieve the user With the given ID, which is four in this case. And so this means that we are going to have to do what we did for the add page. And that is we need to determine what type of request that we are working with. Because if it is a get request, then we are going to retrieve the data from the database and then display that. So let's handle that First, if it's a get request, then we of course want to query the database. And for that we need our PDO object. So let's just copy that. Now we need this PDO object regardless of what happens if it's a get request, then we want to retrieve the data If it's a POST request, then we want to update the data. So we need our PDO. Here, it's just a matter of what we do with that PDO is going to be the main difference. Let's also go ahead and copy the code that is going to clean up our PDO objects and in fact, let's go ahead Copied the redirect as well because after we are done editing, we can just go back to the Index page. Alright, so we have a get request, we need to get the value from the ID query parameter. So we can use the filter input function that we used for adding, but in this case We are going to say that we are going to look for a get value. It's called ID, and the ID is an integer value. And if it's not an integer value, then we don't want to do anything with it. So we can use that one of the validation filters. If it is an INT, then ID is going to contain that int value. If it's not, then it's going to be empty. So we could go ahead and say, if not. Id, then well what could we do? We could just echo not found. And while that would work that would look kind of weird in the page, but let's just run with that, and then we can return there. Otherwise we have an ID, we want to use that to query the database. So let's use the database query method. Our query is going to look a lot like the previous lesson select all from people. But now we need to specify that we have this ID. So we want to select Everything from people where the ID is equal to our given ID. But really we don't need to use query here because we are making a prepared statement. So we will use the prepare method and we need to execute that prepared statement and We will pass in the value for our ID. But since this is a query, we want to fetch data from the database. So we will then use the fetch method now. If you'll remember from the previous lesson, we used the fetch all method. We can look at that code very quickly. There it is right there. That's because we were fetching all of the records in the database in this particular case. We only have one record. If that I mean the database is not going to allow multiple records with the same ID. So we can call just fetch to fetch the single Or no record. If that is the case, stored in model. And then we are good to go. Now, if we don't have a record, like for example if the ID that was supplied does not exist in the database, then model is going to be no. And you know what, we can Change this just ever so slightly so that we can set model to know if we don't have an ID we will set model to know. And then inside of our view we will check to see if model is no If it is, then we will say that it's not found. Otherwise we will display our form. So that should work rather well. And you know what, let's comment out this header because that would be a problem that would execute regardless of what happens right now. So Let's go to the browser let's refresh. Well, we don't need to do that we need to go to our edit view. And we need to add the code for checking if our model is there or not. So, we will use our handy if statement and we will check if model is no. If it is then we will simply say. Not found, so we can go ahead and do that. But if we do have a model then we of course want to display our form and display the data in that form. But let's go ahead and finish this if else statement and then we will add in Our form data. So we still have our model here and we are simply going to add in the value attributes for these fields and we will grab The value from our model, it's not item anymore it's model. So this is the first name, we will grab that value. And then we will do the same thing for Everything else now, we do need some way of keeping track of what Id we have. And yes, we technically have that inside of the URL, but I like to have that actually inside of the form. So we can add a hidden field for that. And in fact, let's just go ahead Let's do that it doesn't matter where we put this just as long as it is inside of the form. So we will have our input the type will be hidden. Let's give this a name of ID and then of course Of course we need the value. Although, let's not do this let's have person ID, and then we will have our value sent to the ID from our model, because we will have that as well. And there we go. So if we go to the browser now and we refresh the page, we should see That information is automatically populated. If we view the source, then we should see this input hidden field with an ID of 4. So we are all good there but let's also test what happens if we have an ID that is out of range and we can see that is not found. So we are halfway through editing our record. In the next lesson, we are going to write the code that will actually edit the record.

Back to the top