html—将鼠标悬停在php和mysql中的类别名称上,显示数据库中每个类别的子类别

7fyelxc5  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(268)

经过这里提供的一些解决方案,我仍然无法正确地理解。当我将鼠标悬停在类别名称上时,只有最后一个类别的子类别正在从数据库中提取和显示。

<div class="mega-menu-category">
      <ul class="nav">
        <!-- Other side-menu navs -->
        <?php $sql=mysqli_query($con,"SELECT id,categoryName FROM category");
         while($row=mysqli_fetch_array($sql))
          {
            ?>
          <li><a href="category.php?cid=<?php echo $row['id'];?>"><?php echo $row['categoryName'];?></a>
            <?php
            $getid = $row['id'];
            }?>
          <div class="wrap-popup column1">
            <div class="popup">
              <?php $sql=mysqli_query($con,"SELECT id, subcategory FROM subcategory WHERE categoryid= '$getid'");
               while($row=mysqli_fetch_array($sql))
                   {
                   ?>
              <ul class="nav">
                <li><a href="sub-category.php?scid=<?php echo $row['id'];?>"><?php echo htmlentities($row['subcategory']);?></a></li>
              </ul>
              <?php } ?>
            </div>

          </div>
        </li>

        <!-- used one  -->

      </ul>
    </div>
ct2axkht

ct2axkht1#

您正在使用外部查询的所有结果集,而不是在内部循环中使用它。
下一个问题是对两个查询使用相同的变量,因为运行内部查询时,会破坏外部外观的结果集

<div class="mega-menu-category">
    <ul class="nav">
        <!-- Other side-menu navs -->
<?php 
$sql=mysqli_query($con,"SELECT id,categoryName FROM category");
while($row=mysqli_fetch_array($sql)) {
?>
        <li><a href="category.php?cid=<?php echo $row['id'];?>">
                <?php echo $row['categoryName'];?>
            </a>
<?php
//}  this causes the first loop to run to its end hence only one category processed
?>
            <div class="wrap-popup column1">
                <div class="popup">
<?php 
    // use different variable otherwise you will destroy the first resultset
    $sql1=mysqli_query($con,"SELECT id, subcategory 
                                FROM subcategory 
                                WHERE categoryid= '$row[id]'");
    while($row1=mysqli_fetch_array($sql1)){
?>
            <ul class="nav">
                <li>
                    <a href="sub-category.php?scid=<?php echo $row['id'];?>">
                        <?php echo htmlentities($row1['subcategory']);?>
                    </a>
                </li>
            </ul>
<?php 
    }  // end inner while
} // end outer while
?>
            </div>
        </div>
    </li>

    <!-- used one  -->
</ul>
</div>

我不得不提一下,你是开放的sql注入攻击。即使你正在逃避输入,它也不安全!您应该考虑在 MYSQLI_ 或者 PDO api而不是串联的值

相关问题