Inserting Records in a Database

The overview given earlier very clearly stated that the name=value pairs are given to the cgi program by the server. We shall avoid discussing the post method and the other programming languages that could be considered as better options to suit the needs.

Workings of the Program

If the cgi program is a Windows executable (.exe ) and the method is get in the html file, then the information comes to the program in form of command line arguments. Any Windows programmer is aware of the fact that the third parameter in WinMain stands for command line arguments. So in our program the information is available as name=value pairs from the third parameter. The name, as we have designed our form, represents the field name in the database file and the value is the information to be fed into the database. In a database, normally, records are entered and not a particular field. Hence the entire string now has to be parsed to generate an appropriate sql statement and insert records. Most of the code in the program is string parsing and the remaining part initialises the processes required for ODBC as well as create the sql statements.

So, lets now look at the html form and the 'C' program. Even though it is quite lengthy, we need not worry as there are many repetitive statements.

HTML Form - UserInterface

form.htm

<html>
<title>
itpro - Professional Information Sheet
</title>
<body bgcolor=white text=black>
<blockquote><pre>
<center><h1> Professional Information Sheet</h1></center>
<hr>
<form method=get action="http://202.54.27.226/cgi-shl/cgi.exe">
<a name="top">
<input type=hidden name=id value=0>
<table>
<tr><td><b>Contact Information:</b></td></tr>
<tr><td> </td><td>Title:</td><td><input type=radio name="title" value="Mr." checked > Mr. <input type=radio name="title" value="Ms."> Ms. <input type=radio name="title" value="Dr."> Dr. </td></tr>
<tr><td> </td><td>Name:</td><td><input type=text name=fname size=40 maxlength=40></td><td>(Required)</td></tr>
<tr><td> </td><td>Designation:</td><td><input type=text name=desig size=40 maxlength=40></td></tr>
<tr><td> </td><td>Company:</td><td><input type=text name=company size=40 maxlength=40></td></tr>
<tr><td> </td><td> </td><td>(Address,Tel,Fax, Email -One of the foll. required)</td></tr>
<tr><td> </td><td>Address1:</td><td><input type=text name=addr1 size=40 maxlength=40></td></tr>
<tr><td> </td><td>Address2:</td><td><input type=text name=addr2 size=40 maxlength=40></td></tr>
<tr><td> </td><td>Street:</td><td><input type=text name=street size=20 maxlength=20></td></tr>
<tr><td> </td><td>City:</td><td><input type=text name=city size=20 maxlength=20></td></tr>
<tr><td> </td><td>Postal Code:</td><td><input type=text name=pcode size=15 maxlength=15></td></tr>
<tr><td> </td><td>Country:</td>
<td><select name="country" size=1>
<option></option>
<OPTION>Argentina</option>
<OPTION>Australia</option>
<OPTION>Austria</option>
<OPTION>Belgium</option>
<OPTION>Brazil</option>
<OPTION>Canada</option>
<OPTION>Caribbean</option>
<OPTION>Chile</option>
<OPTION>China</option>
<OPTION>Colombia</option>
<OPTION>Czech Republic</option>
<OPTION>Denmark</option>
<OPTION>Europe</option>
<OPTION>Finland</option>
<OPTION>France</option>
<OPTION>Germany</option>
<OPTION>Greece</option>
<OPTION>Hong Kong</option>
<OPTION>Hungary</option>
<OPTION>India</option>
<OPTION>Ireland</option>
<OPTION>Israel</option>
<OPTION>Italy</option>
<OPTION>Japan</option>
<OPTION>Korea</option>
<OPTION>Latin America</option>
<OPTION>Luxemburg</option>
<OPTION>Malaysia</option>
<OPTION>Mexico</option>
<OPTION>Middle East</option>
<OPTION>Netherlands</option>
<OPTION>New Zealand</option>
<OPTION>North Africa</option>
<OPTION>Norway</option>
<OPTION>Peru</option>
<OPTION>Poland</option>
<OPTION>Portugal</option>
<OPTION>Russia</option>
<OPTION>Slovakia</option>
<OPTION>Slovenija</option>
<OPTION>South Africa</option>
<OPTION>Spain</option>
<OPTION>Sweden</option>
<OPTION>Switzerland</option>
<OPTION>Taiwan</option>
<OPTION>Thailand</option>
<OPTION>Turkey</option>
<OPTION>United Kingdom</option>
<OPTION>United States</option>
<OPTION>Uruguay</option>
<OPTION>Venezuela</option>
</select></td></tr>
<tr><td> </td><td>Telephone:</td><td><input type=text name=tele size=15 maxlength=15></td></tr>
<tr><td> </td><td>Fax:</td><td><input type=text name=fax size=15 maxlength=15></td></tr>
<tr><td> </td><td>Email:</td><td><input type=text name=email size=40 maxlength=40></td></tr>
</table>
<p>
<ul>
<li><i>Display Contact Information when queried </i><input type=radio name=flag value="Y" checked>Yes
<input type=radio name=flag value="N">No
</ul>
<hr>

<table>
<tr><td><b>Qualification:</b></td><td><input type=checkbox name=engcom></td><td>Engineer (Computers)/MCA</td></tr>
<tr><td> </td><td><input type=checkbox name=engoth ><td>Engineer (Other)</td></tr>
<tr><td> </td><td><input type=checkbox name=manag><td>Management</td></tr>
<tr><td> </td><td><input type=checkbox name=sc><td>Science Graduate</td></tr>
<tr><td> </td><td><input type=checkbox name=acom><td>Commerce or Arts Graduate</td></tr>

</table>
<hr>
<table>
<tr><td><b>Experience:</b></td><td>Programming</td><td><input type=text size=3 maxlength=3 name=prgm value=0></td><td>months</td></tr>
<tr><td> </td><td>Systems Analysis</td><td><input type=text size=3 maxlength=3 name=analysis value=0><td>months</td></tr>
<tr><td> </td><td>Project Management</td><td><input type=text size=3 maxlength=3 name=prjmgmt value=0><td>months</td></tr>
</table>
<hr>
<table>
<tr><td><b>Areas Of <br>Work:</b></td><td><input type=checkbox name=appl></td><td>Applications</td></tr>
<tr><td> </td><td><input type=checkbox name=syssoft><td>System Software</td></tr>
<tr><td> </td><td><input type=checkbox name=network><td>Networking</td></tr>
<tr><td> </td><td><input type=checkbox name=commun><td>Communication</td></tr>
<tr><td> </td><td><input type=checkbox name=web><td>Web Related</td></tr>
<tr><td> </td><td><input type=checkbox name=gra><td>Graphics</td></tr>
</table>
<hr>
<table>
<tr><td><b>Tools:     </b></td><td><center><input type=checkbox name=c></center></td><td>C</td></tr>
<tr><td> </td><td><center><input type=checkbox name=cpp></center><td>C++</td></tr>
<tr><td> </td><td><center><input type=checkbox name=java></center><td>Java</td></tr>
<tr><td> </td><td><center><input type=checkbox name=rdbms></center><td>RDBMS</td></tr>
<tr><td> </td><td><center><input type=checkbox name=erp></center><td>ERP</td></tr>
<tr><td> </td><td> </td><td>Other Languages:</td><td><input type=text name=othlang size=40 maxlength=40></td></tr>
</table>
<hr>
<table>
<tr><td><b>Operating System</b></td></tr>
</table>
<input type=checkbox name=unix>Unix
<input type=checkbox name=winnt>NT
<input type=checkbox name=win95>Win'95
<input type=checkbox name=win3x>Windows 3.x
<input type=checkbox name=dos>DOS <br>
Others:<input type=text name=otheros size=40 maxlength=40>
<p>
<p>
<b><u>I am an expert in :</b></u><input type=text name=expert size=40 maxlength=40> <br>

<hr>
<p>
<center>
<input type=submit value="Submit the Form">
<input type=reset value="Clear the Form">
<hr size=3 width=50%>
<a href="http://www.vijaymukhi.com">Vijay Mukhi's Technology Cornucopia Page</a> | <a href="mailto:sonalk@gyral.com">Contact Webmaster</a>
</center>

</form>
</body>
</html>
</blockquote></pre>
</html>

Short explanations to the HTML tags used in the .htm files

<html> --- </html>   
	Every html file must begin in <html> and end in </html>

<title>......</title>
	The title tag makes the text enclosed within this tag as the window title.

<body> ..... </body>
	Within the body tag is given the text that is to be displayed on the screen

<center>....</center> 
	Centrally Aligns the text enclosed within these two tags.

<h1>.........</h1>
	Displays the text enclosed within these two tags in H1 format

<hr>
	A horizontal line is displayed

<form>....</form>
	Gives meaning to the input tag and allows creation of data entry forms

<a name="top">
	a - anchor - assigning a name to the text section following it

<input...> 
	Input tag is used to create controls based on the type field. It has meaning if it is  	within the form tag

<table>....</table>
	Designs a tabular format for the data in rows and columms .These tables may/maynot 	have borders.

<tr>..</tr>
	Creates a row within the table

<td>..</td>
	Creates a column within the row.

<b>..</b>
	The text is displayed in Bold

 	
	Inserts a white space

<select..></select>
	The select tag is used for list boxes . It must be within the form tags

<u>...</u>
	The text is underlined

<a href=...></a>
	Creates a hyper link 

<p>
	Paragraph break

<blockquote>..</blockquote>
<pre>.....</pre>
	Displays the output in a bock form and considers the text as preformatted.

cgi.c

#include <windows.h>
#include <stdio.h>
#include <sql.h>
char temp[1000], bb[5000],pp[1000],qq[1000],yy[1000],zz[1000];
void * henv,*hdbc,*hstmt;
int result,ret_code,idno,recordno,m,n, flag;
int ctr,ctr1;
char titlestr[4];
char fnamestr[41];
char desigstr[41];
char companystr[41];
char addr1str[41];
char addr2str[41];
char streetstr[21];
char citystr[21];
char pcodestr[16];
char countrystr[16];
char addressstr[1000];
char telestr[16];
char faxstr[16];
char emailstr[41];
char flagstr[2];
char engcomstr[30];
char engothstr[30];
char managstr[20];
char scstr[20];
char acomstr[40];
char qualistr[150];
char prgmstr[5];
char analstr[5];
char prjmgmtstr[5];
char applstr[21];
char syssoftstr[21];
char networkstr[21];
char communstr[21];
char webstr[21];
char grastr[21];
char workareastr[149];
char cstr[3];
char cppstr[5];
char javastr[5];
char rdbmsstr[7];
char erpstr [5];
char otoolstr[41];
char toolstr[70];
char unixstr[5];
char winntstr[7];
char win95str[7];
char win3xstr[7];
char dosstr[7];
char oos[41];
char osstr[76];
char expstr[41];
void breakstr();
void callbrk(char ss[]);
_stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l)
{
	strcpy(temp,k);
	printf("Content-type:text/html\r\n\r\n");
	puts("<html>");
	puts("<body bgcolor=white text=black>");
	ret_code=SQLAllocEnv(&henv);
	ret_code=SQLAllocConnect(henv,&hdbc);
	ret_code=SQLConnect(hdbc,"sonal",-3,"aaa",-3,"aaa",-3);
	ret_code=SQLAllocStmt(hdbc,&hstmt);
	strcpy(bb,"Select id from bd");
	ret_code=SQLExecDirect(hstmt,bb,-3);
	result = SQLFetch(hstmt);
	while (result == SQL_SUCCESS)
	{
	SQLGetData(hstmt, 1,SQL_INTEGER,&idno,sizeof(idno),&l );
	result = SQLFetch(hstmt);
	}
	idno++;
	recordno = idno;
	SQLFreeStmt(hstmt,SQL_DROP);
	ret_code=SQLAllocStmt(hdbc,&hstmt);
	strcpy(pp,k);
	for (m=0; pp[m] !=0; m++)
	{
		if (pp[m] == '+')
			pp[m] = ' ';
	}
	n=0;
	for(m=0;pp[m] != 0; m++)
	{
		if (pp[m] == '&')
		{
			qq[n]=0;
			callbrk(qq);
			m++;
			n=0;
			breakstr();
		}
		qq[n] = pp[m];
		n++;
	}
	qq[n]=0;
	callbrk(qq);
	breakstr();
if ((strlen(fnamestr) == 0))
{
	flag = 1;
	puts("<b>You have missed out on the <u>Name</u> Field</b><p>");
	puts("Please Click on the Back Button and enter the required data<p>");
}
if (flag == 0)
{
if((strlen(addressstr) == 0 ) && (strlen(telestr) == 0 ) && (strlen(faxstr) == 0) && (strlen(emailstr) == 0))
{
	flag = 1;
	puts("<b>You must enter one of the fields so that we can contact you easily</b><p>");
	puts("Please Click on the Back Button and enter the required data<p>");
}
}
if (flag == 0)
{
if(strlen(qualistr) == 0 )
{
	flag = 1;
	puts("<b>You must enter your <u>qualifications</u></b><p>");
	puts("Please Click on the Back Button and enter the required data<p>");
}
}
if (flag == 0)
{
if((prgmstr[0] == '0' ) && (analstr[0]== '0') && (prjmgmtstr[0] == '0'))
{
	flag = 1;
	puts("<b>You must enter your <u>experience</u></b><p>");
	puts("Please Click on the Back Button and enter the required data<p>");
}
}
if (flag == 0)
{
if(strlen(workareastr) == 0) 
{
	flag = 1;
	puts("<b>You must enter one of the <u>areas of work</u></b><p>");
	puts("Please Click on the Back Button and enter the required data<p>");
}
}
if (flag == 0)
{
if(strlen(toolstr) == 0) 
{
	flag = 1;
	puts("<b>You must enter one of the <u>tools</u></b><p>");
	puts("Please Click on the Back Button and enter the required data<p>");
}
}
if (flag == 0)
{
if(strlen(osstr) == 0)
{
	flag = 1;
	puts("<b>You must select one of the <u>Operating Systems</u><p>");
	puts("Please Click on the Back Button and enter the required data<p>");
}
}
if (flag == 0)
{
sprintf(bb,"Insert into bd values ( %d,'%s','%s ','%s ','%s ','%s ','%s ','%s ','%s ','%s','%s ', \
		%s,%s,%s,'%s ','%s ','%s ','%s ' )",idno,titlestr,fnamestr,desigstr,companystr,
		addressstr,telestr,faxstr,emailstr,flagstr,qualistr,prgmstr,analstr,prjmgmtstr,
		workareastr,toolstr,osstr,expstr);
ret_code=SQLExecDirect(hstmt,bb,-3);
if (ret_code == 0)
{
	puts("<h2> Data has been successfully entered</h2>");
	puts("<h2>Thank you for taking the time to answer the questions</h2>");
}
else
{
	puts("<h2> Data not entered</h2>");
	puts("Please contact the web master immediately and report the problem<p>");
}
}
SQLFreeStmt(hstmt,SQL_DROP);
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
puts("</body></html>");
}

void callbrk(char ss[])
{
	int y=0,x=0,z=0;
	for (x=0; ss[x] != '='; x++)
	{
	yy[z] = ss[x];
	z++;
	}
	yy[z]=0;
	x++;
	for (;ss[x]!=0; x++)
	{
		zz[y] = ss[x];
		y++;
	}
		zz[y]=0;
}
void breakstr()
{
if (strcmp(yy,"title") ==0)
	strcpy(titlestr,zz);
if (strcmp(yy,"fname") ==0)
	strcpy(fnamestr,zz);
if (strcmp(yy,"desig") ==0)
	strcpy(desigstr,zz);
if (strcmp(yy,"company") ==0)
	strcpy(companystr,zz);
if (strcmp(yy,"addr1") ==0 )
{	if (zz[0] !=0)
{
	strcpy(addr1str," ");
	strcat(addr1str,zz);
}
	ctr++;
}
if (strcmp(yy,"addr2") ==0 )
{
	if (zz[0] !=0)
{
	strcpy(addr2str," ");
	strcat(addr2str,zz);
}
	ctr++;
}
if (strcmp(yy,"street") ==0 )
{
	if (zz[0] !=0)
{
	strcpy(streetstr," ");
	strcat(streetstr,zz);
}
	ctr++;
}
if (strcmp(yy,"city") ==0 )
{
if (zz[0] !=0)
{
	strcpy(citystr," ");
	strcat(citystr,zz);
}
	ctr++;
}
if (strcmp(yy,"pcode") ==0 )
{
if (zz[0] !=0)
{
	strcpy(pcodestr," ");
	strcat(pcodestr,zz);
}
	ctr++;
}
if (strcmp(yy,"country") ==0 )
{
if (zz[0] !=0)
{
	strcpy(countrystr," ");
	strcat(countrystr,zz);
}
	ctr++;
}
if (ctr == 6)
{ 
if(strlen(addr1str) != 0)
strcat(addressstr,addr1str);
if(strlen(addr2str) != 0)
strcat(addressstr,addr2str);
if(strlen(streetstr) != 0)
strcat(addressstr,streetstr);
if(strlen(citystr) != 0)
strcat(addressstr,citystr);
if(strlen(pcodestr) != 0)
strcat(addressstr,pcodestr);
if(strlen(countrystr) != 0)
strcat(addressstr,countrystr);
ctr++;
}
if (strcmp(yy,"tele") ==0 )
	strcpy(telestr,zz);
if (strcmp(yy,"fax") ==0 )
	strcpy(faxstr,zz);
if (strcmp(yy,"email") ==0 )
	strcpy(emailstr,zz);
if (strcmp(yy,"flag") ==0 )
	strcpy(flagstr,zz);
if (strcmp(yy,"engcom") ==0 )
{
	strcpy(engcomstr,"  Engineer(Computers)");
	ctr1 ++;
}
if (strcmp(yy,"engoth") ==0 )
{
strcpy(engothstr,"  Engineer(Others)");
ctr1++;
}
if (strcmp(yy,"manag") ==0 )
{
strcpy(managstr,"  Management");
ctr1++;
}
if (strcmp(yy,"sc") ==0 )
{
strcpy(scstr,"  Science Graduate");
ctr1++;
}
if (strcmp(yy,"acom") ==0 )
{
strcpy(acomstr,"  Commerce,Arts");
ctr1++;
}
if (strcmp(yy,"prgm") ==0 )
{
strcat(qualistr,engcomstr);
strcat(qualistr,engothstr);
strcat(qualistr,managstr);
strcat(qualistr,scstr);
strcat(qualistr,acomstr);
strcpy(prgmstr,zz);
}
if (strcmp(yy,"analysis") ==0 )
strcpy(analstr,zz);
if (strcmp(yy,"prjmgmt") ==0 )
strcpy(prjmgmtstr,zz);
if (strcmp(yy,"appl") ==0 )
strcpy(applstr," Application");
if (strcmp(yy,"syssoft") ==0 )
strcpy(syssoftstr," System Software");
if (strcmp(yy,"network") ==0 )
strcpy(networkstr," Networking");
if (strcmp(yy,"commun") ==0 )
strcpy(communstr," Communication");
if (strcmp(yy,"web") ==0 )
strcpy(webstr," Web Related");
if (strcmp(yy,"gra") ==0 )
strcpy(grastr," Graphics");
if (strcmp(yy,"c") ==0 )
{
strcpy(cstr,"C ");
}
if (strcmp(yy,"cpp") ==0 )
strcpy(cppstr,"C++ ");
if (strcmp(yy,"java") ==0 )
strcpy(javastr,"Java ");
if (strcmp(yy,"rdbms") ==0 )
strcpy(rdbmsstr,"RDBMS ");
if (strcmp(yy,"erp") ==0 )
strcpy(erpstr,"ERP ");
if (strcmp(yy,"othlang") ==0 )
{
strcat(workareastr,applstr);
strcat(workareastr,syssoftstr);
strcat(workareastr,networkstr);
strcat(workareastr,communstr);
strcat(workareastr,webstr);
strcat(workareastr,grastr);
strcpy(otoolstr,zz);
strcat(toolstr,cstr);
strcat(toolstr,cppstr);
strcat(toolstr,javastr);
strcat(toolstr,rdbmsstr);
strcat(toolstr,erpstr);
strcat(toolstr,otoolstr);
}
if (strcmp(yy,"unix") ==0 )
strcpy(unixstr," UNIX ");
if (strcmp(yy,"winnt") ==0 )
strcpy(winntstr," NT ");
if (strcmp(yy,"win95") ==0 )
strcpy(win95str," Win95 ");
if (strcmp(yy,"win3x") ==0 )
strcpy(win3xstr," Win3.x ");
if (strcmp(yy,"dos") ==0 )
strcpy(dosstr," DOS ");
if (strcmp(yy,"otheros") ==0 )
strcpy(oos,zz);
if (strcmp(yy,"expert") ==0 )
{
strcat(osstr,unixstr);
strcat(osstr,winntstr);
strcat(osstr,win95str);
strcat(osstr,win3xstr);
strcat(osstr,dosstr);
strcat(osstr,oos);
strcpy(expstr,zz);
}
}

Steps

  1. Create a new project - Win32 Application.
  2. Add to the Project :
  3. Build an exe file
  4. Copy this exe file to \website\cgi-shl
  5. Start the browser and enter the url

Explanations

The program starts by creating many global variables. This is against the programming rules where it is preferred having few global variables, just to reduce the file size.

	strcpy(temp,k);

As we pointed out earlier, k has the required data and is copied into temp. Please note that we have not been consistent in our naming conventions and would like you to bear with us.

	printf("Content-type:text/html\r\n\r\n");
	puts("<html>");
	puts("<body bgcolor=white text=black>");

The above three statements basically creates the headers and the required contents for the html file. This is given to the server because at this point , the server is the standard output . The server will then instantly dispatch this information to the browser or the client. (In case of ASP, the server manipulates the information received as output, and sends across a newly, freshly created page with the data filled in)

	ret_code=SQLAllocEnv(&henv);
	ret_code=SQLAllocConnect(henv,&hdbc);
	ret_code=SQLConnect(hdbc,"sonal",-3,"aaa",-3,"aaa",-3);
	ret_code=SQLAllocStmt(hdbc,&hstmt);
	strcpy(bb,"Select id from bd");
	ret_code=SQLExecDirect(hstmt,bb,-3);
	result = SQLFetch(hstmt);
	while (result == SQL_SUCCESS)
	{
	SQLGetData(hstmt, 1,SQL_INTEGER,&idno,sizeof(idno),&l );
	result = SQLFetch(hstmt);
	}
	idno++;
	recordno = idno;
	SQLFreeStmt(hstmt,SQL_DROP);

The above code has a lot to do with ODBC than with CGI. As every new task and activity under Windows must be initialised , ODBC routines also have to be initialised. SQLAllocEnv initialises the necessary routines for ODBC and fills up the pointer with the environmental handle. What more jobs it performs is not for us to tackle at this moment.

SQLAllocConnect sets up the resources required for one database connection. Again hdbc is a handle (void *) which is initialised with this function and needs the environmental handle too. The database connection is meaningless unless provided with enough information. Using the function SQLConnect , hdbc now represents the DSN (Data Source Name) 'sonal', having the userid as 'aaa' and password again as 'aaa' . -3 indicates it to be a null terminated string .

A lot of data manipulation is possible within a database. Therefore each and every new update to the database needs a statement handle. A point to note here is that every new statement needs a new handle independent of the others. Since we want to query on a particular field for a record, we create a handle and assign it the select statement. This again is a null terminated string. Now, once the select statement is executed, the output is a result set to which the handle now points to . Henceforth, hstmt refers to the resultset and SQLFetch retrieves a record at a time from this set. It results in a non-zero when there are no more records to fetch.

SQLData binds the first column of the result set which is an integer datatype to the variable idno. Therefore in the loop, idno will constantly keep changing having different values. Finally,when the while loop terminates, idno will hold the value of the last record.

The field idno is basically a counter, assigning an id (in a sequential order) to every record. The new record to be inserted must have the new number, hence the id is incremented by one and finally the handle is released. The new id number is stored in another variable - recordno.

SQLFreeStmt function releases the handle off the data and the resources. This function is basically used to avoid having many handles to hold different sql statement. A handle once freed of its resources can be reused to perform a new job.

Proceeding with the program explanation, once a new idno has been generated, the other fields now need to get their values.

	ret_code=SQLAllocStmt(hdbc,&hstmt);
	strcpy(pp,k);
	for (m=0; pp[m] !=0; m++)
	{
		if (pp[m] == '+')
			pp[m] = ' ';
	}
	n=0;
	for(m=0;pp[m] != 0; m++)
	{
		if (pp[m] == '&')
		{
			qq[n]=0;
			callbrk(qq);
			m++;
			n=0;
			breakstr();
		}
		qq[n] = pp[m];
		n++;
	}	qq[n]=0;
	callbrk(qq);
	breakstr();

As before, SQLAllocStmt is once again used to allocate the necessary resources for the insert command. The third variable k in WinMain is initialised to the values for the respective fields but in pairs, so now the major job on hand is to break up the pairs. Before moving onto this job, we copy the contents to pp, just avoiding any change in value to the original variable.Furthermore, all the + encountered in the string is now replaced with a wide space. To repeat, the browsers replace the spaces with + or some other character since the url cannot have any spaces within them. Now that the value is in the right format, each pair is now to be separated from the other. The only available information here is that an & is the delimiter. Hence in the for loop, the if condition looks out for a character matching &. If the character is not an & , then it is saved in another string. This is terminated on an & . Assuming the character is &, then the string is terminated and another function is called , callbrk, to separate the name from the value , the delimiter here being an =. The pointer in pp is also pushed ahead to forego the & and the new string is again initialised to the starting position. This iteration goes on till the end of the string and the same proces is repeated again to complete the job.

void callbrk(char ss[])
{
	int y=0,x=0,z=0;
	for (x=0; ss[x] != '='; x++)
	{
	yy[z] = ss[x];
	z++;
	}
	yy[z]=0;
	x++;
	for (;ss[x]!=0; x++)
	{
		zz[y] = ss[x];
		y++;
	}
		zz[y]=0;
}
The callbrk function is responsible for separating the field names from the values. Both, the fieldname and the value are stored in two different variables viz yy and zz.

When the breakstr function is called, necessary error checks are made. This function then stores the value in the variable associated with the field.

It was suggested that some personal information or contact number of the user was most crucial so that they could be contacted in future. Their work- experiences, computer backgound and operating tools were required to judge the knowledge of the user. Their position in the company and the languages worked on were also essential. And so ..on

All these validations were to be incorporated in the program. So, in the breakstr, all the values are stored as per their validation in different strings. Returning back to the main program, these strings are checked against the conditions specified. If the results failed, relevant messages are displayed on the client's monitor just to notify him about the information not being complete. There is no entry made in the database until complete data is gathered.

Working on the assumption that the user has keyed in data as per the requirements, an sql statement is created. Since the job of this program is to insert record, the sql command is insert and all the values are then concatenated to this string. This string is given to SQLExecute alongwith the statement handle that finally executes the command hence resulting to a record entry in the database.

As we pointed out earlier, there are better techniques to perform the same job and many other languages which could suit the needs. This is just a small project to demonstrate the workings of CGI thru the Win32 SDK.

Back to CGI Page
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