niekerd
niekerd

Reputation: 13

Store multiple values in one id

I'm making a shopping cart script for a project. The cart is already created but now I'm making a query where the products from the cart are inserted into the order table. But when I do this, every product is getting it's own order_id, is it possible to give every product from the cart the same order_id?

The query where the products are inserted into the orders table:

function cart() {
global $query;
global $query2;
$total = 0;
$total2 = 0;
foreach($_SESSION as $name => $value) {
    if ($value>0)   {
        if (substr($name, 0, 5)=='cart_')   {
            $id = substr($name, 5, (strlen($name)-5));
            $get = mysql_query('SELECT id, partnr, specs, price FROM parts WHERE id='.mysql_real_escape_string($id));
            while ($get_row = mysql_fetch_assoc($get))  {
                $sub = $get_row['price']*$value;
                echo $get_row['partnr'].' x '.$value.' @ &euro;'.number_format($get_row['price'], 2). ' = &euro;'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?toevoegen2='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a> <br/>';
                if(isset($_POST['behandelen'])){
                    $partnr=$get_row['partnr'];
                    $price=$get_row['price'];
                    $datum=date("Y-m-d H:i:s");
                    $gebruikersnaam = $_REQUEST['gebruikersnaam'];
                    $query = "UPDATE parts set hoeveelheid = hoeveelheid - '$value' WHERE id = '$id'";
                    $query2= "INSERT INTO orders(partnr, price, hoeveelheid, betaaldatum)VALUES('$partnr', '$price', '$value', '$datum')";
                }
                mysql_query($query);
                mysql_query($query2);

            }

        }
        $total += $sub; //totaalprijs exclusief btw
        $total2 += $sub*1.21; //berekening inclusief btw van 21%
        $btw = $total2-$total;
    }
}
if ($total==0)  {
    echo "Uw winkelwagen is leeg.";
}
else {
    global $lang;
    echo $lang['CART_SUBTOTAL'], '&euro;'.number_format($total, 2); echo "<br/>";
    echo $lang['CART_BTW'], '&euro;'.number_format($btw, 2); echo "<br/>";
    echo $lang['CART_TOTAL'], ' &euro;'.number_format($total2, 2); //prijs inclusief btw wordt weergegeven op de site
}


}

The sql query looks like this: query2= "INSERT INTO orders(partnr, price, hoeveelheid, betaaldatum)VALUES('$partnr', '$price', '$value', '$datum')";

I hope someone can give me any advice,

Thanks in advance!

Upvotes: 0

Views: 928

Answers (4)

Lotto Kuma
Lotto Kuma

Reputation: 38

I suggest you create a bridge name "Order_Items" between your Order and Product table with attributes such as Order_ID, Product_Id, Quantity, Price , etc.

Upvotes: 0

David D
David D

Reputation: 1295

You should only insert one new row into the orders table. This will you give the order_id.

Create another table called "orders_products". In this table you insert each product, for example:

orders_products_id order_id  product_id  quantity
1                  5         8           1
2                  5         7           2
3                  5         12          1

As you can see the order_id is 5, but there are different products on each row.

Upvotes: 0

Dedipyaman Dash
Dedipyaman Dash

Reputation: 11

Why would you add the same product id to number of products?

What you can do is- add an unique universal product ID for every unique product, for example- a toothpaste of a brand x with specifications y and price z should have the same universal product ID. Then you can let users add them to a cart.

The cart will have another unique ID, and all the products stored in the database will have the same cart ID. So all your products ordered or stored in the cart will have the same cart ID but a different product ID so that you can fetch all the information about the product.

You can just do this-

(partnr, price, hoeveelheid, betaaldatum, productID, cartID)VALUES   
('$partnr', '$price', '$value', '$datum', someID, anotherID)";

There should also be another table containing all the cart information and another for product information

Upvotes: 0

Richard   Housham
Richard Housham

Reputation: 1680

Your database is set up all wrong, so you have an order and an order has items. I would create a new table to contain details about the order and the current table really should be renamed order_items.

In the order table, you can add details about the customer/delivery details? There will then be a foriegn key between orders and the order_items.

ie.

Orders (table)
order_id - primary key - auto increment.
other_fields....................

Order_items (table 2)
order_item_id - primary key - auto inc
order_id - foreign key
details about the order item - e.g quantity

That should point you in the right direction.

Upvotes: 2

Related Questions