第一步:运行sql脚本:https://download.csdn.net/download/qq_40098572/10946799
第二步:搭建开发环境
第三步:搭建mysql环境
#include <stdlib.h>
#include <stdio.h>
#include <winsock.h>
#include <mysql.h>
#include "cgic.h"
#include "ctemplate.h"
#include <Windows.h>
void executeNonQuery(char * sql);
MYSQL_RES * executeQuery(char *sql);
void showError(char *msg);
void printHtmlContentType();int cgiMain()
{
// MessageBox(NULL, "ok", "ok", MB_OK);char mod[32] = { 0 };char action[32] = { 0 };//所有的对cgi的请求都至少要带这两个参数。我的约定//MVC.cgi?mod=teacher&action=list 列出所有的老师数据//MVC.cgi?mod=teacher&action=addnew 进入新增老师的界面//MVC.cgi?mod=teacher&action=addnewSubmit 新增老师的保存操作//StudengMgr.cgi?mod=student&action=list 列出所有的学生数据//MVC.cgi?mod=user&action=login 用户登陆if (cgiFormString("mod", mod, sizeof(mod)) != cgiFormSuccess){showError("没有提供mod参数");return 0;}if (cgiFormString("action", action, sizeof(action)) != cgiFormSuccess){showError("没有提供action参数");return 0;}if (strcmp(mod, "teacher") == 0){if (strcmp(action, "list") == 0){MYSQL_RES *result = executeQuery("select Id,Name,PhoneNum from T_Teachers");TMPL_varlist *varlist = 0;TMPL_loop *loopTeachers = 0;MYSQL_ROW row;while (row = mysql_fetch_row(result)){char *id = row[0];char *name = row[1];char *phoneNum = row[2];loopTeachers = TMPL_add_varlist(loopTeachers,TMPL_add_var(0, "Id", id, "Name", name, "PhoneNum", phoneNum, 0));}//不要写错成:TMPL_add_loop(varlist,"teachers",loopTeachers);varlist = TMPL_add_loop(varlist, "teachers", loopTeachers);printHtmlContentType();TMPL_write("TeacherList.htm", 0, 0, varlist, cgiOut, cgiOut);mysql_free_result(result);}else if (strcmp(action, "addnew") == 0){printHtmlContentType();TMPL_write("TeacherAddNew.htm", 0, 0, 0, cgiOut, cgiOut);}else if (strcmp(action, "addnewSubmit") == 0){char name[256] = { 0 };char phoneNum[256] = { 0 };if (cgiFormString("Name", name, sizeof(name)) != cgiFormSuccess){showError("姓名不能为空");return;}if (cgiFormString("PhoneNum", phoneNum, sizeof(phoneNum)) != cgiFormSuccess){showError("手机号码不能为空");return;}{char sql[1024] = { 0 };sprintf(sql, "insert into T_Teachers(Name,PhoneNum) values('%s','%s')",name, phoneNum);executeNonQuery(sql);cgiHeaderLocation("MVC.cgi?mod=teacher&action=list");}}else if (strcmp(action, "edit") == 0){int id;if (cgiFormInteger("id", &id, 0) != cgiFormSuccess){showError("请提供要修改的id");return;}{//加载旧的数据MYSQL_RES * result;char sql[1024] = { 0 };MYSQL_ROW row;sprintf(sql, "select Id,Name,PhoneNum from T_Teachers where Id=%d", id);result = executeQuery(sql);if (row = mysql_fetch_row(result))//有一条{char* name = row[1];char *phoneNum = row[2];TMPL_varlist *varlist = 0;varlist = TMPL_add_var(varlist, "Id", row[0], "Name", name, "PhoneNum", phoneNum, 0);printHtmlContentType();TMPL_write("TeacherEdit.htm", 0, 0, varlist, cgiOut, cgiOut);}else//一条结果都没有{showError("找不到这个id对应的教师信息");return;}}}else if (strcmp(action, "editSubmit") == 0){int id;char name[256] = { 0 };char phoneNum[256] = { 0 };if (cgiFormInteger("Id", &id, 0) != cgiFormSuccess){showError("Id不能为空");return 0;}if (cgiFormString("Name", name, sizeof(name)) != cgiFormSuccess){showError("Name不能为空");return 0;}if (cgiFormString("PhoneNum", phoneNum, sizeof(phoneNum)) != cgiFormSuccess){showError("PhoneNum不能为空");return 0;}{char sql[1024] = { 0 };sprintf(sql, "update T_Teachers set Name='%s' ,PhoneNum='%s' where Id=%d",name, phoneNum, id);executeNonQuery(sql);//重定向返回教师列表页面cgiHeaderLocation("MVC.cgi?mod=teacher&action=list");}}else if (strcmp(action, "delete") == 0){int id;if (cgiFormInteger("id", &id, 0) != cgiFormSuccess){showError("Id不能为空");return 0;}{char sql[128] = { 0 };sprintf(sql, "delete from T_Teachers where Id=%d", id);executeNonQuery(sql);cgiHeaderLocation("MVC.cgi?mod=teacher&action=list");}}else{showError("未知的action参数");return 0;}}else//倡议:有if就要写else{showError("未知的mod参数");return 0;}return 0;
}void printHtmlContentType()
{cgiHeaderContentType("text/html;charset=gbk");
}
void showError(char *msg)
{TMPL_varlist *varlist = 0;varlist = TMPL_add_var(varlist, "msg", msg, 0);printHtmlContentType();TMPL_write("Error.htm", 0, 0, varlist, cgiOut, cgiOut);
}
void executeNonQuery(char * sql)
{MYSQL* pConn = mysql_init(0);if (!mysql_real_connect(pConn, "localhost", "root", "root", "studengmgr", 0, 0, 0)){goto error;}if (mysql_query(pConn, "set names gbk")){goto error;}if (mysql_query(pConn, sql)){goto error;}goto exit;
error:cgiHeaderContentType("text/html;charset=gbk");fprintf(cgiOut, "执行出错 %s", mysql_error(pConn));//printf("执行出错 %s",mysql_error(pConn));
exit:mysql_close(pConn);
}
MYSQL_RES * executeQuery(char *sql)
{MYSQL* pConn = mysql_init(0);if (!mysql_real_connect(pConn, "localhost", "root", "root", "studengmgr", 0, 0, 0)){goto error;}if (mysql_query(pConn, "set names gbk")){goto error;}if (mysql_query(pConn, sql)){goto error;}{MYSQL_RES *result = mysql_store_result(pConn);mysql_close(pConn);return result;}
error:cgiHeaderContentType("text/html;charset=gbk");fprintf(cgiOut, "执行出错 %s", mysql_error(pConn));//printf("执行出错 %s",mysql_error(pConn));
exit:mysql_close(pConn);
}