Brian B. Burton's Guide WebCatalog
WebDNA Basics Advanced Topics OnSite Training WebCat Rants & Raves Other WebCatalog Links Site Feedback    



Adding New Records to a Database File
So, it's Friday afternoon, about 4:55PM, when your boss suddenly appears in your ever so humble office doorway, with a sly grin on his face. "You know that real estate website you've been working on?" he starts off, then he drops the bomb: "Well, the client faxed over 25 more listings, that they want on the site for this weekend." You ponder this briefly, then grab your calendar and desk clock and say "See this thing here, it's called a calendar, and, what's this, according to this calendar, it's FRIDAY. And this other thing, over here in reality, we call a clock. According to this clock, it's now 4:57." Your boss doesn't miss a beat, and says "Hmm, well look at that, it IS Friday, which means, uhhh, it's payday. Oddly enough, I still have your check, and you still need to get this site updated... Come on, it'll only take a few minutes..." You've been had! A few minutes later, as you type away, you realize that there has to be a better way, a way for the client themselves to do this...

Hey, welcome back. In this lesson, we are going to learn how to program a web page that allows someone, using just a web browser, to add a record at a time into a WebCatalog hosted database. For the purposes of this guide, I will be illustrating the database structure with spaces to improve legibility, your databases when done properly will probably not look like this if viewed in a text editor.

a sample of our database file listings.db

Address          City        Zip     Bedrooms   Baths  Sq_Ft
123 West Main    Parsippany  07054   3          2.5    2000
13 Oak Street    Parsippany  07054   1          1      950
48 Rock Ave      Huntsville  08943   2          2      1700

 

Entering the Data on Webpage
Adding new records to a database in WebCatalog is super easy. It's all done in two steps. The first step involves gathering the information, and the second step stores it in the database. Gathering the information is a hugely simple step, which is accomplished by a standard HTML form. In our real estate database, we have 6 fields. The absolute simplest data entry page would look like this:

<body>
 <form method="post" action="addrecord.tpl">
   Address: <input type="text" name="_Address"><br>
   City: <input type="text" name="_City"><br>
   Zip Code: <input type="text" name="_Zip"><br>
   Bedrooms: <input type="text" name="_Bedrooms"><br>
   Bathrooms: <input type="text" name="_Baths"><br>
   Square Feet: <input type="text" name="_Sq_Ft"><br>
   <input type="submit" name="Submit" value="Submit">
 </form>
</body>

Obviously, I left off the <html> tags and the header info, I'll leave that part as an exercise for you, dear reader, to do if you so desire.

So is there anything special on this page? Not really. Let's look at the form tag for a moment. Most of the time, when using a form, you want to include the text method="post". This hides the data being transmitted from being shown in the URL bar, and allows a large amount of data to be submitted at once. The other choice would be method="get", but this has two draw backs, all of the information submitted shows up in the URL bar, and you can only submit a total of 255 characters. So keep it simple, and stick with method="post", OK?

Also, we have the action modifier. With WebCatalog, this is a no brainer. All you have to do is enter the name of the next page (WebDNA template) you want the visitor to go to after they submit the form. Assuming you have indicated a file that is mapped to WebCatalog (i.e. .tpl or .tmpl or possibly .htm or .html) then WebCatalog will take the information you have submitted, and use it (if needed) when executing the WebDNA code on the subsequent page.

All of the form inputs are completely standard. You could also use drop down boxes or other form inputs to ease data entry, or limit the possible entry choices. Notice the name of the input fields. Each is named after the corresponding field in the database with a preceding underscore. While this is not technically a requirement, it will make things easier on the next page. Technically the fields can be named almost anything at all, but you may run into issues if they are named exactly the same as the fields of the database.

Storing the Data in the Database
Ok, we've created a page to let others enter a new record, and someone has done just that (entered a new record they want stored) and clicked the Submit button. Now what? Eeeeck, We haven't coded the next page yet! We better get going...!

WebCatalog stores information in a database with an [append] context. In it's simplest form, it would look like:
[append db=listing.db][/append] but that wouldn't be very handy, because nothing would be appended... To append things, we have to reference the actual name of the fields in the database, and then tell it what data to put in those fields. To store our data, the append would look like:

[append db=listings.db]Address=[_Address]&City=[_City]&Zip=[Zip]&Bedroom=[_bedrooms]
&Baths=[_baths]&Sq_Ft=[_sq_ft][/append]

OK, that was simple enough. Wait a moment... yes? You, with your hand up, there in the back of the class. What's that? There's an error in the code? Ah yes, actually there are two errors in the code above. Nothing would ever get stored in the zip code field. Why? Because we aren't storing a known value. Specifically, the page before sets a variable named _Zip, but we are trying to store a variable just named Zip. Even worse, if this [append] happens inside of another database search (happens quite a bit, actually) and the searched database has a field called zip, the wrong value would get set! And the other error is... trying to store data in a field called Bedroom. There is no field bedroom  in the database (there is a field bedrooms ) WebCatalog is not a mind reader, and will not correct typos. Sorry. If WebCatalog is directed to store information in a field not in the database, it will simply discard the information.

It is also very important to note that the entire append has to be done on one line (no line breaks for readability!) so you're going to have to get used to seeing a really long line in your text editor. When I am putting an append tag together, I usually type it on a few lines, and as the last step concatenate it to just one line, but your method may differ, either way, the end result has to be just one line.

One more thing needs to be covered here. When you store information in a database, it is possible that the user will enter information that could cause problems for WebCatalog. Therefor, you need to add some preemptive programming to your site to prevent future problems. The problem we are going to talk about now is the ampersand (&). If someone enters it into a data entry field, it will cause problems when that information is stored in the database. The way around this problem is to encode the & into something else when we store it, and then decode it when we display the info. We accomplish this with a [URL] tag. In our example, in the address field, someone might enter the intersection of two streets, like "West Main & Cherry Street" That ampersand is the problem... so in our append, we change it to look like address=[url][_addresss][/url]. Our updated append might look like:

[append db=listings.db]Address=[url][_Address][/url]&City=[_City]&_Zip=[Zip]
&Bedrooms=[_bedrooms]&Baths=[url][_baths][/url]&Sq_Ft=[_sq_ft][/append]

Notice that I also put the URL tags around the baths field. Why? someone might enter "2&1/2". You really have to become a predictor of possible data entries to become a good WebDNA programmer. Don't worry, this happens fairly quickly with only a few headache inducing learning experiences...:-)

Ok, you type the above line into a webpage, and run it, what will you see on the HTML page you get back?
NOTHING.
WebCatalog gives no feedback whatsoever that this worked. (It will give you grief if you leave off the [/append]) Because this is more of a programming language then a consumer database, it is up to you to decide what you want to response to be. Let's put together a simple page, and you can see what I mean.

addrecord.tpl

<html>
[append db=listings.db]Address=[url][_Address][/url]&City=[_City]&_Zip=[Zip]
&Bedrooms=[_bedrooms]&Baths=[url][_baths][/url]&Sq_Ft=[_sq_ft][/append]
<head>
   <title>Record Saved</title>
 </head>
 <body>
   Your data has been saved, and is now live on the website. Thank You! 
 </body>
</html>

That's about as basic as you could get. I often times embed a simple search in the body, showing the record pulled back out of the database as a visual confirmation that the information was indeed saved properly in the database. It aids in debugging, and users like it too.

That's great, but what if there are errors in the database that need to be corrected?

<-- Previous    Index    Next -->


HOME | Basics | Advanced | Training | Rants+Raves | Links | Feedback
This whole page, and everything on it ©2001 Brian B. Burton. This page and the text contained herein may not be reproduced on any other site, or via any other means. Really, how hard is it just to link to this site, so people get to see the latest updates? http://www.burtons.com/webdna/   Thank You.

Site design by Clint Davis. Thank You Clint!