Membership
Main Menu
Forum Boards
Stats
- 20 tutorials
- 74,815 members
- 734,916 forum posts
- 13 blog posts
Tutorials
PHP Basic Database Handling
Views: 67592
Dealing With The Database: UPDATE
In this chunk of code, we will update names in the database, if applicable.
02. First we check to see if there are any names to update. If there are, then proceed to the next line of code.
04. The next thing we do is make a foreach loop, to cycle through each name. Down in our form, we made the name's id the key, so that we could pass both id and name at the same time without having to get all fancy about it. So, for each iteration of the foreach loop, $cid will be the current id and $cname will be the current name.
06. & 07. We go ahead and sanitize the variables just like we did before.
09. Here is our update query. We use $name and $id in place of a hard coded name/id, because it will change each iteration of the foreach loop.
10. We send off the query string to be executed, just like before. Again, in this instance, we don't really need to assign it to a variable, since we aren't doing anything with the result anyway.
The foreach loop will cycle through all the current names in the list, updating them in the database one at a time. Now, this isn't very efficient, as there's no reason for us to update something that doesn't need updating. Ideally you should write your script to only update it if it's actually changed. But again, writing efficient code isn't necessarily the goal of this tutorial.
