2013-03-15 62 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。由于每个订单只有一个成员,因此没有更多要为第二个订单行进行检索。