- Overview
- Transcript
2.4 Updating Data With a Form: Writing the New Record
The final step in updating data is actually updating the database record. We'll implement that code in this lesson.
1.Introduction2 lessons, 11:07
1.1Introduction01:23
1.2What You Need09:44
2.Working With Data6 lessons, 56:43
2.1Creating Records (Inserting Data)13:20
2.2Reading Data08:10
2.3Updating Data With a Form: Retrieving the Record09:39
2.4Updating Data With a Form: Writing the New Record05:52
2.5Deleting Data07:47
2.6Refactoring for Clarity and Security11:55
3.Conclusion1 lesson, 00:56
3.1Conclusion00:56
2.4 Updating Data With a Form: Writing the New Record
In the previous lesson, we handled the first portion of editing a record that is using the get request to fetch data from the database so that we can then display that data in our form. So now we just need to actually edit that data, that is going to be the post request. So let's just jump on in and handle that code. So we will add an else here. And we first of all want to grab the information from the form. And we already have that code inside of our Add File. So let's grab that so that we get the first name, last name and email. And we also need to do the same thing for the id. Now, yes we could use the id that's in the URL. However, I want to rely strictly upon the data that is coming from the request. So, we want to grab that from that hidden field that we added to our form. So this means that we need to change the INPUT_GET to INPUT_POST. The name of this field is id-person, no, it's person-id, that doesn't make sense, id-person. But we still want to validate that integer value. So, we have our values. The next thing that we need to do is prepare a SQL statement. So let's go ahead and let's copy that. And we aren't selecting data here, instead, we are updating the people table and we want to set the first_name equal to our first_name value, the last_name equal to our last_name value. And then finally, the email should be equal to our email value but but, we need a WHERE clause. If we left this SQL statement as is, it would update every record in the database and it would set the first_name to our first_name value last_name to last_name, email to email. We don't want that, we want to update just the record where the id is equal to our id value. And then of course, we want to execute that statement. We don't need to fetch anything because there's nothing to fetch, this is a statement where we execute. And we also need to supply the first name, last name and email values which is also inside of our add file. So let's grab that code so that we can then paste that into our edit code. And there we go. So that should be fine. Although I guess we should make sure that our id is valid. And how do we want to go about doing that? I guess we could do this, kinda what we did in the get request. If we don't have an id then we will just go back in the browser and say that that was not found. Okay, so we have all of that in place. We are closing the statement and the database object. But then in the case of editing, we want to redirect to the index. And we could do this in a couple of different ways, we could do this so that in both the if and the else we set the statement and DB to null. So we would have that up here, except that in the else, then we would set the location header. That's not what I want to do because, well, that's just, no, I don't want to do that. So instead one option would be to once again, check to see if we have a post request here. And I guess that that's okay. I mean, it doesn't necessarily look the best, but yeah, this will be okay. So if this is a post request, then we will set the location header to index.php, otherwise, we are fine. Now I guess, we could come up here inside of the if and we could have a return statement, but to me there's really not a great way of going about it. This is probably the cleanest as far as if we were reading the code. So we'll just go with that. I'm not happy about it but that's the thing about running software. You don't have to be happy about your code all the time. One thing that I do want is let's change the text here. And let me get back on that, to me code that works is much more important than the most beautiful clean code. As long as it is understandable and it works, that's it for me, but that's my mindset. So let's change some of these values so that we can be sure that we are updating them. So I'm just adding some values, and we're gonna click on Save. Hopefully it's gonna redirect us, it does, we can see that our data is updated because our name inside of the list changed. So once again, if we change all of this information then everything should be fine. So we have now successfully updated our records in the database. So now we just need to implement the delete functionality, and we will do that in the next lesson.