首页 | 免费域名 | 个人服务器 | 一流信息监控拦截系统 | 虚拟主机知识库 | ASP 空间 | ASP技术大全 | 下载中心 | 客户服务中心
  7i24 > ASP技术大全 > ASP与数据库 >
    7i24 .Com  
  表单来提交sql (二)

7i24.Com不停为您服务
表单来提交sql (二)
2001-9-10 


列表 B:使用 request.form 来轻松建立SQL字符串。

<%
iStr = "insert into uData "
vStr = "values ("
nStr = "("

' 在表单集合中循环,并建立起SQL语句的组成部分
for each x in request.form
     ' 建立字段名列表
     nStr = nStr & x & ", "
     ' 建立字段值列表
     if uCase(x) = "AGE" then
         vStr = vStr & request.form(x) & ", "
     else
         vStr = vStr & "'" & request.form(x) & "', "
     end if   
next

' 把结尾的", " 从我们建立的字符串中去掉
vStr = left(vStr, len(vStr) - 2) & ")"
nStr = left(nStr, len(nStr) - 2) & ") "

' 把SQL语句组装起来
iStr = iStr & nStr & vStr

if trim(request("fName")) >> "" then
     response.write( iStr & ">BR>")
else
%>

<html>
<body>
<form name=f method=post action="列表2.asp">
Gimme your:

First Name: <input type=text name="fName">

Last Name: <input type=text name="lName">

Age: <input type=text name="age">

<input type="submit" value="Submit">
</form>
</body>
</html>

<%
end if
 %>



列表 C:把字段类型嵌入到HTML字段名中。


<%function buildSQLInsert( targetTable)    
iStr = "insert into " & targetTable & " "   
vStr = "values ("    nStr = "("  
' 在表单集合中循环,并建立起SQL语句的组成部分
for each x in request.form 
     fieldName = x
   fieldData = replace( request.form(fieldName), "'", "''")
     typeDelimPos = inStr(fieldName, "_")
     if typeDelimPos = 0 then
      ' Its a text field
      ' 建立字段名列表
         nStr = nStr & fieldName & ", "
         vStr = vStr & "'" & fieldData & "', "
     else
      ' 是另外一种数据类型
       fieldType = left(fieldName, typeDelimPos - 1)
         fieldName = mid(fieldName, typeDelimPos + 1)
         ' 把字段名加入字段名列表中
         nStr = nStr & fieldName & ", "
         ' 把字段类型变成大写,以确保匹配
         select case uCase(fieldType)
            case "NUM"
                vStr = vStr & fieldData & ", "
                ' 把不明类型按文本型处理
            case else
                vStr = vStr & "'" & fieldData & "', "
            end select
         end if   
     next

     ' 把结尾的", " 从我们建立的字符串中去掉
     vStr = left(vStr, len(vStr) - 2) & ")"
     nStr = left(nStr, len(nStr) - 2) & ") "

     ' 把SQL语句组装起来
     buildSQLInsert = iStr & nStr & vStr
end function


if trim(request("fName")) >< "" then
     response.write( buildSQLInsert & ">BR<")
else
%>

<html>
<body>
<form name=f method=post action="listing3.asp">
Gimme your:

First Name: <input type=text name="fName">

Last Name: <input type=text name="lName">

Age: <input type=text name="num_age">

<input type="submit" value="Submit">
</form>
</body>
</html>

<%
end if
%>




  2002年1月8日  阅读 651 次  发送此页给朋友  来源:    版权争议  删除

相关文章:   近期热点:

上一篇: 用表单来提交sql(转)1
下一篇: 表单来提交sql(三)
返回上一层...
搜索:

(C)2004-2022 7i24.Com 保留所有权利