PDA

View Full Version : Updating database with repeat region


djfenom
02-14-2006, 01:20 PM
I'm using repeat regions on my page to update a database, I can get it going with one repeat region, but when I add in a nested region my database is not updating. Here is my code:

<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="../Connections/alpine.asp" -->
<%
Dim RSprodcat
Dim RSprodcat_numRows

Set RSprodcat = Server.CreateObject("ADODB.Recordset")
RSprodcat.ActiveConnection = MM_alpine_STRING
RSprodcat.Source = "SELECT * FROM prodSubCat"
RSprodcat.CursorType = 0
RSprodcat.CursorLocation = 2
RSprodcat.LockType = 1
RSprodcat.Open()

RSprodcat_numRows = 0
%>
<%
' *** Restrict Access To Page: Grant or deny access to this page
MM_authorizedUsers=""
MM_authFailedURL="index.asp"
MM_grantAccess=false
MM_Username = Session("MM_Username")
If Session("MM_Username") <> "" Then
If (true Or CStr(Session("MM_UserAuthorization"))="") Or _
(InStr(1,MM_authorizedUsers,Session("MM_UserAuthorization"))>=1) Then
MM_grantAccess = true
End If
End If
If Not MM_grantAccess Then
MM_qsChar = "?"
If (InStr(1,MM_authFailedURL,"?") >= 1) Then MM_qsChar = "&"
MM_referrer = Request.ServerVariables("URL")
if (Len(Request.QueryString()) > 0) Then MM_referrer = MM_referrer & "?" & Request.QueryString()
MM_authFailedURL = MM_authFailedURL & MM_qsChar & "accessdenied=" & Server.URLEncode(MM_referrer)
Response.Redirect(MM_authFailedURL)
End If
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = -1
Repeat1__index = 0
RSprodlist_numRows = RSprodlist_numRows + Repeat1__numRows
%>
<!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=iso-8859-1" />
<title>Alpine Foods</title>
<style type="text/css">
<!--
@import url("../alpine.css");
-->
</style>
</head>
<body id="maintain">
<div id="container">
<div id="top"><a href="secure.asp"><img src="../images/logo.gif" alt="Alpine Foods" width="238" height="63" border="0" /></a></div>
<div id="navnpic"></div>
<div id="subnav"></div>
<div id="mainbody">
<h2><%= MM_Username %> Product List</h2>
<form method="post" action="product-confirm.asp" name="form1">
<%
While ((Repeat1__numRows <> 0) AND (NOT RSprodcat.EOF))
%>
<%
Set RSprodlist = Server.CreateObject("ADODB.Recordset")
RSprodlist.ActiveConnection = MM_alpine_STRING
RSprodlist.Source = "SELECT * FROM products WHERE productSubCategory = " & RSprodcat.Fields.Item("subCatID").Value & " AND " & MM_Username & " >= 1 ORDER BY productTitle ASC"
RSprodlist.CursorType = 0
RSprodlist.CursorLocation = 2
RSprodlist.LockType = 1
RSprodlist.Open()

Dim iLoop

Dim iCount
iCount = 0

Do While Not RSprodlist.EOF

if str1 <> (RSprodcat.Fields.Item("subCatTitle").Value) Then
Response.Write "<h3>" & ((RSprodcat.Fields.Item("subCatTitle").Value)) & "</h3>" & vbCrLf
end if
str1 = RSprodcat.Fields.Item("subCatTitle").Value

Response.Write "<dl>"
Response.Write vbCrLf & "<dt><input type=""text"" class=""quantity"" name=""" & iCount & ".quant"" value=""" & _
RSprodlist(MM_Username) & """" & ">" & vbCrLf
Response.Write "<input type=""hidden"" name=""" & iCount & ".ID"" " & _
"value=""" & RSprodlist("productID") & """>"
Response.Write "</dt>" & vbCrLf
Response.Write "<dd>" & RSprodlist("productTitle") & "</dd>" & vbCrLf
Response.Write "</dl>" & vbCrLf & vbCrLf

RSprodlist.MoveNext
iCount = iCount + 1
Loop
%>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
RSprodcat.MoveNext()
Wend
%>
<div class="cleaner"></div>

<p>
<input type="submit" name="Submit" value="Place Order" />
<input name="add" type="button" class="button" value="Add Products" onclick="window.location.href=('product-full-list.asp');" />
</p>
<input type="hidden" name="Count" value="<%=iCount - 1 %>" />
</form>
<div class="cleaner"></div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
<%
RSprodlist.Close()
Set RSprodlist = Nothing
%>
<%
RSprodcat.Close()
Set RSprodcat = Nothing
%>

It appears that the count isn't working properly as it's displayed as "-1" at the moment in the hidden field, whereas if I remove the RSprodcat repeat region it works fine and displays the correct count. The next page is working fine also when this is removed, here is the code for that:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../Connections/alpine.asp" -->
<%
' *** Restrict Access To Page: Grant or deny access to this page
MM_authorizedUsers=""
MM_authFailedURL="index.asp"
MM_grantAccess=false
MM_Username = Session("MM_Username")
If Session("MM_Username") <> "" Then
If (true Or CStr(Session("MM_UserAuthorization"))="") Or _
(InStr(1,MM_authorizedUsers,Session("MM_UserAuthorization"))>=1) Then
MM_grantAccess = true
End If
End If
If Not MM_grantAccess Then
MM_qsChar = "?"
If (InStr(1,MM_authFailedURL,"?") >= 1) Then MM_qsChar = "&"
MM_referrer = Request.ServerVariables("URL")
if (Len(Request.QueryString()) > 0) Then MM_referrer = MM_referrer & "?" & Request.QueryString()
MM_authFailedURL = MM_authFailedURL & MM_qsChar & "accessdenied=" & Server.URLEncode(MM_referrer)
Response.Redirect(MM_authFailedURL)
End If
%>
<%
Response.Buffer = True

'First, we need to get the total number of items that could be updated
Dim iCount
iCount = Request("Count")


'We will also need to build a SQL statement
Dim strSQL

Dim RSprodupdate
set RSprodupdate = server.CreateObject("adodb.connection")
RSprodupdate.open MM_alpine_STRING

'Now, we want to loop through each form element
Dim iLoop
For iLoop = 0 to iCount
strQuant = Request(iLoop & ".quant")
strID = Request(iLoop & ".ID")

sql = " UPDATE products SET " & MM_Username & " = " & CDBl(strQuant) & _
" WHERE productID = " & strID
'response.write(sql)
RSprodupdate.Execute(sql)
Next

RSprodupdate.Close
Set RSprodupdate = Nothing

%>
<%
Dim RSprodlist__MMColParam
RSprodlist__MMColParam = "1"
If (Request("MM_EmptyValue") <> "") Then
RSprodlist__MMColParam = Request("MM_EmptyValue")
End If
%>
<%
Dim RSprodlist
Dim RSprodlist_numRows

Set RSprodlist = Server.CreateObject("ADODB.Recordset")
RSprodlist.ActiveConnection = MM_alpine_STRING
RSprodlist.Source = "SELECT * FROM products WHERE " & MM_Username & " >= 1 ORDER BY productTitle ASC"
RSprodlist.CursorType = 0
RSprodlist.CursorLocation = 2
RSprodlist.LockType = 1
RSprodlist.Open()

RSprodlist_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = -1
Repeat1__index = 0
RSprodlist_numRows = RSprodlist_numRows + Repeat1__numRows
%>
<!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=iso-8859-1" />
<title>Alpine Foods</title>
<style type="text/css">
<!--
@import url("../alpine.css");
-->
</style>
</head>
<body id="maintain">
<div id="container">
<div id="top"><a href="secure.asp"><img src="../images/logo.gif" alt="Alpine Foods" width="238" height="63" border="0" /></a></div>
<div id="navnpic"></div>
<div id="subnav"></div>
<div id="mainbody">
<h2><%= MM_Username %> Order Confirmation </h2>
<form id="form1" name="form1" method="post" action="product-confirm.asp">
<%
While ((Repeat1__numRows <> 0) AND (NOT RSprodlist.EOF))
%>
<p><%=(RSprodlist.Fields.Item(MM_Username).Value)%> x <%=(RSprodlist.Fields.Item("productTitle").Value)%> </p>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
RSprodlist.MoveNext()
Wend
%>
<p>
<input type="submit" name="Submit" value="Place Order" />
</p>
<p>&nbsp;</p>
</form>
<div class="cleaner"></div>
</div>
<div id="footer"></div>
</div>
</body>
</html>
<%
RSprodlist.Close()
Set RSprodlist = Nothing
%>

Any help would be appreciated.

Many Thanks

Chris

Baleric
02-15-2006, 06:51 AM
hey chris, just noticed u are using dreamweaver to make your restrict access to pages, and most likey your login pages,
i used to use it to, then relised that every single time you use the script it uses the session MM_username, wich isnt very secure, i still use dreamweaver to make recordsets but everything else i do myself, due to buggs, eg. u can only use 1 update script per page and no delete on the same page either.


basically if i use the scipt on a page i make then goto ur URL and ill have access to everything you have in ur admin pages with out loging in.
i recomend u use ur own login script.

-baleric

djfenom
02-15-2006, 10:28 AM
Thanks for the tip Baleric, I'll get that changed. Any ideas on my other problem?