So what I have written right now is code for a user to place an order. They would add a book to the cart and press 'checkout' then from there it lists what they would have to pay and then they would press 'place order' which then needs to send the variables to my database. In my database I have two different tables, the first one is Orders where it has an OrderID which is incremented with each order placed. In Orders I have to input CustomerID,ShippingCost,Tax, and Total. Then FOR EACH ITEM, I must input into OrderDetails the OrderID,ProductID,Quantity, and LineTotal.
However, I cannot figure out how to input the OrderID with each item ordered corresponding to the OrderID generated in the Orders Table. As an example, I order two books, book a and book b. It will add a new Order and have CustomerID set to the ID of the user logged in, the shipping cost, the tax, and the total. It will then set the OrderID to one number higher than the last place, so lets say 19017. I must then get that orderID and input into OrderDetails the order id(19017), the ProductID (b1), the quantity(1) and the LineTotal(however much the book costs). I then must do the same for book b using the same OrderID. I cannot figure out how to pull the order id from when I insert it into Orders and use that when inputting OrderDetails. Thanks!
if($_SESSION['cart'])
{
echo"<pre># Title Price Quantity Total</pre>";
foreach($_SESSION['cart'] as $product_id => $quantity)
{
$query="Select ProductID , Title , Price From Products where ProductID='$product_id'";
$result = mysql_query($query);
list($ProductID, $Title, $Price) = mysql_fetch_row($result);
$cost = $Price * $quantity;
$total = $total + $cost;
echo "<br>";
echo "<br>";
echo "<br>";
echo "$ProductID      ";
echo "$Title      ";
echo "$ $Price          ";
echo "$quantity    ";
echo "            $ $cost      ";
echo "<br>";
echo"___________________________________________________________________________________________________________________________";
}
If($total<25)
$ship=4.50;
else if($total <50)
$ship=7;
else if($total>=50)
$ship=10.25;
else
$ship=0;
$tax=$total*.06;
$totalPrice=$total+$ship+$tax;
$query2="Select CustomerID from Customers where Email='$username'";
$result2 = mysql_query($query2);
list($CustomerID) = mysql_fetch_row($result2);
$query3="Insert into Orders(CustomerID, ShippingCost,Tax,Total) values($CustomerID,$ship,$tax,$totalPrice)";
mysql_query($query3);
$query4="Insert into OrderDetails(ProductID,Quantity,LineTotal) values('$ProductID',$quantity,$total)";
mysql_query($query4);
}