【CGI-C语言】成绩管理系统----之注册用户操作

本文档介绍如何用C语言编写CGI程序实现成绩管理系统的用户注册功能。通过处理用户输入的数据,利用SQL语句插入到数据库中,完成用户信息存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

adduser.c


#include "stdio.h"

#include "stdlib.h"
#include "string.h"
#include <windows.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <odbcss.h>

int adduser(char *username,char *password);
int main()
{
int i;
char * data;
char * cookie_data;
char username[10];
char password[10];
char pmt_password[10];
data=getenv("QUERY_STRING");

cookie_data=getenv("HTTP_COOKIE");

sscanf(data,"user=%[^&]&pwd=%[^&]&rpwd=%s",username,password,pmt_password);

printf("Contenttype:text/html\n\n");
printf("<html><head><title>注册结果</title></head><body>");

if(cookie_data!=NULL&&strcmp(cookie_data,"i=1")==0)
{
if(0==strcmp(password,pmt_password))
{
i=adduser(username,password);
}
else
{
i=0;
}

if(i==0)
{
printf("注册不成功,密码不一致\n");
printf("<a href=/gm/adduser.html>返回</a><br>");
}

if(i==1)
{
printf("注册成功\n");
printf("<a href=/cgi-bin/gm/listuser.cgi>返回用户列表</a><br>");
}
if(i==2)
{
printf("网络或数据库出错可能是用户名已经存在,请重试\n");
printf("<a href=/gm/adduser.html>返回</a><br>");
}
if(i==3)
{
printf("未知错误有,请重试\n");
printf("<a href=/gm/adduser.html>返回</a><br>");
}
}
else
{
printf("你无权限访问该网页,请登录<br>");
printf("<a href=/gm/login.html>登录</a>");
}



printf("</body></html>");

return 0;
}

int adduser(char *username,char *password)
{
int i;
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLRETURN retcode;
char sql1[79]="insert usr values ('";

char sql2[10]="','";

char sql3[10]="')";

strcat(sql1,username);
strcat(sql1,sql2);
strcat(sql1,password);
strcat(sql1,sql3);


retcode=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
if(retcode==SQL_SUCCESS || retcode==SQL_SUCCESS_WITH_INFO)
{
retcode=SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
if(retcode==SQL_SUCCESS || retcode==SQL_SUCCESS_WITH_INFO)
{
retcode=SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc);
if(retcode==SQL_SUCCESS || retcode==SQL_SUCCESS_WITH_INFO)
{
retcode=SQLConnect(hdbc,(SQLCHAR*)"Csql",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS); //可修改,数据源,用户名,密码
if(retcode==SQL_SUCCESS || retcode==SQL_SUCCESS_WITH_INFO)
{
retcode=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
if(retcode==SQL_SUCCESS || retcode==SQL_SUCCESS)
{
SQLINTEGER p=SQL_NTS;
SQLPrepare(hstmt,sql1,79);//第三个参数与数组大小相同,而不是数据库列相同
retcode=SQLExecDirect(hstmt,sql1,79);

if(retcode == SQL_ERROR )
{
i=2;

}
else if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
i=1;
}
else
{
i = 3;
}
}
SQLDisconnect(hdbc);
}
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
}
}
}
return i;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值