Professional Web Applications Themes

inserting multiple items into database - Coldfusion - Advanced Techniques

Hello, I am trying to insert multiple items from a shopping cart into a database. What I want to do is have fields for item, quantity, unitprice in my database, and if there are multiple items in the cart have all items dumped into my single item field in the database etc. I tried using this code below that is only putting in the last item in the cart and not all items. I am so stumped and it is probably something simple I am missing. Thanks in advance for any help. Here is the code: <cfset CurrentLoop=1> <cfloop from="1" ...

  1. #1

    Default inserting multiple items into database

    Hello,
    I am trying to insert multiple items from a shopping cart into a database.
    What I want to do is have fields for item, quantity, unitprice in my database,
    and if there are multiple items in the cart have all items dumped into my
    single item field in the database etc. I tried using this code below that is
    only putting in the last item in the cart and not all items. I am so stumped
    and it is probably something simple I am missing. Thanks in advance for any
    help. Here is the code:

    <cfset CurrentLoop=1>
    <cfloop from="1" to="#ArrayLen(Session.Cart.SKU)#" index="ThisItem">
    <cfset CurrentLoop = CurrentLoop + 1 >
    <cfset #form.Item# = "#replace(Session.Cart.Name[ThisItem], """", "")#,">
    <cfset #form.UnitPrice# = "#DollarFormat(Session.Cart.UnitPrice[ThisItem])#,">
    <cfset #form.Quantity# = "#Session.Cart.Quantity[ThisItem]#,">

    </cfloop>

    <cfquery name="AddOrder" datasource="mydb">
    INSERT INTO mytable
    (OrderTax,
    ShippingCharge,OrderTotal,Shipped,

    ShippingMethod,Item,Quantity,UnitPrice,CompanyName ,FirstName,LastName,Address
    1,Address2,City,StateOrProv,Zip,Country,ShpAddress 1,ShpCity,ShpStateOrProv,ShpZi
    p,ShpCountry,PhoneNumber,FaxNumber,EmailAddress,No tes,CCardType,CCNumber,ExprMon
    th,ExprYr)
    VALUES
    (
    '#FORM.Tax#','#FORM.Shipping#',
    '#FORM.OrderTotal#','#IsShipped#',

    '#ShipBy#','#form.Item#','#form.Quantity#','#form. UnitPrice#','#FORM.CompanyN
    ame#','#FORM.FirstName#','#FORM.LastName#','#FORM. Address1#','#FORM.Address2#','
    #FORM.City#','#FORM.StateOrProv#','#FORM.Zip#','#F ORM.Country#','#FORM.Address1#
    ','#FORM.City#','#FORM.StateOrProv#','#FORM.Zip#', '#FORM.Country#','#FORM.PhoneN
    umber#','#FORM.FaxNumber#','#FORM.EmailAddress#',' #FORM.Notes#','#FORM.CCardType
    #','#FORM.CCNumber#','#FORM.ExprMonth#','#FORM.Exp rYr#')
    </cfquery>

    brianism Guest

  2. #2

    Default Re: inserting multiple items into database

    So if I order Item A, quantity 3, at $10 per, and Item B, quanity 2, at $15
    per, and you want a single record for this, what will be your values for
    quantity and unit price?

    Originally posted by: brianism
    Hello,
    I am trying to insert multiple items from a shopping cart into a database.
    What I want to do is have fields for item, quantity, unitprice in my database,
    and if there are multiple items in the cart have all items dumped into my
    single item field in the database etc.



    Dan Guest

  3. #3

    Default Re: inserting multiple items into database

    If I order Item A, quantity 3, at $10 per, and Item B, quanity 2, at $15 per
    what I would like to happen is to insert item A and Item B in a single field
    "Item" in my database...then it would insert both quantities for items A and B
    in a single field "Quantity" in my database...same with unitprice. When
    someone will orders someting it gathers all or their info and inserts it into
    the database in a single row. My problem is how to get multiple items,
    quantities, and unit prices to be inserted into each field in the database.

    When I the insert statement gets to #form.Item# I need the value of
    #form.Item# to be "Item A, ItemB, ItemC" if that makes any sense. I am
    confusing myself a little bit here

    brianism Guest

  4. #4

    Default Re: inserting multiple items into database

    LIfe would be so much easier if you were willing to accept mulitple rows. Why
    aren't you?

    Originally posted by: brianism
    If I order Item A, quantity 3, at $10 per, and Item B, quanity 2, at $15 per
    what I would like to happen is to insert item A and Item B in a single field
    "Item" in my database...then it would insert both quantities for items A and B
    in a single field "Quantity" in my database...same with unitprice. When
    someone will orders someting it gathers all or their info and inserts it into
    the database in a single row. My problem is how to get multiple items,
    quantities, and unit prices to be inserted into each field in the database.

    When I the insert statement gets to #form.Item# I need the value of
    #form.Item# to be "Item A, ItemB, ItemC" if that makes any sense. I am
    confusing myself a little bit here



    Dan Guest

  5. #5

    Default Re: inserting multiple items into database

    Originally posted by: brianism
    If I order Item A, quantity 3, at $10 per, and Item B, quanity 2, at $15 per
    what I would like to happen is to insert item A and Item B in a single field
    "Item" in my database...then it would insert both quantities for items A and B
    in a single field "Quantity" in my database...same with unitprice. When
    someone will orders someting it gathers all or their info and inserts it into
    the database in a single row. My problem is how to get multiple items,
    quantities, and unit prices to be inserted into each field in the database.


    brianism,

    Save yourself (and your customers) some headaches and don't do that :-) One of
    the first rules you learn in an introductory database design courses is that
    storing comma delimited lists of values in a column is a bad design.

    1) It makes data very difficult to work with
    2) It encourages inconsistencies in your data, which is always bad, but
    especially in shopping application where you're dealing with customer's money.
    3) It yields poor performance and essentially defeats the purpose of having a
    powerful database, that is optimized for set based operations.





    mxstu Guest

  6. #6

    Default Re: inserting multiple items into database

    Brianism,

    I'm with MXSTU. Also, it is good database design practice to store only raw
    data, unless archiving.

    If you have all of your item information in table, let's call it tblItem
    (itemNumber, unitPrice, Description, etc.) depending how your shopping cart is,
    an tblOrder would have orderID, orderDate, customerID, shippingZone,
    taxPercentage, etc. and tblOrderItems would only need orderItemID, orderID
    (foreign key), itemNumber, quantity, unitSalePrice. In tblOrderItems, you could
    have the field orderItemID as the primary key, or you could have the
    combination orderID and itemID be the primary key.

    The idea is you have one customer with many orders, one order with perhaps
    many items.

    Stealth Guest

Similar Threads

  1. add multiple items to cart
    By siti_nana in forum Dreamweaver AppDev
    Replies: 0
    Last Post: February 26th, 12:13 AM
  2. Inserting individual items into a datagrid
    By W Akthar in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: August 11th, 10:19 AM
  3. multiple data items in each column
    By xzzy in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: April 18th, 08:59 PM
  4. changing attributes on multiple items at once
    By darrel in forum Macromedia Freehand
    Replies: 1
    Last Post: February 18th, 08:22 PM
  5. inserting existing items - how do i handle the error ???
    By Fredrik/Sweden in forum ASP Database
    Replies: 2
    Last Post: October 6th, 12:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139