2013-03-15 24 views
0

我相信我的代碼沒有完全工作,因爲我在while語句中有while循環,並且只允許循環運行一次。該代碼允許管理員查看訂單並訪問關於該訂單的所有信息。然而,就像這樣的代碼,它只能查看一個產品,即使該訂單包含2個或更多。下面是我的PHP代碼塊數組並沒有獲取所有數據,因爲if語句

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(); 
} 
} 

這是我的表,包含數據

<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> 
+2

從你的SQL中刪除'LIMIT 1'。它只返回第一條記錄 – Yellowledbet 2013-03-15 21:05:38

+0

外部while循環不需要在那裏,或者你應該改變'$ sql = mysql_query(「SELECT * FROM transactions WHERE order_id ='$ targetID'LIMIT 1」);'因爲您將結果集限制爲一次返回。 – Jon 2013-03-15 21:05:57

+0

我刪除了限制1,但它沒有區別。所有這一切只允許一個order_id,這是正確的1 order_id可以有很多product_id的 – jhetheringt7 2013-03-15 21:09:11

回答

1

將輸出保存在一個變量中,下一次通過代碼時它將覆蓋原始值。

而不是

$products_ordered = "<tr> 

你應該做的

$products_ordered .= "<tr> // notice the dot to concatenate the original html with the new html 

如果你的第一個查詢返回2個結果與你預期2級的產品,這意味着主迴路必須循環內的每個while循環至少一次。

打印每個計數值,並確保第一個顯示2,其他顯示1。如果它不總是顯示1這意味着你的數據庫缺少數據。

您還可以通過使用SQL JOIN來避免此問題。這樣,您可以直接使用SQL客戶端或phpmyadmin測試最終輸出。

另外,由於您使用的是mysql_*函數且查詢參數不是sanitized,因此您的代碼可以打開到SQL injection。相反,你應該使用MySQLiPDO。作爲臨時解決方案,您可以將$_GET['orderid']轉換爲整數,或者檢查$_GET['orderid']是否爲整數。

2

這是一個複雜的方式,讓您的數據,這可能與某些連接少做(也許一個)查詢。

你的問題是你在兩個級別使用$行:$ order_details和$ member_details。由於每個訂單隻有一個成員,因此沒有更多要爲第二個訂單行進行檢索。