Hey all,
My situation is this..

A user has already selected the items into the shopping cart.
Now it is on to the transaction table. So all the items are added to the transaction table for the user to purchase the items.
When it goes to the transaction, the shopping cart has to be cleared so that when another user enters the website, the shopping cart will be empty for that user.
1. How can the items in the shopping cart be inserted into the transaction table?
2. How can i clear the shopping cart for the next user?


How can i achieve this using dreamweaver in PHP?
Hopefully someone can provide a tutorial for this because im a beginner in PHP.
Thanks!

Dani AI

Generated

— the cart should be moved to an order record only for that shopper, not emptied site-wide. was right to flag a shared cart; correctly asked how the cart is stored. A reliable pattern is: create an order row, copy only that shopper's cart rows into order_items, then remove only those cart rows inside a single database transaction so the move is atomic.

Typical schema (examples): carts(id, cart_key, product_id, qty, price); orders(id, user_id, total, created_at); order_items(id, order_id, product_id, qty, price). Checkout steps: start DB transaction, insert an orders row, copy cart rows into order_items with an INSERT ... SELECT ... FROM carts WHERE cart_key = ?, delete those carts rows, then commit. Use prepared statements, check inventory/price consistency before commit, and rollback on any error to avoid partial state.

A compact PDO example showing the pattern:

<?php
$db = new PDO($dsn,$user,$pass,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
$db->beginTransaction();

$stmt = $db->prepare("INSERT INTO orders (user_id,total,created_at) VALUES (:uid,:total,NOW())");
$stmt->execute([':uid'=>$userId,':total'=>$total]);
$orderId = $db->lastInsertId();

$ins = $db->prepare("
  INSERT INTO order_items (order_id,product_id,qty,price)
  SELECT :oid,product_id,qty,price FROM carts WHERE cart_key = :key
");
$ins->execute([':oid'=>$orderId,':key'=>$cartKey]);

$del = $db->prepare("DELETE FROM carts WHERE cart_key = :key");
$del->execute([':key'=>$cartKey]);

$db->commit();
?>

Notes: store the cart key securely (cookie or tied to logged-in user), validate prices and stock just before commit (or lock rows), and handle multi-tab/parallel checkout by using locks or checking affected row counts. In Dreamweaver you place this PHP on the checkout handler page and test on a PHP-enabled server; prefer PDO or mysqli over deprecated interfaces.

Recommended Answers

All 2 Replies

When it goes to the transaction, the shopping cart has to be cleared so that when another user enters the website, the shopping cart will be empty for that user.

This does not look like a good idea, if I am understanding you then it would mean that everyone on the site would 'share' the cart...

Have a look at sessions, in your tables, add a column for 'session_id' or similar and use the session ID for this, this would mean that multiple users can have items in the cart at the same time and also would mean that once they click the checkout or pay button that you can remove only their items from the cart.

How is the cart stored? Browser cookies? A session on the server?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.