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>
答案 0 :(得分:0)
你有两个问题:
第一个是查询中的错误。您正在尝试从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>