오늘은 간단한 회원등록 cgi 를 만들어 보겠습니다.
만약 당신이 이것을 손수 작성 할 수 있다면 web에서 Db에 Insert하는 방법을 알게 된 것입니다.
1) 클라이언트용 html 문서 작성하기
name에 대한 변수이름은 reg1 즉.. name<input type=text name=reg1>
age에 대한 변수이름은 reg2
sex중 male의 변수이름은 reg3 type은 radio 선택하였을때의 값은 1
sex중 female의 변수이름은 reg3 type은 radio 선택하였을때의 값은 2
address에 대한 변수이름은 reg4
telephone에 대한 변수이름은 reg5
memo에 대한 변수이름은 reg6 memo는 textarea로 한다.
2) 서버용 ex1.cgi 작성하기
#include "stdio.h"
#include "/home/superman/cgic/cgic.h"
#define USERNAME "superman"
#define PASSWORD "superman"
#define DBSTRING "db"
EXEC SQL BEGIN DECLARE SECTION;
char *username = USERNAME;
char *password = PASSWORD;
char *dbstring = DBSTRING;
int count1;
VARCHAR re1[20];
int re2;
int re3;
VARCHAR re4[60];
VARCHAR re5[20];
VARCHAR re6[2000];
VARCHAR re7[8];
EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE sqlca;
void sqlerror();
int getCgiParameter();
void setMemory ( void );
int cgiMain ( void )
{
int result;
putenv("ORACLE_HOME=/usr/oracle/733");
putenv("ORACLE_SID=superman");
putenv("NLS_LANG=American_America.ko16ksc5601");
putenv("LD_LIBRARY_PATH=/usr/oracle/733/lib");
EXEC SQL WHENEVER SQLERROR DO sqlerror();
EXEC ORACLE OPTION(HOLD_CURSOR=NO);
EXEC SQL CONNECT :username IDENTIFIED BY :password USING :dbstring;
cgiHeaderContentType("text/html");
setMemory();
getCgiParameter();
EXEC SQL SELECT to_char(SYSDATE,'YY-MM-DD') into re7 from sys.dual;
EXEC SQL SELECT max(S_ID) INTO :count1 FROM s_reg_db order by s_date;
count1++;
EXEC SQL INSERT INTO s_reg_db ( s_id , s_name , s_age , s_sex , s_address , s_telephone , s_date , s_memo ) VALUES ( :count1, :re1 , :re2 , :re3 , :re4 , :re5, SYSDATE , :re6 ); // 데이터 삽입
fprintf(cgiOut, "Insert ok\n");
fprintf(cgiOut, "home");
EXEC SQL COMMIT RELEASE; // commit하고 연결을 지속한다. 왜냐 다음 데이터를 또 insert해야하므로
return 0;
}
void sqlerror() {
EXEC SQL WHENEVER SQLERROR CONTINUE;
fprintf(cgiOut, "ORACLE Error detected: \n");
fprintf(cgiOut, "% .70s \n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}
int getCgiParameter() {
int result;
char x[3];
result = cgiFormString ( "reg1", re1.arr , sizeof(re1.arr)); // 클라이언트용 html파일에서 데이터를 입력받을 때 사용
re1.len = strlen( (char *)re1.arr ); // 꼭 해주어야 함. 길이를 꼭 설정해주어야 올바른 값이 나옵니다.
result = cgiFormString ( "reg4" , re4.arr , sizeof(re4.arr));
re4.len = strlen( (char *)re4.arr );
result = cgiFormString ( "reg5" , re5.arr , sizeof(re5.arr));
re5.len = strlen( (char *)re5.arr );
result = cgiFormString ( "reg6" , re6.arr , sizeof(re6.arr));
re6.len = strlen( (char *)re6.arr );
result = cgiFormString ( "reg2" , x , 3 );
re2 = atoi ( x );
result = cgiFormString ( "reg3" , x , 3 );
re3 = atoi ( x );
if ( result == cgiFormNotFound )
{
re3 = -1;
}
else
{
re3 = atoi ( x );
}
fprintf(cgiOut," name : %s\n\n",re1.arr);
fprintf(cgiOut," age : %d\n\n",re2);
if ( re3 == 1 )
{
fprintf(cgiOut," sex : male\n");
}
else if ( re3 == 2 )
{
fprintf(cgiOut," sex : female\n");
}
fprintf(cgiOut," address : %s\n\n",re4.arr);
fprintf(cgiOut," telephone : %s\n\n",re5.arr);
fprintf(cgiOut," memo : %s\n\n",re6.arr);
}
void setMemory ( void )
{
memset(re1.arr,0,sizeof(re1.arr));
memset(re4.arr,0,sizeof(re4.arr));
memset(re5.arr,0,sizeof(re5.arr));
memset(re6.arr,0,sizeof(re6.arr));
memset(re7.arr,0,sizeof(re7.arr));
re2 = -1;
re3 = -1;
count1 = 0;
}
4) Makefile
include ../env_precomp.mk
.SUFFIXES: .pc .c .o
LDSTRING=
PRODUCT_LIBHOME=
MAKEFILE=Makefile
#PROCPLSFLAGS= sqlcheck=full userid=$(USERID) dbms=V6
PROCPLSFLAGS= dbms=v6
PROCPPFLAGS= code=cpp $(CCPSYSINCLUDE)
USERID=superman/superman
INCLUDE=$(I_SYM). $(PRECOMPPUBLIC)
CGILIBHOME=/home/urstory/cgic
EXECDIR=/www/cgi-bin/ex1/
CGILIB=-lcb -lcgic -lmy
TARGETS=ex1
# Rule to compile any program (specify EXE= and OBJS= on command line)
build: $(OBJS)
$(CC) -o $(EXE) $(OBJS) ./printTemplet.o -L$(LIBHOME) -L$(CGILIBHOME) $(PROLDLIBS) $(CGILIB)
#build: $(OBJS)
# $(CC) -o $(EXE) $(OBJS) -L$(LIBHOME) $(PROLDLIBS)
all: $(TARGETS)
$(TARGETS):
$(MAKE) -f $(MAKEFILE) build OBJS=$@.o EXE=$@.cgi
.pc.c:
$(PROC) $(PROCFLAGS) $(PROCPLSFLAGS) iname=$*.pc
.pc.o:
$(PROC) $(PROCFLAGS) $(PROCPLSFLAGS) iname=$*.pc
$(CC) $(CFLAGS) -c $*.c
.c.o:
$(CC) $(CFLAGS) -c $*.c
install:
mv *.cgi $(EXECDIR)
댓글 없음:
댓글 쓰기