谁能告诉如何编写?

lkjlnet 2013-02-04 07:15:59
谁能告诉我如何解决我一打开下面的asp网页就在access数据库里写入了空白的数据,还有在用户名文本框和密码文本框提交后,一刷新网页又把文件框里的数据又写入了一次access数据库!
这个asp网页的代码如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label>用户名:
<input type="text" name="yhm" />
</label>
<br />
<label>密码:
<input name="mima" type="text"/>
</label>
<br />
<input type="submit" name="submit" value="提交" />
<input type="reset" name="submit2" value="重置" />
</p>
</form>
<%
set conn=server.CreateObject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.MapPath("data/nod32.mdb")
%>
<%
yhm=request.Form("yhm")
mima=request.Form("mima")
conn.execute("INSERT INTO id(yhm,mima) VALUES ('"+yhm+"','"+mima+"')")
exec="select * from id where date=date()"
set rs=server.CreateObject("adodb.recordset")
rs.open exec,conn,3,1
%>
<%do while not rs.eof%>
<%=rs("yhm")%><br>
<%=rs("mima")%><br>
<%=rs("date")%><br>
<%
rs.movenext
loop
%>
<%
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
</body>
</html>


刚开始数据库是空的,一打开后就出现如下图:
...全文
189 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dogfish 2013-02-07
  • 打赏
  • 举报
回复
<%
yhm=request.Form("yhm")
mima=request.Form("mima")
conn.execute("INSERT INTO id(yhm,mima) VALUES ('"+yhm+"','"+mima+"')")
exec="select * from id where date=date()"
set rs=server.CreateObject("adodb.recordset")
rs.open exec,conn,3,1
%>
这段话是没有条件限制就可以执行的。给个条件去限制就行。如:提交之后才执行。检测提交变量后才执行。
xxwood 2013-02-06
  • 打赏
  • 举报
回复
顶3楼,如果你的实际需求不允许插入空值,那么就那样。 如果允许插入空值,那么把 If yhm<>"" And mima<>"" Then 改成 if request.form("submit")="提交"
scscms太阳光 2013-02-05
  • 打赏
  • 举报
回复
假如你上面的文档是index.asp 把conn.execute("INSERT INTO id(yhm,mima) VALUES ('"+yhm+"','"+mima+"')")改成: If yhm<>"" And mima<>"" Then conn.execute("INSERT INTO id(yhm,mima) VALUES ('"+yhm+"','"+mima+"')") Response.Redirect "index.asp" '重定向以防刷新再次写入 Response.End End If
  • 打赏
  • 举报
回复
因为你没有验证并且在同一个页面,所以页面一打开他就执行了一次,这次数据是空的,当然插入空的数据,因为你提交以后还是在当前页,由于浏览器本身的cookie和历史功能,提交的表单动作会被保存,如果你再试刷新,他就会再次提交,所以每次提交完以后一定要跳转一次页面,让页面执行刷新一下,要不你得用其他验证方式限制刷新提交
YiYanXiYin 2013-02-04
  • 打赏
  • 举报
回复
可以通过判断request.Form("yhm")是否为空来确定是否是提交

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧