Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code to check if a post contains a value, and increas it.

Status
Not open for further replies.

Beracosta

Programmer
Oct 25, 2000
47
SE
I have a problem in my shopping basket. If i try to put a merchant in the basket and the merchant already is there, i get a error msg... but that isn't how i want it... i want it to increas the Quantity instead. Can u help me? The code i have now is:

<%
Dim oCmd
Dim BasketID, MerchantID, Quantity
Dim strSQL

BasketID = Request(&quot;BasketID&quot;)
MerchantID = Request(&quot;MerchantID&quot;)
Quantity = Request.Form(&quot;Quantity&quot;)

Set oCmd = Server.CreateObject(&quot;ADODB.Command&quot;)
With oCmd
.ActiveConnection = &quot;DSN=SYCENT;&quot;
strSQL = &quot;INSERT INTO BasketContent(BasketID, MerchantID, Quantity) VALUES(&quot; & BasketID & &quot;, &quot; & MerchantID & &quot;, &quot; & Quantity & &quot;)&quot;
.CommandText = strSQL
.Execute
Set .ActiveConnection = Nothing
End With

Set oCmd = Nothing

Response.Redirect &quot;ShowArt.asp?ID=&quot; & Request(&quot;MerchantID&quot;)
%>

can anybody help me to increase the Quantity if the BasketID already contains a MerchantID? thanx in advance...
 
If the basketID already contains a merchantID then you would have to use an update statement.

You could store the basket id and merchant ids associated with it in a dictionary object or some other data structure and check it before issuing the sql ststement. If they are there then you could then issue an update statment instead.

Another option is to issue the following SELECT statement and check the record count:

Code:
dim oRs

set oRs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
strSQL = &quot;SELECT Quantity FROM BasketContent WHERE BasketID = &quot;
strSQL = strSQL & BasketID
strSQL = strSQL & &quot; AND MerchantID = &quot;
strSQL = strSQL & MerchantID
oRs.Source = strSQL
oRs.ActiveConnection = &quot;DSN=SYCENT&quot;
oRs.Open

if oRs.RecordCount = 0 then
  strSQL = &quot;UPDATE BasketContent SET Quantity = Quantity + &quot;
  strSQL = strSQL & Quantity
  strSQL = strSQL & &quot; WHERE BasketID = &quot;
  strSQL = strSQL & BasketID
  strSQL = strSQL & &quot; AND MerchantID = &quot;
  strSQL = strSQL & MerchantID
else
  strSQL = &quot;INSERT INTO BasketContent(BasketID, MerchantID, Quantity) VALUES(&quot; & BasketID & &quot;, &quot; & MerchantID & &quot;, &quot; & Quantity & &quot;)&quot;
end if
   
&quot;)

Set oCmd = Server.CreateObject(&quot;ADODB.Command&quot;)
with oCmd 
  .ActiveConnection = &quot;DSN=SYCENT&quot;
  .CommandText = strSQL
  .Execute
  Set .ActiveConnection = Nothing
End With

Hope this helps.

James :)
James Culshaw
jculshaw@active-data-solutions.co.uk
 
i didn't work. i get the error msg that the post already exists or something like that! =( what could it be? Pls help me even further James... i'm realy thankful for the help sofar! carry on the good work!
 
Have you got a unique index on the BasketID. This might cause the problem. It means that you can only have one entry per basket regardless of the MerchantID.
If you could post the exact error message and give a clearer description of what you are doing it would help immensly.

James :) James Culshaw
jculshaw@active-data-solutions.co.uk
 
Thanx for the help James... i will post the exakt error msg later but i have to translate it into english first. And that is a big problem because when they translate the English to Swedish they translate it very bad so when i translate it back, the error won't be the same error msg that u are just to. hehe but what the hell... i'll give it a shot... hope u can understand it =)

Björn Helin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top