Thread: Resolved My SQL Database PHP Inquiry
View Single Post
Old 12-06-2012, 03:08 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You mean you had me writing PHP code for 30 minutes when I don't even *USE* PHP???

AAARRRGGGGHHHH!!

Well...FWIW, and remembering I don't use PHP, here's what I came up with:
Code:
<html>
<body>
<?php

... make your DB connection here ...

$orderid = (int)$_GET["order"]; // ensure that orderid *IS* an integer!  no SQL injection

$sql = "SELECT customers.CompanyName, 
               orders.CustomerID, orders.OrderID, orders.Freight
               order_details.Quantity,
               products.ProductID, products.ProductName, products.UnitPrice,  
               shippers.CompanyName AS shipCompany
	FROM customers, orders, order_details, products, shippers
	WHERE customers.CustomerID = orders.CustomerID
	AND orders.OrderID = " . $orderid . "
	AND orders.OrderID = order_details.OrderID
	AND order_details.ProductID = products.productID
	AND orders.ShipVia = shippers.ShipperID
	ORDER BY products.productName";

$result = mysql_query($sql) or die(mysql_error());

// *MUST* have at least one row:
$row = mysql_fetch_assoc($result) or die(mysql_error());

// get things started:
$shipcost = $row["Freight"];
$shipvia = $row["shipCompany"];
$total = $shipcost;

// first output order header:
?>

<table border="1">
<tr>
    <th>Company</th>
    <th>Order#</th>
    <th colspan="5">&nbsp;</th>
</tr>
<tr>
    <td><? echo $row["CompanyName"]; ?></td>
    <td><? echo $orderid; ?></td>
    <td colspan="7">&nbsp;</td>
</tr>
<tr>
    <th colspan="2">&nbsp;</th>
    <th>Product</th>
    <th>Prod #</th>
    <th>Quantity</th>
    <th>Price</th>
    <th>Cost</th>
</tr>

<?php
// now output one row per record for the order details:
while ( true )
{
    $price = $row["UnitPrice"};
    $qty = $row["quantity"]
    $cost = $price * $qty;
    $total += $cost;
?>
<tr>
    <td colspan="2">&nbsp;</td>
    <td><? echo $row["ProductName"]; ?></td>
    <td><? echo $row["ProductID"]; ?></td>
    <td><? echo $qty; ?></td>
    <td><? echo $price; ?></td>
    <td><? echo $cost; ?></td>
</tr>
<?php
    // try to get next row
    $row = mysql_fetch_assoc($result);
    if ( $row == FALSE ) { break; /* out of while loop */ }
} /* end of while loop

// ouput totals, etc.
?>
<tr>
    <th colspan="5">&nbsp;</th>
    <th>Ship Via</th>
    <th>Ship Cost</th>
</tr>
<tr>
    <td colspan="5">&nbsp;</td>
    <td><? echo $shipvia; ?></td>
    <td><? echo $shipcost; ?></td>
</tr>
<tr>
    <td colspan="7"><br/></td>
</tr>
<tr>
    <td colspan="6" style="text-align: right; font-weight: bold;">TOTAL</td>
    <td><?echo $total; ?></td>
</tr>
</table>
</body>
</html>
How close is it to what you had?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote