Delete.c
This code is executed when the Client wants to delete any item on his Shopping list.
Source code of Delete.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;
// BUFFER DECLARATIONS FOR FUNC LIST
char IDN[5];char IDN1[5];char id_of_prod[5];
char a1[30];char b[30]; int result;
char c[30];char d[30];
char e[30];char f[30];
char Cost[10];long TEMP;
char PROD_ID[10];
char PROD_DESC[10];
char QTY[10];
char COST_ITEM[5];
char TOT_COST[8];
SDWORD cba,cbb,cbc,cbd,cbe,cbf; // Last parameter of
SqlGetData
// POINTER DECLARATIONS FOR ODBC CONNECTION
void *henv;
void *hdbc;
void *hstmt;
char *cook; // pointer to the cookie
int cgiMain()
{
int len,i;
cgiHeaderContentType("text/html");
fprintf(cgiOut,"<HTML><HEAD>\n");
fprintf(cgiOut,"<TITLE> SUBMIT ORDER
</TITLE></HEAD>");
fprintf(cgiOut,"<body><h2><font
color='red'> Your Modified Order has been accepted
</font></h2><br><br>");
cook=getenv("HTTP_COOKIE");
sprintf(IDN1,"%s",cook);
cgiFormStringNoNewlines("prod_id",id_of_prod,5);
len=strlen(id_of_prod);
for(i=0;i<len;i++)
{
id_of_prod[i]=toupper(id_of_prod[i]);
}
// odbc code starts
SQLAllocEnv(&henv);
SQLAllocConnect(henv,&hdbc);
SQLConnect(hdbc,"PRODS",-3,0,0,0,0);
SQLAllocStmt(hdbc,&hstmt);
sprintf(aa,"DELETE FROM S WHERE IDNO='%s' AND PROD_ID='%s'
",&IDN1,&id_of_prod);
result=SQLExecDirect(hstmt,aa, -3);
SQLFreeStmt(hstmt,SQL_DROP);
// recalling again from database
SQLAllocStmt(hdbc,&hstmt);
sprintf(aa,"SELECT * FROM S WHERE
IDNO='%s'",&IDN1);
result=SQLExecDirect(hstmt,aa, -3);
SQLFetch(hstmt);
while (result != SQL_NO_DATA_FOUND)
{
SQLGetData(hstmt, 1,SQL_C_CHAR,a1,sizeof(a1),&cba);
SQLGetData(hstmt, 2,SQL_C_CHAR,b,sizeof(b),&cbb);
fprintf(cgiOut, "<B>PROD_ID :</B> %s ", b);
SQLGetData(hstmt, 3,SQL_C_CHAR,c,sizeof(c),&cbc);
fprintf(cgiOut, "<B>PROD_DESC:</B> %s ",
c);
SQLGetData(hstmt, 4,SQL_C_CHAR,d,sizeof(d),&cbd);
fprintf(cgiOut, "<B>QTY:</B> %s ", d);
SQLGetData(hstmt, 5,SQL_C_CHAR,e,sizeof(e),&cbe);
fprintf(cgiOut, "<B>COST_ITEM :</B> %s
<BR>", e);
SQLGetData(hstmt, 6,SQL_C_CHAR,f,sizeof(f),&cbf);
fprintf(cgiOut, "<B>TOT_COST :</B>Rs
%s<BR><BR>", f);
n=atol(f);
TEMP=TEMP+n;
ltoa(TEMP,Cost,10);
result=SQLFetch(hstmt);
}
fprintf(cgiOut, "\n<B>FINAL COST =Rs %s
</B><br><br>\n", Cost);
fprintf(cgiOut, "<form name='modify' method='post'
action='/cgi-shl/delete.exe'>");
fprintf(cgiOut, "Enter PRODUCT ID of Item to be deleted
:");
fprintf(cgiOut, "<input type='text' name='prod_id'
size='4' maxlength='4' ><br>");
fprintf(cgiOut, "<input type='submit' name='bb'
value='Delete Item'>");
fprintf(cgiOut, "</form>");
fprintf(cgiOut, "<form name='modify' method='post'
action='/cgi-shl/update.exe'>");
fprintf(cgiOut, "Enter PRODUCT ID of Item to be updated
:");
fprintf(cgiOut, "<input type='text' name='prod_id'
size='4' maxlength='4'><br>");
fprintf(cgiOut, "Enter previous value of Qty
ordered:");
fprintf(cgiOut, "<input type='text' name='old' size='4'
maxlength='4' ><br>");
fprintf(cgiOut, "Enter new value of Qty required:");
fprintf(cgiOut, "<input type='text' name='new' size='4'
maxsize='4' ><br>");
fprintf(cgiOut, "<input type='submit' name='aa'
value='Update Item'><br>");
fprintf(cgiOut, "</form>");
fprintf(cgiOut, "<BR>");
fprintf(cgiOut, "<a href='/listof.htm'>BACK TO MAIN
SHOPPING PAGE </a><BR> ");
fprintf(cgiOut, "<form name='modify' method='post'
action='/cgi-shl/invoice.exe'>");
fprintf(cgiOut, "<input type='submit' name='bb'
value='EXIT'>");
fprintf(cgiOut, "</form>");
SQLFreeStmt(hstmt,SQL_DROP); // SQLFreeStmt 4
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
fprintf(cgiOut, "</BODY></HTML>\n");
return 0;
}// END OF MAIN
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 ODBC connection explanation has already been provided in cooky.c. This connection will delete the item from the Client's shopping cart in accordance with his cookie number and the product ID number,which is obtained using the CGI function,cgiFormStringNoNewlines( ).After deleting the item ,the modified Shopping Cart is shown to the Client in an HTML format .
Recall.c :
Update.c :