-1

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 &nbsp &nbsp &nbsp";
                    echo "$Title &nbsp &nbsp &nbsp";
                    echo "$ $Price &nbsp &nbsp &nbsp &nbsp &nbsp";
                    echo "$quantity &nbsp &nbsp";
                    echo "&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp $ $cost  &nbsp &nbsp &nbsp";
                    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);

    }
Luceos
  • 6,629
  • 1
  • 35
  • 65
jskozerino
  • 45
  • 2
  • 6
  • first of, any new code based on mysql_* functions is considered bad practice, either use PDO or mysqli_* functions. Second, the answer is running a `select last_insert_id()`: https://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html – Luceos May 02 '15 at 17:48

1 Answers1

1

As you're using mysql_ functions, you can use mysql_insert_id e.g.

$query3="Insert into Orders(CustomerID, ShippingCost,Tax,Total) values($CustomerID,$ship,$tax,$totalPrice)";
mysql_query($query3);

$orderId = mysql_insert_id();

But I would strongly recommend using PDO or mysqli instead. See here for more information on that.

Community
  • 1
  • 1
rjdown
  • 9,162
  • 3
  • 32
  • 45
  • I used that. However, if I use this where I have mine it will only insert the last product entered. I have to somehow get the orderID inside the loop so it will read in all products with the same OrderID – jskozerino May 02 '15 at 17:58
  • I don't understand. All you need to do is use `$orderId` inside the loop. – rjdown May 02 '15 at 18:03
  • The loop runs then query3 is ran, so if I do use it in the loop it doesnt know what $orderId is – jskozerino May 02 '15 at 18:08
  • There is only one loop in your code, and it doesn't have any insert queries inside it. Your loop ends right before `if ($total < 25)` maybe you need to make another loop for inserting? – rjdown May 02 '15 at 18:14