Cooky1.c
This code is executed when the Client clicks on the Add To Shopping Cart button provided on the Shopping Mall.
Source code of Cooky1.c
#include <stdio.h>
#include <cgic.h>
#include <windows.h>
#include <string.h>
#include <sql.h>
#include <sqlext.h>
char a[100];char aa[100];char bb[100];static long n=0;static long
c=0;
void list();
// BUFFER DECLARATIONS FOR FUNC LIST
char IDN[5];char IDN1[5];
char PROD_ID[10];
char PROD_DESC[10];
char QTY[10];
char COST_ITEM[5];
char TOT_COST[8];
// POINTER DECLARATIONS FOR ODBC CONNECTION
void *henv;
void *hdbc;
void *hstmt;
char *cook; // pointer to the cookie
int cgiMain( )
{
cgiHeaderContentType("text/html");
fprintf(cgiOut,"<HTML><HEAD>\n");
fprintf(cgiOut,"<TITLE> SUBMIT ORDER
</TITLE></HEAD>");
fprintf(cgiOut,"<body><h2><font
color='red'> Your Order has been accepted
</font></h2><br>");
fprintf(cgiOut," <h2> Click on the
<B>BACK</B> Button of your Browser to Continue.
</h2><br>");
cook=getenv("HTTP_COOKIE");
sprintf(IDN1,"%s",cook);
list( );
// odbc code starts
SQLAllocEnv(&henv);
SQLAllocConnect(henv,&hdbc);
SQLConnect(hdbc,"PRODS",-3,0,0,0,0);
SQLAllocStmt(hdbc,&hstmt);
sprintf(aa,"INSERT INTO
S(IDNO,PROD_ID,PROD_DESC,QTY,COST_ITEM,TOT_COST) VALUES
('%s','%s','%s','%s','%s','%s')",&IDN1,&PROD_ID,&PROD_DESC,&QTY,&COST_ITEM,&TOT_COST);
SQLExecDirect(hstmt,aa, -3);
SQLFreeStmt(hstmt,SQL_DROP); // SQLFreeStmt 3
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
fprintf(cgiOut, "</BODY></HTML>\n");
return 0;
}// END OF MAIN
void list()
{
static long GT=0;
if(cgiFormCheckboxSingle("Pchk1")== cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Plist1", QTY, 10);
sprintf(PROD_ID,"P001");
sprintf(PROD_DESC,"Pic_1");
sprintf(COST_ITEM,"Rs 1000");
n=atol(QTY);
c=1000 * n;
GT=GT+c;
ltoa(c,TOT_COST,10);
}//end if //
if (cgiFormCheckboxSingle("Pchk2") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Plist2", QTY, 10);
sprintf(PROD_ID,"P002");
sprintf(PROD_DESC,"Pic_2");
sprintf(COST_ITEM,"Rs 1000");
n=atol(QTY);
c=1000 * n;
GT=GT+c;
ltoa(c,TOT_COST,10);
}// end if//
if (cgiFormCheckboxSingle("Pchk3") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Plist3", QTY, 10);
sprintf(PROD_ID,"P003");
sprintf(PROD_DESC,"Pic_3");
sprintf(COST_ITEM,"Rs 1000");
n=atol(QTY);
c=1000 * n;
GT=GT+c;
ltoa(c,TOT_COST,10);
}//end if//
if (cgiFormCheckboxSingle("Pchk4") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Plist4", QTY, 10);
sprintf(PROD_ID,"P004");
sprintf(PROD_DESC,"Pic_4");
sprintf(COST_ITEM,"Rs 1000");
n=atol(QTY);
c=1000 * n;
GT=GT+c;
ltoa(c,TOT_COST,10);
}//end if//
if (cgiFormCheckboxSingle("Bchk1") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Blist1", QTY, 10);
sprintf(PROD_ID,"B001");
sprintf(PROD_DESC,"Bk_1");
sprintf(COST_ITEM,"Rs 500");
n=atol(QTY);
c=500 * n;
ltoa(c,TOT_COST,10);
}// end if
if (cgiFormCheckboxSingle("Mchk1") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Mlist1", QTY, 10);
sprintf(PROD_ID,"M001");
sprintf(PROD_DESC,"E_CD1");
sprintf(COST_ITEM,"Rs 525");
n=atol(QTY);
c=525 * n;
ltoa(c,TOT_COST,10);
} //end if
if (cgiFormCheckboxSingle("Mchk2") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Mlist2", QTY, 10);
sprintf(PROD_ID,"M002");
sprintf(PROD_DESC,"E_CD2");
sprintf(COST_ITEM,"Rs 525");
n=atol(QTY);
c=525 * n;
ltoa(c,TOT_COST,10);
}// end if
if (cgiFormCheckboxSingle("Mchk3") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Mlist3", QTY, 10);
sprintf(PROD_ID,"M003");
sprintf(PROD_DESC,"E_CAS1");
sprintf(COST_ITEM,"Rs 95");
n=atol(QTY);
c=95 * n;
ltoa(c,TOT_COST,10);
} //end if
if (cgiFormCheckboxSingle("Mchk4") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Mlist4", QTY, 10);
sprintf(PROD_ID,"M004");
sprintf(PROD_DESC,"E_CAS2");
sprintf(COST_ITEM,"Rs 125");
n=atol(QTY);
c=125 * n;
ltoa(c,TOT_COST,10);
}// end if
if (cgiFormCheckboxSingle("Hchk1") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Hlist1", QTY, 10);
sprintf(PROD_ID,"H001");
sprintf(PROD_DESC,"H_CD1");
sprintf(COST_ITEM,"Rs 375");
n=atol(QTY);
c=375 * n;
ltoa(c,TOT_COST,10);
} //end if
if (cgiFormCheckboxSingle("Hchk2") == cgiFormSuccess)
{
cgiFormStringNoNewlines("ID",IDN,4);
cgiFormStringNoNewlines("Hlist2", QTY, 10);
sprintf(PROD_ID,"H002");
sprintf(PROD_DESC,"H_CAS1");
sprintf(COST_ITEM,"Rs 60");
n=atol(QTY);
c=60* n;
ltoa(c,TOT_COST,10);
}// end if
}// end LIST function //
PROGRAM EXPLANATION :Firstly, you have to add cgic.c and cgic.h to your project workspace in Visual C++. These files have been provided in the downloaded files link .
cook = getenv("HTTP_COOKIE");
This function retrieves the cookie from the environment variable ,HTTP_COOKIE , and stores it in the variable cook .This is done so as to distinguish one client from another ,in case of multiple connections to the Server .
The function list ( ) checks the status of every checkbox item on the Shopping Mall page.If the checkbox is checked , then the particulars of that Item are obtained using cgiFormstringNoNewlines ( ) and redirected to the buffers.This data is then stored into the database(S.dbf) against the Client's ID number .The ODBC connection explanation has already been provided in cooky.c.
Recall.c :
Delete.c :
Update.c :