【问题标题】:use inner join to filter products based on category使用内连接根据类别过滤产品
【发布时间】:2018-10-04 15:09:50
【问题描述】:

SQL 查询从数据库中正确选择了所需的产品,但是我无法让 php 将数据输出到网站。我也需要数据在下面的 HTML 中。 if statement 块内部某处有问题,但我根本找不到它

SQL:

CREATE TABLE Product(
    Product_ID int NOT NULL AUTO_INCREMENT,
    Name varchar(255) NOT NULL,
    Stock_Level int NOT NULL,
    Price int NOT NULL,
    Image blob NOT NULL,
    Admin_ID int NOT NULL,
    PRIMARY KEY (Product_ID),
    FOREIGN KEY (Admin_ID) REFERENCES Admins(Admin_ID)
);

CREATE TABLE Category(
    Category_ID int NOT NULL AUTO_INCREMENT,
    Category varchar(15) NOT NULL,
    PRIMARY KEY (Category_ID)
);

CREATE TABLE ProductLookup(
    ProductLookup_ID int NOT NULL AUTO_INCREMENT,
    Product_ID int(100) NOT NULL,
    Category_ID int NOT NULL,
    PRIMARY KEY (ProductLookup_ID),
    FOREIGN KEY (Product_ID) REFERENCES Product(Product_ID),
    FOREIGN KEY (Category_ID) REFERENCES Category(Category_ID)
);

PHP:

   <?php
  //Connect to DB
  include_once 'DBConnection.php';

 //Query DB to find Meat Products based on category

  $sql = "SELECT p.Name, p.Price, p.Image FROM Product p INNER JOIN (ProductLookup pl) ON (p.Product_ID = pl.Product_ID) WHERE pl.Category_ID = 1;";

 $result = mysqli_query($conn, $sql);
 $resultCheck = mysqli_num_rows($result);
 if($resultCheck > 0){
   while($row = mysqli_fetch_assoc($result)){
      echo $row['Name'];
      echo $row['Price'];
      echo $row['Image'];
   }
} else{
  echo "Empty";
}
?>

HTML

<div class="panel-body">
                <div class="row">
            <div class="col-md-4 product">
                <div class="product-price">£000</div>
                <a href="product.html">
                    <img src="#"/>
                </a>
                <div class="product-title">
                    Placeholder
                </div>
                <div class="product-add">
                    <?php
                    if(isset($_SESSION['id'])){
                     echo '<button class="btn btn-primary" type="submit">Add To Basket</button>';
                    }else{
                        echo 'Please <a href="login.php">Log In to Purchase</a>';
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>

【问题讨论】:

  • 看起来您的查询中已经有了过滤器。尝试重新表述您将要实现的目标。
  • 只需在 URL 中传递 ID 并使用该值而不是您当前的硬编码值。
  • 我在Product 表中有Category_ID 的数据库。但是,出于系统中的其他目的,我需要每个产品有多个类别,因此 ProductLookup

标签: php sql inner-join data-retrieval


【解决方案1】:

你有两个问题:

第一个是您的查询中的错误。您正在尝试从 Products 表中获取数据,但表的名称是 Product

试试这个查询:

SELECT p.Name, p.Price, p.Image
FROM Product p
INNER JOIN (ProductLookup pl) ON (p.Product_ID = pl.Product_ID)
WHERE pl.Category_ID = 1

第二个是行错误:

while($row = msqli_fetch_assoc($result)){

函数 msqli_fetch_assoc 用于 Microsoft 数据库而不是 MySQL。你可以使用这个:

while ($row = mysqli_fetch_assoc($result)) {

我做了以下代码,效果很好:

<?php

//Connect to DB
$conn = mysqli_connect('localhost', 'user', 'password', 'database');

//Query DB to find products
$sql = "SELECT Products.Name, Products.Price, Products.Image 
FROM Products 
INNER JOIN ProductLookup ON Products.Product_ID = ProductLookup.Product_ID 
WHERE ProductLookup.Category_ID = 1;";

$sql = "
SELECT p.Name, p.Price, p.Image
FROM Product p
INNER JOIN (ProductLookup pl) ON (p.Product_ID = pl.Product_ID)
WHERE pl.Category_ID = 1
";

$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);

?>


<div class="panel-body">
    <div class="row">

<?php

if ($resultCheck > 0) {

    while ($row = mysqli_fetch_assoc($result)) { ?>

        <div class="col-md-4 product">
            <div class="product-price">
                £<?= $row['Price'] ?>
            </div>
            <a href="product.html">
                <img src="<?= $row['Image'] ?>"/>
            </a>
            <div class="product-title">
                <?= $row['Name'] ?>
            </div>
            <div class="product-add">
                <?= isset($_SESSION['id']) ? '<button class="btn btn-primary" type="submit">Add To Basket</button>': 'Please <a href="login.php">Log In to Purchase</a>' ?>
            </div>
        </div>

    <?php }

} else {

    echo "<h2>There is no products avaliable</h2>";

}

?>

    </div>
</div>

【讨论】:

  • 试一试,它仍然是空的。我的数据库中有该类别的数据只是为了测试
  • 我在我的数据库中运行了查询,它可以正常工作,所以谢谢你,但是 php 脚本返回空,所以 if 语句块中某处存在问题
  • 已编辑,查看
  • &lt;?php //Connect to DB include_once 'DBConnection.php'; //Query DB to find Meat Products based on category $sql = "SELECT p.Name, p.Price, p.Image FROM Product p INNER JOIN (ProductLookup pl) ON (p.Product_ID = pl.Product_ID) WHERE pl.Category_ID = 1;"; $result = mysqli_query($conn, $sql); $resultCheck = mysqli_num_rows($result); if($resultCheck &gt; 0){ while($row = mysqli_fetch_assoc($result)){ echo $row['Name']; echo $row['Price']; echo $row['Image']; } } else{ echo "Empty"; } ?&gt;
  • 这是我的代码,它们没有正确回显到网站
猜你喜欢
  • 2022-11-17
  • 1970-01-01
  • 2021-11-21
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多