注册 登录
编程论坛 ASP技术论坛

[已解决]rs.update的奇怪问题,大家来看看,问题出在哪儿

slfyeye 发布于 2007-08-20 15:28, 1206 次点击
是这样的,我用session保存了待更改的产品id,当我修改某产品图片路径时,用了如下语句:
====================
Dim rs,strSql
Set rs = Server.CreateObject("ADODB.Recordset")
strSql = "select imgpath from product where id=" & Session("id") 'imgpath为access数据库中保存的图片路径
rs.open strSql,G_CONN,1,3 '这句有的,不好意思,漏打上去了刚才。
rs("imgpath")="一个新的路径字符串"
rs.update
rs.close
Set rs = Nothing
Session("id")=""
====================
可是这么写总是变成新添加一条记录(我这里并没有写rs.addnew)而不是更改原记录。
也尝试过改为:
1,"insert into product(imgpath) values('新路径') where id=" & Session("id")
2, "update product set imgpath ='新路进’where id=" & Session("id")
但结果和前面一样变成新建了一条记录,并把结果填写到对应的imgpath字段内。
高手来帮我看一下吧,我懊恼了N久,还是没发现原因所在。
谢谢大家!

[此贴子已经被作者于2007-8-21 10:18:56编辑过]

15 回复
#2
ayue2222007-08-20 15:32
Dim rs,strSql
Set rs = Server.CreateObject("ADODB.Recordset")
strSql = "select imgpath from product where id=" & Session("id") 'imgpath为access数据库中保存的图片路径
rs.open strsql,conn,1,3 //少了这句吧 ..
rs("imgpath")="一个新的路径字符串"
rs.update
rs.close
Set rs = Nothing
Session("id")=""

如果用 conn.execute ("update product set imgpath ='新路进’where id=" & Session("id")) 这样也是可行的 ....
#3
slfyeye2007-08-20 15:37
有这句的,呵呵,不好意思,刚才漏了。继续想听听高手的意见,问题的原因不知道在哪儿。。。
G_CONN.Execute +上面的语句1或2我都试过,但是出现了同样的问题。
我测试过,该页面传递的Session("id")完全正确。但是最后为什么变成添加新纪录了而不是更改原记录的字段?

[此贴子已经被作者于2007-8-20 15:41:14编辑过]

#4
ayue2222007-08-20 15:44
id="&Cint(Session("id"))
#5
slfyeye2007-08-20 15:50
以下是引用ayue222在2007-8-20 15:44:34的发言:
id="&Cint(Session("id"))

谢谢楼上那位朋友,我这样试了一下,还是原来的状况,呜呜


Session传过来的id是没有问题的。我用Response.write测试过,正确的。

[此贴子已经被作者于2007-8-20 15:51:06编辑过]

#6
ayue2222007-08-20 15:56
Dim rs,strSql
Set rs = Server.CreateObject("ADODB.Recordset")
strSql = "select imgpath from product where id=" & Cint(Session("id")) 'imgpath为access数据库中保存的图片路径
rs.open strsql,conn,1,3
if not (rs.bof and rs.eof) then
rs("imgpath")="一个新的路径字符串"
rs.update
end if
rs.close
Set rs = Nothing
Session("id")=""

还有就是你的数据库应该是*.mdb而不是*.ldb...
#7
slfyeye2007-08-20 16:12

肯定有记录的,还是找不到问题的根源。。。。

#8
madpbpl2007-08-20 16:57
response.write session("id")
你看看这个值是不是你要更新对应的id,感觉问题应该出在这里
#9
slfyeye2007-08-20 17:02
以下是引用madpbpl在2007-8-20 16:57:40的发言:
response.write session("id")
你看看这个值是不是你要更新对应的id,感觉问题应该出在这里

我用过这句语句来测试,session 传值正常!

#10
阳光白雪2007-08-20 17:04
把你的全部代码贴上
#11
slfyeye2007-08-20 17:09

调用的地方是:<iframe src='admin_upload.asp?action=modify'></iframe>

amdin_upload.asp
<%
Response.expires=-1
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","no-store"
%>
<!--#include file="inc/conn.asp"-->
<!--#include file="inc/function.asp"-->
<!--#include file="inc/UpLoadClass.asp"--> ’此处运用风声无组件上传图片
<%
If checkAdminLogin() = False Then '检验是否登录
Response.Redirect "admin_login.htm"
End If
Server.ScriptTimeOut=5000
Dim request2,rs,strSql,strAction
Set request2=New UpLoadClass '这个是风声上传里的
request2.Charset="utf-8"
request2.Open()
%>
<!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>Upload Image</title>
<style type="text/css">body,form{margin:0;padding:0;background:#C6EBDE}#strPhoto{width:300px;}Input{font-family:"Verdana, Arial";font-size:9pt;color:#555555;background-color:#fefefe;border:1px solid #555555}</style>
</head>
<body>
<!--下面的表单用于提交图片-->
<form name="upimg" method="post" action="admin_upload.asp?toup=1" enctype="multipart/form-data"><input name="strPhoto" type="file" id="strPhoto" /> <input type="submit" name="upload" value="upload" /></form>
<%
’以下程序分为两部分,modify用于修改图片的地址(问题出在这个模块),而main则是用于添加新商品的图片
strAction = Trim(Request.QueryString("action"))
If strAction = "modify" Then
Call modify() '修改商品图片
Else Call main() '添加新商品
End If

Sub main()
If Session("imgid") = "" Then
Response.Write "<div style='background:#999999;width:300px;height:14px;font:bold 10px Verdana;color:#FFFF00;' id='noticeimg'>Only images [jpg/gif] &lt;200K are allowed!</div>"
If Request.QueryString("toup") = 1 Then
Set rs=Server.CreateObject("ADODB.Recordset")
strSql="select * from product"
rs.open strSql,G_CONN,1,3
rs.addnew
rs("imgpath")=request2.SavePath&request2.Form("strPhoto")
rs.update
'Aspjpeg组件生成两个尺寸的缩略图
Set Jpeg=Server.CreateObject("Persits.Jpeg")
Path = Server.MapPath(rs("imgpath"))
Jpeg.Open Path
Jpeg.Width = Jpeg.OriginalWidth / 2
Jpeg.Height = Jpeg.OriginalHeight / 2
Jpeg.Quality = 90
Jpeg.Save Server.MapPath("upload\middle")&Mid(rs("imgpath"),7)
Jpeg.Width = Jpeg.OriginalWidth / 2 '在上一次计算的基础上
Jpeg.Height = Jpeg.OriginalHeight / 2
Jpeg.Save Server.MapPath("upload/small")&Mid(rs("imgpath"),7)'正反斜杠都行
Set Jpeg = Nothing
Session("imgid") = rs("id") '保存上传图片对应的id
rs.Close
Set rs = Nothing
Response.Write "<script>document.upimg.style.visibility='hidden';noticeimg.innerHTML='Image uploaded.'</script>"
End If
Else
Response.Write "<script>document.upimg.style.visibility='hidden'</script><div style='background:#999999;width:300px;height:14px;font:bold 10px Verdana;color:#FFFF00;'>Image uploaded.</div></body></html>"
End If
End Sub

Sub modify()
If Session("delimg") = "" Then
Response.Write "<div style='background:#999999;width:300px;height:14px;font:bold 10px Verdana;color:#FFFF00;' id='noticeimg'>No image need to be updated!</div>"
Response.End
Else
If Request.QueryString("toup") = 1 Then
Dim rs,strSql,temp
strSql="update product set imgpath ='"&request2.SavePath&request2.Form("strPhoto")&"' where id="&Session("delimgid")
G_CONN.Execute strSql
Set rs = Server.CreateObject("ADODB.Recordset")
rs.open "select imgpath from product where id=" & Session("delimgid"),G_CONN,1,1
temp = rs("imgpath")
Set Jpeg=Server.CreateObject("Persits.Jpeg")
Path = Server.MapPath(temp)
Jpeg.Open Path
Jpeg.Width = Jpeg.OriginalWidth / 2
Jpeg.Height = Jpeg.OriginalHeight / 2
Jpeg.Quality = 90
Jpeg.Save Server.MapPath("upload\middle")&Mid(temp,7)
Jpeg.Width = Jpeg.OriginalWidth / 2
Jpeg.Height = Jpeg.OriginalHeight / 2
Jpeg.Save Server.MapPath("upload\small")&Mid(temp,7)
Set Jpeg = Nothing
rs.Close
Set rs = Nothing
Response.Write "<script>document.upimg.style.visibility='hidden';noticeimg.innerHTML='Image uploaded.'</script>"
End If
End If
End Sub
%>

由于刚学asp不久,水平挺菜的。。所以不足之处还有很多,望高手能够多多指出不足,好让小弟改进,衷心的谢谢大家!

[此贴子已经被作者于2007-8-20 17:21:55编辑过]

#12
阳光白雪2007-08-20 17:17
strAction = Trim(Request.QueryString("action"))
If strAction = "modify" Then
response.write("修改")
response.end()
Call modify() '修改商品图片
Else
response.write("新增")
response.end()
Call main() '添加新商品
End If

用这个试下执行了哪个动作,貌似 strAction 有问题
#13
slfyeye2007-08-20 17:24
以下是引用阳光白雪在2007-8-20 17:17:08的发言:
strAction = Trim(Request.QueryString("action"))
If strAction = "modify" Then
response.write("修改")
response.end()
Call modify() '修改商品图片
Else
response.write("新增")
response.end()
Call main() '添加新商品
End If

用这个试下执行了哪个动作,貌似 strAction 有问题

谢谢阳光白雪,刚才试过了这一段和自己的程序,都显示的是“修改”,说明strAction是没有问题的。
继续探索中。。。。

[此贴子已经被作者于2007-8-20 17:26:33编辑过]

#14
ayue2222007-08-20 17:26
strSql="update product set imgpath ='"&request2.SavePath&request2.Form("strPhoto")&"' where id="&Session("delimgid")
response.write strSql:response.end
G_CONN.Execute strSql

看看这个更新的时候提交的 id 和imgpath 是不是正确的 ..
#15
slfyeye2007-08-20 17:35
谢谢大家的帮助,刚才测试下来,发现我的session("delimgid")肯定有问题,不知道我前面的哪段程序将他清空了Session("delimgid")=""。详细情况我要再好好测试一下,等问题解决后再来谢谢大家!

[此贴子已经被作者于2007-8-20 17:35:17编辑过]


#16
slfyeye2007-08-21 10:18

已经解决,谢谢大家的帮助!昨天错误出在提交表单是漏了action=modify,导致提交后变成新增图片了。。。。看来编程需要相当细心啊。

1