如何用最简单的语句提交大量表单对象的值存储到数据库?说道这个问题可能大家经常遇到,感到很头痛,但是又没办法,只能慢慢写,笔者以提交一张应聘表单的提交为例给大家介绍如何用最简短的语句来达到目的
脚本运行通过环境环境:
iis5.0+sqlserver2000(当然也可以access2000等)
如有任何问题或建议请发email:chenxingbai@21cn.com
生成表结构的脚本
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[yingpin]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[yingpin]
GO
CREATE TABLE [dbo].[yingpin] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[yp_ name] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[sex] [varchar] (4) COLLATE Chinese_PRC_CI_AS NULL ,
[birthday] [varchar] (15) COLLATE Chinese_PRC_CI_AS NULL ,
[health] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[hunyin] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[zhengzhi] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[xueli] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[zhicheng] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[english] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[email] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[phone] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[yp_ address] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[zip] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[colledge] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[zhuanye] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[bumen] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[gangwei1] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[gangwei2] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[x_shijian] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[x_address] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[x_zhiwu] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[g_shijian] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[g_address] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[g_zhiwu] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[h_name] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[h_guanxi] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[h_danwei] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[h_zhiwu] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[pingjia] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[shexiang] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[beizhu] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[shijian] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[yingpin] WITH NOCHECK ADD
CONSTRAINT [DF_yingpin_shijian] DEFAULT (getdate()) FOR [shijian],
CONSTRAINT [PK_yingpin] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO
Asp脚本addyp.asp
<!--#include file="lib/config1.asp"-->
<%
if request("action")="add" then
dim str,strvalue,strname,i,str1
str="yp_name,sex,birthday,health,hunyin,zhengzhi,xueli,zhicheng,english,email,phone,yp_address,zip,colledge,zhuanye,bumen,gangwei1,gangwei2,x_shijian,x_address,x_zhiwu,g_shijian,g_address,g_zhiwu,h_name,h_guanxi,h_danwei,h_zhiwu,pingjia,shexiang,beizhu"
str1="x_shijian,x_address,x_zhiwu,g_shijian,g_address,g_zhiwu,h_name,h_guanxi,h_danwei,h_zhiwu"
strname=split(str,",")
for i=lbound(strname) to ubound(strname)
if i<>ubound(strname) then
if instr(str1,strname(i))=0 then
if trim(request.Form(strname(i)))="" then
response.write "<script>alert('发生错误,请将数据填写完整!');history.back(1);</script>"
response.End()
end if
sql=sql&"'"&trim(request.Form(strname(i)))&"',"
else
j=1
much=trim(request.Form(cstr(strname(i)&j)))
for j=2 to 5
much=much&"|"&trim(request.Form(cstr(strname(i)&j)))
next
sql=sql&"'"&much&"',"
end if
else
if trim(request.Form(strname(i)))="" then
response.write "<script>alert('发生错误,请将数据填写完整!');history.back(1);</script>"
response.End()
end if
sql=sql&"'"&trim(request.Form(strname(i)))&"'"
end if
next
sql="insert into yingpin ("&str&") values ("&sql&")"
conn.Execute(Sql)
if Err <> 0 Then
response.write "<script>alert('发生错误,请重新操作!');history.back(1);</script>"
else
response.write "<script>alert('已经成功提交应聘信息,\n我们会尽快和你联系!');window.location='addyp.asp';</script>"
end If
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>gsjj</title>
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body bgcolor="#0066CC" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" style="background-attachment:fixed;">
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="top" class="gray">
<p align="center"><font color="#66FF00">:::::::诚邀加盟:::::::</font></p>
<form action="addyp.asp?action=add" method="post" name="ts">
<table width=100% border=1 align="center" cellpadding=0 cellspacing="0" bordercolorlight="#000000" bordercolordark="#ffffff">
<tbody>
<tr>
<td height="35" colspan="5"><font color="#FFFFFF"><span
id=fps7>姓 名
<input
name="name" onFocus="this.select()"
onMouseOver="this.focus()" size="12"
>
<font color="#FF0000"> *</font> 性别
<input
name="sex" onFocus="this.select()"
onMouseOver="this.focus()" size="4"
type=text>
<font color="#FF0000">*</font>出生年月
<input name="birthday"
type=text id="birthday"
onFocus="this.select()"
onMouseOver="this.focus()" size="16"
>
<font color="#FF0000">*</font> </span></font></td>
</tr>
<tr>
<td height="36" colspan="5"><font color="#FFFFFF"><span
id=fps7>健康状况
<input
name="health" onFocus="this