我的数据库中有两个表。即food_dish和foodcat。食物菜是用于菜肴的,而食物猫是用于食物的类别的。

这是food_dish内部的示例:

dish_id | dish name | dish_cat 1 | Plain Rice | 1 2 | Pork Chop | 2 3 | Buttered Rice | 1

在foodcat中:

cat_id | cat_name 1 | Rice 2 | Pork

我只想在我的menurice.php表中显示纯米饭。我想知道是否可以将dish_cat链接到cat_id,所以当我调用cat_id 1时,它将仅显示莱斯的名称。

 <table class="table table-striped table-bordered table-hover" id="dataTables-example">
 <thead>
 <tr>
 <th>ID</th>
 <th>Dish Name</th>
 <th> Actions </th>
 </tr>
 </thead>
 <tbody>
 <?php
 include 'menuactions/foodconnect.php';
 $sql1= "SELECT * FROM `food_dish`";
 $data= mysqli_query($conn,$sql1) or die("Connection Failed!");
 while($row = mysqli_fetch_array($data, MYSQLI_ASSOC)){
 ?>

 <tr class="odd gradeX">
 <td> <?php echo $row['dish_id']?> </td>
 <td> <?php echo $row['dish_name']?>  </td>
 <td>

 <div class='btn-group'>
 <form action='deleterice.php' method='POST'>
  <button class='btn btn-default' type='submit'><a class='fa fa-trash-o'> Delete</a></button>
          <input type='hidden' value=" <?php echo $row['dish_id'];?>" name="iduse">
           </form>
         </div>
       </td>
       </tr>

         <?php
     }
     ?>

最佳答案

所以您的意思是,当您进入menurice.php?cat_id=1时,它应该只显示莱斯?

使用以下查询:SELECT * FROM food_dish WHERE dish_cat = ?并将$_GET['cat_id']绑定到查询。

例如:

$stmt = mysqli_prepare($conn, "SELECT dish_id, dish_name FROM food_dish WHERE dish_cat = ?");

mysqli_stmt_bind_param($stmt, "i", $_GET['cat_id']);
mysqli_stmt_execute($stmt);

mysqli_stmt_bind_result($stmt, $dish_id, $dish_name);

while (mysqli_stmt_fetch($stmt)) {
    ?>

<tr class="odd gradeX">
  <td> <?php echo $dish_id; ?> </td>
  <td> <?php echo $dish_name; ?> </td>
  <td>
    <div class='btn-group'>
      <form action='deleterice.php' method='POST'>
        <button class='btn btn-default' type='submit'><a class='fa fa-trash-o'> Delete</a></button>
        <input type='hidden' value="<?php echo $dish_id; ?>" name="iduse">
      </form>
    </div>
  </td>
</tr>

    <?php
}

10-07 14:40