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

いくつかの変更を加えたデータとクエリで動作します (order_detail テーブルに product_cost フィールドはありません):
http://www.sqlfiddle.com/#!2/38c9b/7

于 2013-02-10T07:49:03.653 に答える