我只是在学习php而且我的项目有问题。我在mysql(localhost)中有一个数据库,其中有3个表:类别,产品和餐馆,类别和餐馆都链接到产品。当我尝试显示指定类别的产品时,它不会显示任何内容。与数据库的连接很好,查询很好,所以我不知道出了什么问题。请帮助。
这是index.php
<?php
include_once './database.php';
include_once './header.php';
<div class="content" align="center">
<div id="galery" class="content_block" style="height:400px;">
<div class="galery">
<img src="./img/gallery/6.jpg">
</div>
</div>
<div class="content_block" style="padding-bottom:10px;">
$query = "SELECT * FROM categories";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
echo '<a href="categories.php?cat='.$row['category'].'"><div class="contentButton inline"><div class="catFood_img foodCategory_1"></div><div>'.$row['category'].'</div></div></a>';
</div>
<div class="content_block" style="height:190px" align="center">
$query = "SELECT * FROM restourants";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
echo '<a href="categories.php?res='.$row['restourant'].'"><div class="restournats inline"><img src="./food/restourants/chens.jpg"></div></a>';
}
</div>
</div>
include_once './footer.php';
?>
这是categories.php的代码
include_once './database.php';
include_once './header.php';
<div class="content" align="center">
<div class="content_block2">
$category= (int)(isset($_GET['id_category']) ? $_GET['id_category'] : '');
$query = "SELECT * FROM products WHERE products.id_category=$category";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){
echo '
<a href="dishes.php?id='.$row['id'].'"><div class="item inline">
<div class="food_img"><img src="'.$row['img'].'" /></div>
<div class="title">'.$row['name'].'</div>
<div class="price inline">Price: '.$row['price'].' €</div><div class="btn">Choose</div>
</div>
</a>';
}
</div>
</div>
include_once './footer.php';
答案 0 :(得分:0)
在 index.php 中,您将创建链接
echo '<a href="categories.php?cat='.$row['category'].'">
(上面给出的双重检查$row['category']
是您需要的所需类别ID值)
但是在您的 categories.php 中,您正尝试将该类别设为
$category= (int)(isset($_GET['id_category']) ? $_GET['id_category'] : '');
尝试使用$_GET['cat']
代替$_GET['id_category']
还可以尝试echo $category
和echo $query
查看您是否获得了所需的值。