asp写入数据库的问题,急急~!~!

zeng_job 2009-07-16 02:36:44
AJAX的查询页面
<%
Response.Charset="gb2312"
Dim provider_id,class_id
provider_id=request("nm")
class_id=request("q")
'Response.Write (provider_id)
set rs=server.createobject("adodb.recordset")
sql="select * FROM product where class_id='"&class_id&"' and provider_id='"&provider_id&"' and online=1"
rs.open sql,conn,1,1
If rs.eof Then
Else
response.write ("<table width=""700"" border=""0"" align=""center"" cellpadding=""0"" cellspacing=""0"" bordercolor=""#CCCCCC"">")
response.write "<tr><td align=""center"" valign=""middle""><table width=""100%"" border=""1"" align=""center"" cellpadding=""0"" cellspacing=""1"" bordercolor=""#CCCCCC"">"
response.write "<tr><td width=""63"" align=""center"">商品编号</td><td width=""140"" align=""center"">商品名称</td><td width=""288"" align=""center"">规 格</td><td width=""60"" align=""center"" nowrap>单 价(¥)</td><td width=""52"" align=""center"">数 量</td><td width=""76"" align=""center"">小计(¥)</td></tr>"
if not rs.eof then
i=1
do while not rs.eof
response.write ("<tr><td width=""63"" align=""center"">"&rs("product_id")&"")
response.write "<input id=""product_id"" value='"&rs("product_id")&"' type=""hidden""/></td>" '商品编号
response.write ("<td width=""140"" align=""center"">"&rs("product_name")&"</td>")
response.write ("<td width=""288"" align=""center"">"&rs("specification")&"</td>")
response.write ("<td width=""60"" align=""center"">"&rs("price")&"")
response.write "<input name=""price"" id='price"&i&"' value='"&rs("price")&"' type=""hidden""/></td>" '商品编号
response.write ("<td align=""center"" valign=""middle"" nowrap>")
response.write ("<input name=""quantity"" id='"&i&"' value=""0"" type=""text"" onKeyUp=""value=value.replace(/[^\d|]/g,'')"" size=""5"" maxlength=""5"" onBlur=""sum(this)""/>")'商品数量
response.write ("<td width=""76"" align=""center"">")
response.write "<input name=""amount"" id='amount"&i&"' readonly=""true""/></td>"
response.write("</tr>")
i=i+1
rs.movenext
loop
end if
end if
rs.close
conn.close
response.write "</table>"
response.write "<tr><td align=""center"" valign=""middle""><table width=""100%"" border=""1"" align=""center"" cellpadding=""0"" cellspacing=""1"" bordercolor=""#CCCCCC"">"
response.write "<tr><td align=""center"">合 计:</td>"
response.write "<td width=""223"" align=""right"">总 量:<input name=""number_sum"" type=""text"" size=""5"" readonly=""ture"" style=""font:bolder;color:#FF0000""/></td>"
response.write "<td width=""158"" align=""center"">总价:<input name=""amount_sum"" type=""text"" size=""8"" readonly=""ture"" style=""font:bolder;color:#FF0000""/>元</td>"
response.write "</tr>"
response.write "<tr><td align=""center"" valign=""middle"">备注:<span style=""color: #FF0000"">*</span></td>"
response.write "<td colspan=""2"" align=""left"" valign=""middle""><textarea name=""remark"" cols=""52"" rows=""3""></textarea></td>"
response.write "</tr>"
response.write "</table></td>"
response.write "<tr><td height=""22"" colspan=6>"
response.write "</td></tr>"
response.write "</table>"
'%>
在<input name=""quantity""/> 填写数据后提交,把product_id,price,quantity,写入数据库,AJAX查询的记录可能是1条,也可能是10条.
...全文
103 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
zeng_job 2009-07-20
  • 打赏
  • 举报
回复
恩,在接收的时候把空格处理掉了,在插入语句里面处理好像不行,谢谢解决问题了.
toury 2009-07-20
  • 打赏
  • 举报
回复
改两个地方:
1)sql2="INSERT INTO [OrderDetails](order_id,product_id,Price,quantity) VALUES('"&order_id&"','"&arrPID(i)&"','"&arrPrice(i)&"','"&arrQty(i)&"')"
---->
sql2="INSERT INTO [OrderDetails](order_id,product_id,Price,quantity) VALUES('"&order_id&"','"&trim(arrPID(i))&"','"&trim(arrPrice(i))&"','"&trim(arrQty(i))&"')"
(或者在上面接收数据的时候就把空格处理掉。你还是没仔细看我让你看的那个参考地址)

2)加蓝字
else'有错误
response.write "写入数据库出错!原因:"& err.description
response.End
end if
调皮的蟠桃 2009-07-20
  • 打赏
  • 举报
回复
学习啦!
zeng_job 2009-07-20
  • 打赏
  • 举报
回复
toury老大 ,数据还是写入不了数据库哦,而且在第二条插入记录前都会有空格' CLST003',' 168',' 11',这是为什么啊?
<% sProduct_id=request.form("product_id")
if len(sProduct_id)=0 then
response.write("参数传递错误!无法写库")
response.end
end if
sPrice=request.form("price")'同上
sQuantity=request.form("quantity")'同上
arrPID=split(sProduct_id,",")
arrPrice=split(sPrice,",")
arrQty=split(sQuantity,",")
for i=0 to ubound(arrPID)
sql2="INSERT INTO [OrderDetails](order_id,product_id,Price,quantity) VALUES('"&order_id&"','"&arrPID(i)&"','"&arrPrice(i)&"','"&arrQty(i)&"')"
response.write sql2&" <br>"
conn.execute sql2
next
if err.number=0 then'没错误
response.write "<script language=javascript>alert('下单成功!');window.location.href='order_new.asp';</script>"
response.End
else'有错误
response.write "写入数据库出错!"
response.End
end if
%>下面是打印的SQL语句.
INSERT INTO [OrderDetails](order_id,product_id,Price,quantity) VALUES('CLST200972014143818','CLST002','110','11')
INSERT INTO [OrderDetails](order_id,product_id,Price,quantity) VALUES('CLST200972014143818',' CLST003',' 168',' 11')
写入数据库出错!
toury 2009-07-17
  • 打赏
  • 举报
回复
先按照我上面的提示并参考我给你的那个网页,把上面的代码改一下发出来。
还有更严重的其他问题呢,例如你的订单表和订单明细表之间没有关联字段吗?将来怎么查询啊??这个回头有时间再说吧。先把你眼下的问题解决掉
toury 2009-07-17
  • 打赏
  • 举报
回复
我叫你老大才对,哈,看下面红字
<%
dim MyValue
Function RndNumber(MaxNum,MinNum)'随机数
Randomize
RndNumber=int((MaxNum-MinNum+1)*rnd+MinNum)
RndNumber=RndNumber
End Function
MyValue=RndNumber(30,10)

dim order_id
pnm=request.form("provider_name")'供应商
pnm=RTrim(pnm)
dately=cstr(year(date())&Month(date())&day(date())&Hour(now())&Minute(now())&Second(now()))
order_id=cstr(pnm&dately&MyValue)'订单号
order_name = request.form("order_name")
order_tel = request.form("order_tel")
order_mob = request.form("order_mob")
order_address = request.form("order_address")
area_code = request.form("area_code")
remark = request.form("remark")
consignee_name = request.form("consignee_name")
consignee_tel = request.form("consignee_tel")
consignee_mob = request.form("consignee_mob")
consignee_address = request.form("consignee_address")

''**************************添加订单表ordertab*****************
set rs1=server.createobject("adodb.recordset")
sql1="select * from ordertab"
rs1.open sql1,conn,1,3
rs1.addnew
rs1("order_id")=order_id
rs1("remark") = remark
rs1("order_date")=date()
rs1("status")=0 '下单状态为0 '确认为1,已发货为2,撤销为3,
rs1("express_id") = ""
rs1("express_in") = ""
rs1("order_name")=order_name
rs1("order_tel")=order_tel
rs1("order_mob")=order_mob
rs1("order_address") = order_address
rs1("area_code") = area_code
rs1("consignee_name") = consignee_name
rs1("consignee_tel") = consignee_tel
rs1("consignee_mob") = consignee_mob
rs1("consignee_address") = consignee_address
rs1("provider_id") =pnm
rs1("importer")=Session("AdminName")
rs1("update_time")=now()
rs1.update
rs1.close
'以上部分你没贴过我也不知道,但应该没什么错,我就不看了。

'**************************添加到订单详表OrderDetails*****************
'这部分我知道,请看:
set product_id = request("product_id") ' 问题1: SET 是什么意思?
set price = request("price") ' 问题1:SET 是什么意思?
set quantity = request("quantity") ' 问题1:SET 是什么意思?
for i=1 to product_id.count ' 问题2:product_id.count是什么意思?

'应该这样取数据:::::::::
sProduct_id=request.form("product_id")'得到是是一个类似"1, 199, 3, 68,...."这样的字符串。我让你看的例子里说的很清楚

'判断是否没传过来参数(容错)
if len(sProduct_id)=0 then
response.write("参数传递错误!无法写库")
response.end
end if

sPrice=request.form("price")'同上
sQuantity=request.form("quantity")'同上
'蓝字一定要和前页的控件NAME对应起来!!
'运用SPLIT将字串转换为数组
arrPID=split("sProduct_id",",")
arrPrice=split("sPrice",",")
arrQty=split("sQuantity",",")

for i=0 to ubound(arrPID)
sql="INSERT INTO [OrderDetails](......) VALUES(................)"
conn.execute sql
next
'set rs2=server.createobject("adodb.recordset")
'sql2="select * from OrderDetails"
'rs2.open sql2,conn,1,3
'rs2.addnew
'rs2("order_id")=order_id
'rs2("product_id") = product_id(i)
'rs2("quantity") = quantity(i)
'rs2("price") = price(i)
'rs2.update
'rs2.close
'你下面这句"下单成功"的一句是什么?不是自欺欺人吗?
'response.write " <script language=javascript>alert('下单成功!');
window.location.href='order_new.asp'; </script>"
'应该判断一下:
if err=0 then'没错误
'..........
else'有错误
'..............
end if
response.End
%>
zeng_job 2009-07-17
  • 打赏
  • 举报
回复

<%
dim MyValue
Function RndNumber(MaxNum,MinNum)'随机数
Randomize
RndNumber=int((MaxNum-MinNum+1)*rnd+MinNum)
RndNumber=RndNumber
End Function
MyValue=RndNumber(30,10)

dim order_id
pnm=request.form("provider_name")'供应商
pnm=RTrim(pnm)
dately=cstr(year(date())&Month(date())&day(date())&Hour(now())&Minute(now())&Second(now()))
order_id=cstr(pnm&dately&MyValue)'订单号
order_name = request.form("order_name")
order_tel = request.form("order_tel")
order_mob = request.form("order_mob")
order_address = request.form("order_address")
area_code = request.form("area_code")
remark = request.form("remark")
consignee_name = request.form("consignee_name")
consignee_tel = request.form("consignee_tel")
consignee_mob = request.form("consignee_mob")
consignee_address = request.form("consignee_address")
''**************************添加订单表ordertab*****************
set rs1=server.createobject("adodb.recordset")
sql1="select * from ordertab"
rs1.open sql1,conn,1,3
rs1.addnew
rs1("order_id")=order_id
rs1("remark") = remark
rs1("order_date")=date()
rs1("status")=0 '下单状态为0 '确认为1,已发货为2,撤销为3,
rs1("express_id") = ""
rs1("express_in") = ""
rs1("order_name")=order_name
rs1("order_tel")=order_tel
rs1("order_mob")=order_mob
rs1("order_address") = order_address
rs1("area_code") = area_code
rs1("consignee_name") = consignee_name
rs1("consignee_tel") = consignee_tel
rs1("consignee_mob") = consignee_mob
rs1("consignee_address") = consignee_address
rs1("provider_id") =pnm
rs1("importer")=Session("AdminName")
rs1("update_time")=now()
rs1.update
rs1.close
'**************************添加到订单详表OrderDetails*****************
set product_id = request("product_id")
set price = request("price")
set quantity = request("quantity")
for i=1 to product_id.count
set rs2=server.createobject("adodb.recordset")
sql2="select * from OrderDetails"
rs2.open sql2,conn,1,3
rs2.addnew
rs2("order_id")=order_id
rs2("product_id") = product_id(i)
rs2("quantity") = quantity(i)
rs2("price") = price(i)
rs2.update
rs2.close
response.write "<script language=javascript>alert('下单成功!');window.location.href='order_new.asp';</script>"
response.End
%>
准备分别写入数据库的ordertab,OrderDetails两个表里,谢谢toury 老大的帮助.
zeng_job 2009-07-17
  • 打赏
  • 举报
回复
谢谢toury老大的大力帮助,没有toury的帮助我要搞一个礼拜哦.
toury 2009-07-17
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 chenlongit 的回复:]
toury 大佬 就是强大  ^_^
[/Quote]
兄弟,你就别臭我啦,犯错误了:(((
MR丶CHAN 2009-07-17
  • 打赏
  • 举报
回复
toury 大佬 就是强大 ^_^
toury 2009-07-17
  • 打赏
  • 举报
回复
兄弟,由于我打字笔误,耽误你一下午时间,汗~~
看来我要休息一下了
toury 2009-07-17
  • 打赏
  • 举报
回复
老大!!!!!!是我糊涂啊,对不起!!!!
'运用SPLIT将字串转换为数组
arrPID=split("sProduct_id",",")
arrPrice=split("sPrice",",")
arrQty=split("sQuantity",",")



'运用SPLIT将字串转换为数组
arrPID=split(sProduct_id,",")
arrPrice=split(sPrice,",")
arrQty=split(sQuantity,",")


天啊,我犯这样低级的错误,实在该打,唉!
toury 2009-07-17
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 zeng_job 的回复:]
老大,打印出来是: INSERT INTO [OrderDetails](order_id,product_id,Price,quantity) VALUES('CLST20097171744218','sProduct_id','sPrice','sQuantity')
CLST20097171744218这是随机订单号,好像是字符串转数组不成功是吧?
[/Quote]
这怎么可能呢?
VALUES('CLST20097171744218','sProduct_id','sPrice','sQuantity')?????

你12楼不是告诉我:
对应sProduct_id字段的数据是:【CLST002, CLST003】
对应sPrice字段的数据是:【110, 168】
对应quantity字段的数据是:【1, 1】
zeng_job 2009-07-17
  • 打赏
  • 举报
回复
老大,打印出来是: INSERT INTO [OrderDetails](order_id,product_id,Price,quantity) VALUES('CLST20097171744218','sProduct_id','sPrice','sQuantity')
CLST20097171744218这是随机订单号,好像是字符串转数组不成功是吧?
toury 2009-07-17
  • 打赏
  • 举报
回复
<%
sProduct_id=request.form("product_id")
if len(sProduct_id)=0 then
response.write("参数传递错误!无法写库")
response.end
end if
sPrice=request.form("price")'同上
sQuantity=request.form("quantity")'同上
'Response.write("对应sProduct_id字段的数据是:【"&(sProduct_id)&"】 <br>")
'Response.write("对应sPrice字段的数据是:【"&(sPrice)&"】 <br>")
'Response.write("对应quantity字段的数据是:【"&(sQuantity)&"】 <br>")
arrPID=split("sProduct_id",",")
arrPrice=split("sPrice",",")
arrQty=split("sQuantity",",")
for i=0 to ubound(arrPID)
sql2="INSERT INTO [OrderDetails](order_id,product_id,Price,quantity) VALUES('"&order_id&"','"&arrPID(i)&"','"&arrPrice(i)&"','"&arrQty(i)&"')"
response.write sql2&"<br>"'打印出SQL语句拷贝到数据库查询里走一下看什么问题
conn.execute sql2
next
if err.number=0 then
response.write " <script language=javascript>alert('下单成功!');window.location.href='order_new.asp'; </script>"
response.End
else
response.write "写入出错误!原因:"& err.description
response.End
end if
%>
zeng_job 2009-07-17
  • 打赏
  • 举报
回复
<%
sProduct_id=request.form("product_id")
if len(sProduct_id)=0 then
response.write("参数传递错误!无法写库")
response.end
end if
sPrice=request.form("price")'同上
sQuantity=request.form("quantity")'同上
'Response.write("对应sProduct_id字段的数据是:【"&(sProduct_id)&"】<br>")
'Response.write("对应sPrice字段的数据是:【"&(sPrice)&"】<br>")
'Response.write("对应quantity字段的数据是:【"&(sQuantity)&"】<br>")
arrPID=split("sProduct_id",",")
arrPrice=split("sPrice",",")
arrQty=split("sQuantity",",")
for i=0 to ubound(arrPID)
sql2="INSERT INTO [OrderDetails](order_id,product_id,Price,quantity) VALUES('"&order_id&"','"&arrPID(i)&"','"&arrPrice(i)&"','"&arrQty(i)&"')"
conn.execute sql2
next
if err.number=0 then
response.write "<script language=javascript>alert('下单成功!');window.location.href='order_new.asp';</script>"
response.End
else
response.write "(写入出错误!)"
response.End
end if
%>
上面是插入数据库代码,字段都取到了;但插入数据库还是出错,写入不了,对了OrderDetails表有ID字段,是自增长的.所以语句里面没有写.
对应sProduct_id字段的数据是:【CLST002, CLST003】
对应sPrice字段的数据是:【110, 168】
对应quantity字段的数据是:【1, 1】

订单表和订单明细表之间有关联字段是订单号:order_id字段,谢谢toury 老大.
toury 2009-07-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zeng_job 的回复:]
我想可能我没有说清楚,我现在是后台asp页面"add.asp"写不进数据库。按照toury 老大给的参考地址的方法也没有成功,资质愚笨啊。
[/Quote]
贴add.asp代码,我LOOK一下,呵呵
zeng_job 2009-07-16
  • 打赏
  • 举报
回复
我想可能我没有说清楚,我现在是后台asp页面"add.asp"写不进数据库。按照toury 老大给的参考地址的方法也没有成功,资质愚笨啊。
spkl1 2009-07-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 spkl1 的回复:]
onchange()每选取一个选项,就通过js赋值给另外的控件a,如果已经存在则不再赋值
最后通过request.form("a")获取
[/Quote]
回错页面了
spkl1 2009-07-16
  • 打赏
  • 举报
回复
onchange()每选取一个选项,就通过js赋值给另外的控件a,如果已经存在则不再赋值
最后通过request.form("a")获取
加载更多回复(4)

28,406

社区成员

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

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