473,609 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

updating

Hi everyone
I'm trying to build a shopping cart app using a db the part I'm stuck
on is the fact that, if someone adds a product that they have
previously added to the cart.
I've got it set up to check whether the size and colour fields match
what's in the db, if they do then you've already added this item with
the same colours and size so, only update the quantity field, if they
don't match, then insert a new record because the size and colours are
different so, this is a different item.

It works when for the first item added but not the second e.i.
I could add
Jacket large blue
then
jacket small red

when i go back and add another Jacket large blue it does what it's
supposed to, only updates the quantity, but when i try the other one
it inserts a new record

Here's the code

table = String(Request. Cookies("table" ));

var rsproducts = Server.CreateOb ject("ADODB.Rec ordset");
rsproducts.Acti veConnection = conn_STRING;
rsproducts.Sour ce = "SELECT * FROM " + table ;
rsproducts.Curs orType = 0;
rsproducts.Curs orLocation = 2;
rsproducts.Lock Type = 1;
rsproducts.Open ();
var rsproducts_numR ows = 0;
// check to see is the item is there, if so, check if size and colour
fields match if so, only update the quantity with the value from the
Quantity field from products page

if (!rsproducts.EO F && String(rsproduc ts.Fields.Item( "size").Val ue) ==
size && String(rsproduc ts.Fields.Item( "colour").Value ) == colour &&
String(rsproduc ts.Fields.Item( "ProductName"). Value)){

conn = Server.CreateOb ject('ADODB.Com mand');
conn.ActiveConn ection = conn_STRING;
conn.CommandTex t = ("UPDATE "+ table + " SET Quantity = Quantity +
'"+Quantity+ "' WHERE ProductID='"+Pr oductID+"'AND
ID='"+String(rs products.Fields .Item("ID").Val ue)+"'" );
conn.Execute();
conn.ActiveConn ection.Close();
Response.Redire ct("../index.asp");
}else{// if item does not match with others then insert new record
//create sql insert
var sql = "INSERT INTO "+ table +
"(ProductID,Pro ductName,Price, Quantity,";
sql = sql+ "size,colour,Pr oductCode,NavID ,groupfield)";
sql = sql+" VALUES ('"+ProductID+" ','"+ProductNam e+"','"+Price+" ','"+Quantity+" ','"+size+"'";
sql = sql+ ",'"+colour+"', '"+ProductCode+ "','"+NavID+"', '1')";

conn = Server.CreateOb ject('ADODB.Com mand');
conn.ActiveConn ection = conn_STRING;
conn.CommandTex t = (sql);
conn.Execute();//do the job
conn.ActiveConn ection.Close();
Response.Redire ct("../index.asp");
}
I was thinking maybe a loop of some sort but don't quite know
Any ideas?
Thank's in advance
Roy
Jul 19 '05 #1
1 2045
I'm not entirely sure how it is working the first time around, my guess would
be luck.

My assumption is that this table your passing around is the name of a
temporary table cretd for that one user, otherwise I am really clueless how
it is working the first time.

Yes, you would need some sort of loop, as right now all your checking
against is tat the new item does not match one of the items in your table
(whichever one happens to appear at the top of the recordset).

I can make some sugestions, but they will be rather general without knowing
more info.

One, I don't suggest opening the whole table and looping through it, but if
you are going to do so you will need to add a few things. Before your if
check to match products against the recordset you need to queue the recordset
up to tthe top record, something like:
if(!rsproducts. EOF)
rsProducts.Move First();
The a simple while !EOF loop would allow you to check agaist all the
records. My advice would be to have a boolean variable and do something like
(warning: my javascript is very rusty):
if(!rsproducts. EOF)
rsproducts.Move First();

var found_match = false
while(!rsproduc ts.EOF){
found_match = found_match ||
(String(rsprodu cts.Fields.Item ("size").Val ue) ==
size && String(rsproduc ts.Fields.Item( "colour").Value ) == colour &&
String(rsproduc ts.Fields.Item( "ProductName"). Value))
}

Now you would just do an if check against the found_math boolean, if there
was a match it will be true and you should update, otherwise there wasn't a
match and you should do an insert.
Now the problems I have with the above method are the following:
1) I don't know if you allow multiple items to be submitted at the same
time, but if so then the ineffiency of looping throught the recordset will be
compounded bvy additional looping through multiple products
2) Looping through a recordset in your code is inefficient
My best suggestion would be to use a stored procedure, if you have the
ability. The stored procedure would accept the arguments for the product
(table name, color, id's, etc), select from the table and then either update
if records were returned or insert if they weren't.

My second best suggesiton would be to implement that samemethod in your
code. Instead of pulling back a recordset and looping through it you could
just as easily execute a SELECT statement:
"SELECT ProductID WHERE ProductID = " + ProductID
If the returned recordset is EOF then insert a new record, otherwise do an
UPDATE
If you have the possibility of adding multiple items to the cart, you could
create a comma-delimited list of the ID's, receive back a recordset
containing the ID's that exist in the table already, then loop through the
new items, either inserting or updating them based on whether their ID's aree
in the returned recordset. Even better than checking against the recordset
would be to loop through it once and create a delimited string so you could
then just check the incoming ID's against the string (using InStr - can't
remember the javascript equivalent) and use that check to decide whether to
insert or update.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
16141
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? 1) simply UPDATING the values for all fields in the table, whether or not any particular field has actually changed 2) running a second SELECT statement and comparing the $_POST vars to the returned values, and only UPDATING those that have...
2
4903
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the GUI is updating.  I've done that before without a problem, however, now, I need to pass variables to the separate Thread or Runnable that I'm using.  I'm using something like this: //Other code setting things up and updating GUI //Variables...
45
2656
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
6
5762
by: Hennie de Nooijer | last post by:
Hi, Currently we're a building a metadatadriven datawarehouse in SQL Server 2000. We're investigating the possibility of the updating tables with enormeous number of updates and insert and the use of checkpoints (for simple recovery and Backup Log for full recovery). On several website people speak about full transaction log and the pace of growing can't keep up with the update. Therefore we want to create a script which flushes the...
0
1298
by: cwbp17 | last post by:
Have two tables that have a FK relationship on ID column. Have one datagrid that displays all of the columns of both tables. What's the best approach on updating a row from the datagrid back to the database? I've used the following code for the DataGird1_UpdateCommand Option1
10
5654
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1) DsStudentCourse1.AcceptChanges() i'm also wondering because w/ out AcceptChanges the data is still save into the database and it is now faster.
14
2938
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will utilise the disco file to update the Corresponding proxy file and reflect the changes made to the web service. However, the results of doing this with out params is that the results seem
6
4067
by: muttu2244 | last post by:
hi all am updating the same file in ftp, through multiple clients, but am scared that two clients may open the same file at a time, and try updating, then the data updated by one data will be lost. So i have to provide some lock mechanism to that file in ftp, so how can i lock it, if one client opens it for updating, so that till it gets relased it cannot be updated by other clients. Could u please suggest me the simple solutions...
0
1117
by: =?Utf-8?B?YmFrZXJzaGFjaw==?= | last post by:
Unless my app is EXTREMELY simple, I get the cross-threading error message regularly when updating controls on a Windows form. My latest example involves a dll that runs a System.Threading.Timer, updating private shared variables. When it finishes updating, it raises a NewData event. My app responds to that event by updating display controls (labels in this case). I have to use a delegate and update them like this: Private Sub...
5
5343
by: rosaryshop | last post by:
I'm working a jewelry/rosary design web site at http://www.rosaryshop.com/rosariesAndKits2.php. As the user makes selections, it updates images of various parts, giving them a preview of the finished item. The preview resides within a div and is simply a series of <br>-separated images. Simple html. The system is working fine with FireFox and Safari, but some MSIE 6 and 7 users are reporting that only the first image is updating and the...
0
8115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8053
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8513
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8380
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5504
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1638
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.