Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access_asp thread: Append double record !


Message #1 by "Remus Bodoni" <remusb@a...> on Tue, 17 Sep 2002 18:54:13
I (try) make a e-shop, with book "Teach Yourself E-Commerce Programming 
with ASP in 21 Days", but now I have a problem: when I wont to append a 
new record in my database, append double record. I can't to find where is 
mistake. Can somebody to help my? Thanks.

CODE SOURCE (addcart.asp):
<%
' Get Product ID
productID = TRIM( Request( "pid" ) )
%>

<%
' Add Item to cart
IF productID <> "" THEN
  sqlString = "SELECT cart_id FROM cart " &_
    "WHERE cart_userID= " & userID & " " &_
    "AND TRIM(cart_productID)= TRIM('& productID &') "  
    set RS = Con.Execute( sqlString )
  IF RS.EOF THEN
  	 sqlString = "INSERT INTO cart ( cart_userID, cart_productID, 
cart_quantity )" &_ 
	 " VALUES ( "&userID&", '"&productID&"', 7 )"   
  ELSE
  	 sqlString = "UPDATE cart SET " &_
     "cart_quantity=cart_quantity+1 " &_
     "WHERE cart_id=" & RS( "cart_id" ) 
  END IF
  RS.Close
  SET RS = Nothing
  Con.Execute sqlString
END IF

' Update Shopping Cart Quantities
IF Request( "updateQ" ) <> "" THEN
SET RS = Server.CreateObject( "ADODB.Recordset" )
RS.ActiveConnection = Con
RS.LockType = adLockOptimistic
sqlString = "SELECT cart_id, cart_quantity FROM cart " &_
  "WHERE cart_userID=" & userID
RS.Open sqlString

WHILE NOT RS.EOF
newQ1 = TRIM( Request( "pq" & RS( "cart_id" ) ) )
newQ2 = TRIM( Request( "pd" & RS( "cart_id" ) ) )
deleteProduct = TRIM( Request( "pq" & "pd" & RS( "cart_id" ) ) )
IF newQ1 = "" OR newQ1 = "0" OR newQ2 = "1" THEN
  RS.Delete
ELSE
  IF isNumeric( newQ1 ) THEN
    RS( "cart_quantity" ) = newQ1
  END IF
  IF isNumeric( newQ2 ) THEN
    RS( "cart_quantity" ) = newQ2
  END IF  
END IF
RS.MoveNext
WEND


RS.Close
SET RS = Nothing
END IF
%>
<html>
<head><title>Cosul dvs. de cumparaturi:</title></head>
<body bgcolor="white">

<center>
<font face="Arial" size=3 color="darkgreen">
<b>Cosul de cumparaturi pentru clientul:<%=username%></b>
</font>

<%
' Get the shopping cart
sqlString = "SELECT cart_ID, product_name, " &_
  "product_price, cart_productID, cart_quantity " &_
  "FROM cart, products " &_
  "WHERE cart_userID=" & userID & " " &_
  "AND TRIM(cart_productID) = TRIM(cod) " &_
  "ORDER BY cart_ID DESC"
SET RS = Con.Execute( sqlString )

IF RS.EOF THEN
%>
<p><b>Nu aveti nici un produs in cosul de cumparaturi !</b>
<p>
<form action="default.asp">
<input type="submit" value="Continua cumparaturile">
</form>
<% 
ELSE 
orderTotal = 0
%>
<form method="post" action="cart.asp">
<input name="updateQ" type="hidden" value="1">
<input name="username" type="hidden" value="<%=username%>">
<input name="password" type="hidden" value="<%=password%>">
<table bgcolor="lightyellow" border=0 cellpadding=4 cellspacing=0>
<tr bgcolor="lightgreen">
  <th>Produs</th>
  <th>Pret</th>
  <th>Cantitate</th>
  <th></th>  
</tr>
<% 
WHILE NOT RS.EOF 
orderTotal = orderTotal + ( RS( "product_price" ) * RS( "cart_quantity" ) )
%>
<tr>
  <td>
  <%=Server.HTMLEncode( RS( "product_name" ) )%>
  </td>
  <td align="right">
  <%=formatNumber( RS( "product_price" ),0 )%>
  </td>
  <td align="center">
   <input name="pq<%=RS( "cart_id" )%>" type="text" size=4
   value="<%=RS( "cart_quantity" )%>">
  </td>
  <td align="center">
   <input name="pd<%=RS( "cart_id" )%>" 
   type="checkbox" value="1" >Renunta
  </td>
</tr>
<% 
RS.MoveNext
WEND
%>
<tr bgcolor="yellow">
  <td colspan=3 align=right>
  <b>Total factura:</b>
  </td>
  <td align=left>
  <%=formatNumber( orderTotal )%>
  </td>
</tr>
<tr>
  <td colspan=3>
  <table border=0>
  <tr>
    <td align="center">
	<input type="submit" value="Recalculeaza">
	</td>
	</form>
	<form method="post" action="checkout.asp">
	<input name="username" type="hidden" value="<%=username%>">
	<input name="password" type="hidden" value="<%=password%>">
	<td>
	<input type="submit" value="Confirma comanda">
	</td>
	</form>
	<form action="default.asp">
	<td>
	<input type="submit" value="Continua cumparaturile">
	</td>
	</form>
  </tr>
  </table>
  </td>
</tr>
</table>
<% END IF %>


</center>

</body>
</html>
Message #2 by "Rob Parkhouse" <rparkhouse@o...> on Wed, 18 Sep 2002 00:55:54
Perhaps your first SELECT query is not doing what you think. You are 
checking to see if the record exists and then if it does not, you are 
inserting a new record. Check the initial SELECT query by pasting into 
Access and running to see that nothing is returned. 

Or add "Response.Write sqlString" to see what it actually contains.

Good luck


' Add Item to cart
IF productID <> "" THEN
  sqlString = "SELECT cart_id FROM cart " &_
    "WHERE cart_userID= " & userID & " " &_
    "AND TRIM(cart_productID)= TRIM('& productID &') "  

' check Select query
Response.Write sqlString

    set RS = Con.Execute( sqlString )
  IF RS.EOF THEN
  	 sqlString = "INSERT INTO cart ( cart_userID, cart_productID, 
cart_quantity )" &_ 
	 " VALUES ( "&userID&", '"&productID&"', 7 )"   
  ELSE
  	 sqlString = "UPDATE cart SET " &_
     "cart_quantity=cart_quantity+1 " &_
     "WHERE cart_id=" & RS( "cart_id" ) 
  END IF
  RS.Close
  SET RS = Nothing
  Con.Execute sqlString
END IF

  Return to Index