2

我一生都无法弄清楚为什么我不能从表 B 中获取图像。它从表 A 中的所有内容中都没有从表 B 中返回任何内容。

我想知道它是否是表结构。该表的设置方式是 order_detail 表中可能有多个 order_number 实例。两个 Product_id 字段都是 INT(10) 并且之前 order_detail 表没有主键(自动增量)。因为订单号是从另一个表中获取的,并且对于每个购买的产品,每个订单可能有 10 个相同的订单号,所以主键是一个单独的字段。

我应该将 order_number 创建为索引字段吗?任何帮助都会很棒,因为我快要放弃了。

Here is order details:
id      order_number  date_time         product_id  product_name    quantity
2       10011     2012-12-20 14:11:24   13          T-Shirt         1
3       10011     2012-12-20 20:02:31   11          T-Shirt         1

Here is products:
product_id  who product_name    color   size    price   image
13          men T-shirt     red medium  15.00   /images/apparel/t-shirt.jpg
11          men T-Shirt         red small   15.00   /images/apparel/t-shirt.jpg

This is the end result of my query:

Order Number    Image   Product Name    Quantity    Cost Each   Total
10011               T-Shirt         2           $15.00          $30
10011               T-Shirt         2           $15.00      $30




$order_number = $_GET['var1'];
$query = "SELECT a.product_name, a.quantity, b.image, a.product_cost FROM order_detail a LEFT JOIN products b ON a.product_id = b.product_id WHERE a.order_number = '$order_number'";
$data = mysqli_query($dbc, $query); 

while($row = mysqli_fetch_assoc($data)){
$prod_name = $row['product_name'];
$quantity = $row['quantity'];
$cost = $row['product_cost'];
$img = $row['image'];
4

1 回答 1

3

It works with your data and query with some changes (there is no product_cost field in order_detail table):
http://www.sqlfiddle.com/#!2/38c9b/7

于 2013-02-10T07:49:03.653 回答