Tutorials

PHP Basic Database Handling

by Crayon Violent on Jun 20, 2008 2:06:06 PM

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.

Comments

This is an incredible tutorial, i knew how to handle databases but it was so goood to recap, plus a few little things that i learned, and you have a nice way to explain. I had a great time reading it. Thanks :D

1. fearlex on Jun 27, 2008 3:54:27 PM

We're using ODBC-connect, ODBC_exec, etc. How do I create a new record using ODBC? I don't imagine I can do it with mysql commands?

2. peteschulte on Jul 23, 2008 6:36:06 PM

Well I don't really know anything about ODBC but they do have their own database handling functions in PHP that are very similar to all of the mysql_xxx counterparts. Here is a link to the functions php offers for ODBC: PHP ODBC functions

A kind of skimmed over the functions and it looks like all you really need to do is switch out the mysql_xxx functions with their ODBC_xxx counterparts.

All the code for the form and receiving info from the user etc.. would not need to be altered.

3. Crayon Violent on Jul 26, 2008 3:02:30 PM

I knew that how to handle the database through PHP, but after reading this tutorial i think it's more than i think. It's really good. Thanks!

4. deepshah on Aug 11, 2008 2:05:41 AM

I've just started to learn how to use PHP with MySQL so this tutorial has been *very* useful!

I had already put together scripts and an html form to accomplish similar things to those shown in the tutorial but with the scripts in several seperate .php files. Having everything in one file makes much more sense!

Many thanks

5. ma2tt on Aug 16, 2008 10:29:59 AM

Great Job 'Crayon Violent'!

I like the way you presented the information.
Where Can I go from here to learn more things.
Please guide me!

Many Thanks!

6. mahender on Nov 24, 2008 4:06:02 AM
Login or register to post a comment.