CGI - ODBC
Add/Search Records to Databases on the Internet

Overview

Internet, lately, has gained a prominent position in every business activity. Unlike the good ol' day when this channel was taken seriously only by the military and the research people, today it has evolved incredibly and is being attended by large masses in every field. Even the big companies have realised this importance and are now upgrading their existing sofware and making them internet enabled. Today, if a company doesn't have a Web site and an internet presence, its downfall is taken for granted. A business person prior to signing up a contract with the third party, first visits their Web site and finds out more about them. He uses the internet to collect information and most of his correspondence is through electronic-mail. There are many such instances that everyone is aware of explaining the important role of the 'Net .

All this leads to one conclusion : INTERNETworking now has a major role to play in every organisation.It has gained the momentum and is gradually extending its reach far and wide.

For most people, Internet is synonymous to The World Wide Web and they not only use it for their business but for many other reasons too. Whatever the reasons, most of them use a Web Browser to visit different sites.

What is a Browser?

!!*N!?I*E&^M#@!L!?????!!

A browser is a program that is installed on the client's machine and it helps you in visiting different sites. This program has 2 prime features. At first, the browser, since it has to work closely with the Internet, should be intelligent enough to communicate with the TCP/IP stack program (It is this stack that finally generates the IP packets , encapsulates them based on the communication protocol used and finally places them on the wire). Secondly, the browser performs more of a DTP job by formatting the data received from the server and displaying it in good user interfaces / screens.

All browsers, name any, at first connects to the server. Once the connection is established, it then requests for a particular file. This file can either be an image file, a html file , a java class file or even an activex object. When the server sends the file, it is again now the browser's responsibility to disconnect from the server and display the data received to the end-user/client in the right formatted manner.

All browsers understand the tag language or to be more precise they understand HTML. HTML , a very commonly used word , is an abbreviation to Hyper Text Markup Language. It comprises mostly of tags like <b>, <u>, <I> and so on. If a program is to be termed as a browser then it must contain all the code that understands these tags and behave accordingly. Like for e.g. when there is <b>, the browser knows that any text following the tag must be displayed in bold. It displays all the words in bold upto a </b>. Furthermore, if there is a tag, lets say <applet….> , the java-enabled browser must contain the code required to work with applets and it must also be aware that following applet will be the attributes for it. Similar things happen when the browser sees <embed….> or an <object...> and so on. Normally all the <tags> end in </tags>.

So, to summarise, the browser has to be intelligent enough to parse the text, deliver the output in the right fashion and perform networking routines.

A Si/ample HTML file

<html>
<title> The title of the Window </title>
<img src="a.gif"> This is a picture <br>
<a href="a.html"> A hyper link to a.html <br>
<a href="mailto:sonalk@gyral.com">Email your suggestions </a>
</html>

Now that you understand HTML and realise the importance of a client and a server for information exchange, lets put them in practice through this small project that explains these issues but using programs.

The Workings of CGI -- A Briefing

Forms

As we explained earlier, the HTML language is merely a collection of tags. As <b> is used for bold, <u> for underline, <i> for italics and so on, in order to create a user-interface screen in html , the tag to be used is <form>.
Most of these search engines like Yahoo, AltaVista, InfoSeek and so on, they give the user a good user-interface, accept the search word and finally when the user clicks on a particular button, they return with their search results. These user interfaces (UI) or data-entry forms are again html files with the introduction of <form> tag.
So, following the rules of the HTML language , in order to create a form, there is a <form> which ends with a </form>. Within these two tags, objects like radio buttons, push buttons, listbox etc. are created using the <input> tag. To understand it completely lets look at the form given below.

a.html

<html>
<form>
Enter Name:<input type=text name=fname>
<br>
Employed :<input type=radio name=flag value="Y">Yes
<input type=radio name=flag value="N">No
<br>
Qualification: <input type=checkbox name=engcom>Engineer (Computers)/MCA<br>
<input type=checkbox name=engoth >Engineer (Other) <br>
<input type=checkbox name=manag>Management<br>
<input type=checkbox name=sc>Science Graduate<br>
<input type=checkbox name=acom>Commerce or Arts Graduate<br>
<input type=submit value="Click">
<input type=reset value="Reset">
</form>
</html>

This is the simplest form you can ever create. If you observe carefully you will notice that with every input tag the type changes. It is this attribute that decides the object that should be displayed on the screen. One exception to the rule is for the type - submit and reset. You will yet see a button displayed for it. Now, when you click on the submit button labelled 'Click,' you may/ maynot see anything peculiar happening on your screen. This majorly depends on how you have received the file. This is because when a.html is opened as a file from the local hard disk, it is assumed to be like any other file. The only difference being that it is using a browser for display purpose. This file has nothing to do with the Internet hence you see the same screen as before. However, if the browser had requested this file from the server, then there surely will be noticeable change in the url text box of your browser. What do I mean by a file from server? Allow me to explain. Normally, all files that any client can fetch from the server are placed in a particular subdirectory. In case of WebSite, the directory is htdocs whereas IIS has wwwroot in the InetPub directory. So, if in the url box, you had keyed in http://202.54.3.67/a.html, then this file will be picked up from the right directory as per the server configuration and delivered to you. We suggest you pick up the file using the http protocol and keying in your ip address. For this technique to work, make sure that the server is on and the file lies in the htdocs directory of website. Now when you click on submit, observe the urlbox attentively and you will surely see some changes. The browser internally executes some code and generates this new url. You will notice the original url changed to a spooky url containing + and &. In our case it looked as given below.

http://202.54.3.67/a.html?fname=+flag=+engcom=+engoth=+manag=+sc=+acom=

Now that we have revealed this strange behaviour of a browser, we'll disclose the internal workings of any browser completely, the submit button in particular. Usually the form received from a server has many more attributes associated with it The important ones are method and action. To show you a sample, we have b.html given below.

b.html

<form method="get" action="http://202.54.22.36/cgi-shl/a.exe">
<input type=text name=aa><br>
<input type=text name=bb><br>
<input type=text name=cc><br>
<input type=submit value="Click to Submit">
<input type=reset value="Reset to Default">
</form>

There are two important methods that any form can contain. It can either be initialised to get or post. There is a head value too that can be assigned to this attribute but is never used in particular. Next is the action attribute to which a file name is given. At times you may see a url before the name but is optional. This is an important file that resides on the server. It is an executable file or call it a program. The file extension can be .pl or .exe or .dll or .class and so on. pl extensions are given to perl files , exe and dlls are windows-based programs and.class (you guessed it right) is for a java file.

Submit - Reset

When the submit button is clicked, the browser at first checks the method attribute. The value can either be get or post . Next it checks for action. This attribute is basically to inform the browser about the executable file /program and its location. In most cases, the location is not given which indirectly means connect to the same server from where the form was received. At times directories are specified which contains the executable , hence, informing the browser to connect to the same server from where the form was received but now look into another directory for the program. In case of get, the browser creates a new url depending on the action attribute. Then it appends a? followed by name=value pair. Each pair ends in an & to concate it with another pair . A url cannot contain any wide space characters , so the & is used for concatenation and if any spaces are entered in the text boxes, the browser replaces it with a +. These special characters vary as per the browsers so many a times you may see %2 and so on.

Anyone who understands the http protocol will follow what is stated next. The browser issues a command which is somewhat as shown below

GET http://202.54.22.36/cgi-shl/a.exe?aa="sonal"&bb="cgi+program"&cc="end"

This command is received by the server who then executes the program and transmits the output. Interested readers can refer to the Socket Programming Tutorial on this site to gain more knowledge on these HTTP Clients and Servers.

If the method is post, the browser connects to the url with the filename that is given in action. There is nothing added to this url. The information entered by the user is sent separately again in name=value pair with an & to concatenate.

Reset is reloading the form with the default values as the browser had received it from the server. It loads this file from the cache so it is much quicker as there is no connection made to the server.

CGI

CGI stands for Common Gateway Interface. It is a protocol or call it rules and has different versions too (obviously not important unless you explicitly want to use the newly introduced features with every new version). CGI as it speaks for itself is an interface , in this case, between a client and a server. Needless to say, a user runs a program - browser , to become a client , similarly for a computer to be considered as a server it has to execute a certain program. Each of these server programs normally have environmental variables like REQUEST_METHOD, SERVER_NAME, QUERY_STRING,USER_AGENT.... that are created when the program is installed. The server initialises them as and when required to deliver appropriate information to any client connecting to it. So, after the browser connects to the server and requests for a particular file, the server is aware of thee type of browser the client is using, the client's operating system , the client's ip address through variables like USER_AGENT and so on.

Coming back to our discussion, when the browser makes another fresh connection to the server in for executing the program, the server initialises one of its environmental variables; REQUEST_METHOD with a GET or a POST . (Please note that this behaviour is seen only in case of html forms.) It then checks for the file requested by the browser. Finally, if the method is GET, then the name=value pair becomes the value for the server's Environmental variable -Q UERY_STRING . For POST the server allocates some memory internally to store this chunk of data containing the name=value pair.

When the program is loaded , it receives these values from the server and is not concerned whether the method is get or post; the data is in an environmental variable or in memory. Technically, the server becomes the standard input for the program who will feed it with the information required.

In case of an .exe file, when the method is get the server sends this data as command line argument given to the program. A C programmer is comfortable with command line arguments and can accordingly process this data within his program. We being C/C++ programmers at heart will concentrate more on C programs and the method as get. You can use any other programming language which can do similar things but in a different manner.

ODBC

ODBC -Open Database Connectivity is again a set of rules introduced by Microsoft to solve many problems involved in database management. The problems were as follows.

Initially, all applications were monolithic and had everything built into it. A minor change was asking for too much from the programmer. A database application would contain code to display user interfaces like reports, data entry forms etc. as well as perform database administration tasks. Plus RDBMS' like ORACLE, SYBASE, INGRES ,etc. had their own set of rules and their own proprietary language to communicate with their large databases.

Everybody realised that these RDBMS' performed their job of database administration flawlessly and were superb at it. Since there was no option available, they also worked on user interfaces, obviously not as impressive as it should have been. The point is that the server and the client both these programs were in the same package or were coupled in the same application.

Microsoft decoupled the client and the server. They introduced rules by means of which any client could retrieve data from any database back-end. They worked on the assumption that every database uses SQL for communicating with the databases and released a set of APIs which closely worked with SQL. ODBC is this set of APIs and is used in most of the Microsoft Products that need database communication.

OLEDB

Another major release from Microsoft was OLEDB. OLE / COM,a specification, by means of which a component could talk to a container is the current focus . The APIs involved in ODBC were reformed and a new series of classes (interfaces) introduced. These followed the rules of COM - working at a binary level - and rules of OLE .

Active Server Pages (ASP)

Microsoft realised the importance of CGI and ODBC for online transactions and hence introduced Acitve Server Pages. The asp pages are an incarnation of html pages with the only difference of new variables and the % sign being introduced. To create successful CGI - C programs, one ought to be proficient with the Win32 SDK and must have thorough knowledge of SQL too. This, from Microsoft's point of view was asking for too much from a client. As a reaction to this tedious job, Microsoft released the Active Server Pages concept where in there would be not too much of programming and the results could be achieved accurately and faster. A lot similar to VB scripting language, creating Active Server Pages turned out to be a child's play and hence it was very well received by many web page developers. The concept here too remains the same, where the client receives the form from the server and clicking the button commences the excution of the script. The script uses a lot of variables which are well understood by the server as they become a part of Server Variables or technically speaking Server Side Includes. The server is intelligent enough and initialises all the routines required unlike our program where it is done manually. It is a product which is easy to use and comforts the client , worth a try. All the best.

Download Zip File

This project has been completed by Sonal Kotecha under the guidance of Mr. Vijay Mukhi.


Don't hesitate and feel free to get in touch with all the comments, suggestions etc you might have regarding these programs and explanations.

Back To The Cornucopia Page


Vijay Mukhi's Computer Institute
VMCI, B-13, Everest Building, Tardeo, Mumbai 400 034, India
Tel : 91-22-496 4335 /6/7/8/9     Fax : 91-22-307 28 59
e-mail : vmukhi@giasbm01.vsnl.net.in
http://www.vijaymukhi.com