I believe my code isn't fully working because I have while loops inside a if statement, and is only allowing the loop to run once. The code is allowing an admin to view an order and access all of the information about that order. However with the code like it is, it only views one product, even if the order contains 2 or more. Below is my php code block
if (isset($_GET['orderid'])){
$targetID = $_GET['orderid'];
//query to find the item
$products_ordered = "";
$sql = mysql_query("SELECT * FROM `transactions` WHERE `order_id` ='$targetID' LIMIT 1");
$orderCount = mysql_num_rows($sql);
while ($transactions = mysql_fetch_array($sql)) {
//creating variables from the information
$order_id = $transactions["order_id"];
$mem_id = $transactions["mem_id"];
$OrderDate = $transactions["OrderDate"];
$ship_phone = $transactions["ship_phone"];
$ship_address = $transactions["ship_address"];
$ship_city = $transactions["ship_city"];
$ship_county = $transactions["ship_county"];
$ship_postcode = $transactions["ship_postcode"];
$ship_country = $transactions["ship_country"];
$order_details = mysql_query("SELECT * FROM `transactionDetails` WHERE `order_id` = $order_id") or die(mysql_error());
$orderDetailsCount = mysql_num_rows($order_details);
while ($row = mysql_fetch_array($order_details)) {
//creating variables from the information
$order_details_id = $row["Order_details_ID"];
$order_product_id = $row["Product_ID"];
$order_product_price = $row["Price"];
$order_product_quantity = $row["Quantity"];
$member_details = mysql_query("SELECT * FROM `members` WHERE `mem_id` = $mem_id") or die(mysql_error());
$memberDetailsCount = mysql_num_rows($member_details);
while ($row = mysql_fetch_array($member_details)) {
//creating variables from the information
$order_mem_fname = $row["mem_first_name"];
$order_mem_lname = $row["mem_last_name"];
$product_details = mysql_query("SELECT * FROM `products` WHERE `id` = $order_product_id") or die(mysql_error());
while ($row1 = mysql_fetch_array($product_details)) {
//creating variables from the information
$product_name = $row1["product_name"];
$products_ordered = "<tr>
<td width=\"20%\">Product Name</td>
<td width=\"80%\"><label> $product_name
</label></td>
</tr>
<tr>
<td width=\"20%\">Quantity</td>
<td width=\"80%\"><label> $order_product_quantity
</label></td>
</tr>
<tr>
<td width=\"20%\">Price per Item</td>
<td width=\"80%\"><label> $order_product_price
</label></td>
</tr>";
}
}
}
}
if ($orderCount == 0) {
echo "Sorry, order doesn't exist";
exit();
}
}
And this is my table which contains the data
<table>
<tr>
<td width="20%">Order ID:</td>
<td width="80%"><label><?php echo $order_id; ?> </label></td>
</tr>
<tr>
<td width="20%">Order Date</td>
<td width="80%"><label><?php echo $OrderDate; ?> </label></td>
</tr>
<tr>
<td width="20%">First Name</td>
<td width="80%"><label><?php echo $order_mem_fname; ?> </label></td>
</tr>
<tr>
<td width="20%">Last Name</td>
<td width="80%"><label><?php echo $order_mem_lname; ?> </label></td>
</tr>
<tr>
<td width="20%">Contact Number</td>
<td width="80%"><label><?php echo $ship_phone; ?> </label></td>
</tr>
<tr>
<td width="20%">Address</td>
<td width="80%"><label><?php echo $ship_address; ?> </label></td>
</tr>
<tr>
<td width="20%">City</td>
<td width="80%"><label><?php echo $ship_city; ?> </label></td>
</tr>
<tr>
<td width="20%">County</td>
<td width="80%"><label><?php echo $ship_county; ?> </label></td>
</tr>
<tr>
<td width="20%">Post Code</td>
<td width="80%"><label><?php echo $ship_postcode; ?> </label></td>
</tr>
<tr>
<td width="20%">Country</td>
<td width="80%"><label><?php echo $ship_country; ?> </label></td>
</tr>
<?php echo $products_ordered;?>
</table>
You save the output in a variable and the next time it goes through the code it will override the original value.
Instead of
$products_ordered = "<tr>
You should do that
$products_ordered .= "<tr> // notice the dot to concatenate the original html with the new html
If your first query returns 2 results and you expect 2 products that means each while loop within the main loop must loop at least once.
Print each count values and make sure the first one shows
2
and the other ones show 1
. If it doesn't always show 1
it means your database is missing data.
You can also avoid this issue by using SQL JOIN. That way, you can directly test the final output with a SQL client or phpmyadmin.
Also, your code is open to SQL injection because you are using
mysql_*
functions and the query parameters are not sanitized. Instead you should use MySQLi or PDO. As a temporary solution, you could typecast $_GET['orderid']
to an integer or check if $_GET['orderid']
is an integer.
0 comments:
Post a Comment