使用 inner join 根据类别过滤产品
use inner join to filter products based on category
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>
您有 2 个问题:
首先是您的查询有误。您正在尝试从 Products table 获取数据,但 table 的名称是 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>
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>
您有 2 个问题:
首先是您的查询有误。您正在尝试从 Products table 获取数据,但 table 的名称是 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>