Hi,

I have been building a shopping cart for a website that I am building. What I am trying to do is created a purchase history so when an item is purchased it is stored in the customers purchase history (in the customers MySQL table).

I have three tables
Cart and
Customers and
Books

Here is the problem code:

$result = "select * from `cart` inner join books on cart.itemId = books.bookid where cart.cookieId = '" . GetCardIDCheckout() . "'"; // Get username
  $result .= " UNION (SELECT * FROM customers WHERE username='" . $_SESSION['username'] . "')";
   $result = mysql_query($result) or die (mysql_error());

while($row = mysql_fetch_array($result))
  {
    //echo "BookTitle:  ".$row["title"]; 
    
    $insert = "INSERT INTO customers(purchasehistory) VALUES('" . $row["title"] . "')";
    $result= mysql_query($insert) or die (mysql_error());

  }

What I am attempting to do is grab the books in the customers cart and then insert them into "purchasehistory" in the customers table. The problem I am having is that I can't combine the two. I can get the books in the cart but I can't insert them into purchase history. I attempted to join the two queries together but it isn't working properly.

Any help would be very much appreciated

Thanks!

Hi,

I have been building a shopping cart for a website that I am building. What I am trying to do is created a purchase history so when an item is purchased it is stored in the customers purchase history (in the customers MySQL table).

I have three tables
Cart and
Customers and
Books

Here is the problem code:

$result = "select * from `cart` inner join books on cart.itemId = books.bookid where cart.cookieId = '" . GetCardIDCheckout() . "'"; // Get username
  $result .= " UNION (SELECT * FROM customers WHERE username='" . $_SESSION['username'] . "')";
   $result = mysql_query($result) or die (mysql_error());

while($row = mysql_fetch_array($result))
  {
    //echo "BookTitle:  ".$row["title"]; 
    
    $insert = "INSERT INTO customers(purchasehistory) VALUES('" . $row["title"] . "')";
    $result= mysql_query($insert) or die (mysql_error());

  }

What I am attempting to do is grab the books in the customers cart and then insert them into "purchasehistory" in the customers table. The problem I am having is that I can't combine the two. I can get the books in the cart but I can't insert them into purchase history. I attempted to join the two queries together but it isn't working properly.

Any help would be very much appreciated

Thanks!

try this: http://www.java2s.com/Code/SQL/Join/JOINthreetableswithaliasname.htm

This article has been dead for over six months. Start a new discussion instead.