ASP判断数据是否重复,代码错在哪?
代码如下:
set rs=server.createobject("adodb.recordset")
sql="select * from kybx where bxmc="&request.Form("bxmc")
rs.open sql,conn,1,3
if rs.eof then
rs.addnew
rs("bxmc") = trim(request.Form("bxmc"))
rs("bxlb") = trim(request.Form("d_position3"))
rs("bxlc") = trim(request.Form("bxlc"))
rs.update
response.Write "<script language=javascript>alert('添加成功!');</script>"
response.write "<meta http-equiv=""refresh"" content=""0;url=kybx.asp"">"
else
Response.write "<script language=javascript>alert('bxmc已经存在!'); history.go(-1);</script>"
end if
response.end
rs.close
set rs=nothing
------解决方案--------------------set rs=server.createobject("adodb.recordset")
sql="select * from kybx where bxmc="&request.Form("bxmc")
rs.open sql,conn,1,3
if not rs.EOF then
Response.write "已经存在!"
else
rs.addnew
rs("bxmc") = trim(request.Form("bxmc"))
rs("bxlb") = trim(request.Form("d_position3"))
rs("bxlc") = trim(request.Form("bxlc"))
rs.update
response.Write "添加成功!"
end if
rs.close
set rs=nothing
------解决方案--------------------VBScript code
sql = "select * from kybx where bxmc='" & request.Form("bxmc") & "'"
rs.open sql,conn,1,3
if rs.eof and rs.bof then
' add new
else
' 已存在
end if
------解决方案--------------------
哪果是根据BMXC这个值判断是否重复,建议在输入BMXC的框中即用AJAX技术判定,免得用户填完后又重新再填,像这种检测放在越前越好。