之前写了linux下apache表单与c语言写的cgi程序和c语言与mysql的连接,今天这里就写下apache表单与mysql的连接:
1、创建一新空表
CREATE TABLE num(
ID int(11) NOT NULL auto_increment,
first int(11),
second int(11),
PRIMARY KEY (ID)
);
执行程序,其他步骤如上篇文章,结果如下:
1、创建一新空表
CREATE TABLE num(
ID int(11) NOT NULL auto_increment,
first int(11),
second int(11),
PRIMARY KEY (ID)
);
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mysql.h"
#define SELECT_QUERY "insert into webnum(first,second) values(%d,%d)"
int main(void)
{
MYSQL con,*sock;
MYSQL_RES *res;
MYSQL_ROW row;
char qbuf[160];
char *data,*p;
int i=1;
char tem[4][50];
/*与表单联系*/
printf("content-type:text/html;charset=gb2312\n\n");
data=getenv("QUERY_STRING");
printf("data=%s<br>\n",data);
p = strtok(data,"=&");
strcpy(tem[0],p);
while((p = strtok(NULL,"=&"))){
strcpy(tem[i],p);
i++;
}
printf("the frist word is %s<br>",tem[1]);
printf("the second word is %s<br>",tem[3]);
/*与mysql连接*/
mysql_init(&con);
if(sock=(mysql_real_connect(&con,"localhost","root","wangsong110","foo",0,NULL,0))){
printf("Connection success\n");
}
sprintf(qbuf,SELECT_QUERY,atoi(tem[1]),atoi(tem[3]));
mysql_query(sock,qbuf);
mysql_close(sock);
return 0;
}
执行程序,其他步骤如上篇文章,结果如下: