赛博宇华sop m8:我的ASP代码哪里错了?

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/27 23:27:46
<%
if ( request.Form("un") <> "" ) and ( request.Form("pw") <> "" ) then

dim dbpath,conn,sql,constr,rsUser
dbpath=server.mappath("../database/main.mdb")
constr = "provider=microsoft.jet.oledb.4.0;data source=" & dbpath

session("un") = request.Form("un")
session("pw") = request.Form("pw")

response.write("欢迎"&session("un"))

sql = "SELECT un, pw from userinfo WHERE un="&request.Form("un")&" and pw="&request.Form("pw")

' 建立数据库连接
set conn = server.CreateObject("adodb.connection")
conn.open constr

' 建立记录集查询
set rsUser = server.CreateObject("adodb.recordset")
rsUser.Open sql,conn,1,3
else %>
<form id="login" name="login" method="POST" action="<%=MM_LoginAction%>">
<label>用户名:
<input name="username" type="text" id="username" tabindex="1" size="10" maxlength="50" />
</label>
<label>密码:
<input name="password" type="password" id="password" tabindex="2" size="10" maxlength="50" />
</label>
<label>
<input type="submit" name="submit" value="登陆" tabindex="3" />
</label>
注册
</form>

<% end if %>
当前页面为index.asp
表单里面action=后面是当前页面,也就是
action="index.asp"
我用数据库中存在的用户名密码登陆后,页面没有变化,没有如预料的那样显示出response.write("欢迎"&session("un")) 这行代码,而是仍然是else里面的表格?

找到原因了,
sql = "SELECT un, pw from userinfo WHERE un="&request.Form("un")&" and pw="&request.Form("pw")
应该为sql = "SELECT un, pw from userinfo WHERE un='"&request.Form("un")&"' and pw='"&request.Form("pw") & "'"
提交过来的是字符串,应该加'',就这么简单.

表单里面也有问题,不过那个是我笔误:)还是采纳一个大案,终究人家也好心.鼓励几个大大多帮助我们这些新手啊.

asp部分我倒没看出错误,表单有错,请改为:
<input name="un" type="text" id="username" tabindex="1" size="10" maxlength="50" />
</label>
<label>密码:
<input name="pw" type="password" id="password" tabindex="2" size="10" maxlength="50" />

action="<%=MM_LoginAction%>"
你的action不是index.asp,而是这个从数据库里面拿出来的东西,但是你的数据库连接在if里面,所以当else出现时action不是index.asp,而是空值,所以你怎么传也传不到index.asp里面。
建议你用判断变量的方法处理本页post,看起来比较直观。例如action="index.asp?login=apply",然后在index.asp的开头判断login的值是否为apply,这样你就可以把连接数据库放到if里面。