PHP的MySQL的HTML形式选择循环父级和子级

时间:2018-11-12 21:53:44

标签: php mysql

希望一切都很好

我正在努力基于php和mysql构建html表单。

我要坚持的是:根据类别选择子类别

例如,我有2个父类别的游戏和软件 我有4个子类别,分别是游戏(动作和悲剧)和软件(互联网和防病毒)两个子类别。

我要寻找的是当我选择类别中的游戏时,子类别仅显示(动作和悲剧),而当我选择软件时,该子类别中的游戏仅显示(互联网和防病毒)

亲子游戏child1动作,child2悲剧

父级软件child1 Internet,child2防病毒

Here is the mysql table

enter image description here

这是php代码

<?php
$conn=mysqli_connect("localhost","root","root","mene");
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * from cates WHERE parent_name = 'main'";
$result = mysqli_query($conn,$sql);
$sql1 = "SELECT * from cates WHERE parent_name = 'category'";
$result1 = mysqli_query($conn,$sql1);
$sql2 = "SELECT * from cates WHERE parent_name = 'games'";
$result2 = mysqli_query($conn,$sql2);
$sql3 = "SELECT * from cates WHERE parent_name = 'software'";
$result3 = mysqli_query($conn,$sql3);
?>
<form action="action.php">
  <a>choose the operating system</a>
  <select name="main">
    <option value="main">-----</option>
    <?php 
            while($row = mysqli_fetch_array($result)) {     
         ?>
    <option value="<category"><?php echo $row['name']; ?></option>
    <?php } ?>
  </select>
  <br><br>
  <a>choose the category</a>
  <select name="parent">
    <option value="parent">-----</option>
    <?php 
            while($row = mysqli_fetch_array($result1)) { 
         ?>
    <option value="<category"><?php echo $row['name']; ?></option>
    <?php } ?>
  </select>
  <br><br>
  <a>choose sub category</a>
  <select name="child">
    <option value="child">-----</option>
    <php  ?>
    <?php 
            while($row1 = mysqli_fetch_array($result2)) {
         ?>
    <option value="sub_category"><?php echo $row1['name']; ?></option>
    <?php } ?>
  </select>
  <br><br>
  <input type="submit">
</form>
<?php mysqli_close($conn); ?>
<php } ?>

有什么帮助吗?

0 个答案:

没有答案