选择特定类别后显示子类别

时间:2014-10-14 12:20:58

标签: php mysql sql sql-server mysqli

我的表单有两个参数设施和属性。 amenities参数有一个下拉菜单,其值从一个单独的表中填充。第二个参数是属性,其值将由用户输入并将保存在数据库中。数据库视图将是:

ID  Attribute  Amenities
1     A          Aa
2     B          Bb

此部分的代码是

<form class="form-horizontal" role="form" action="admin_insert_attribute.php" enctype="multipart/form-data" method="post">    
    <div class="form-group">
        <label class="col-lg-3 control-label">Amenities:</label>
            <div class="col-lg-8">    
                <?php
                    $con=mysqli_connect("abc","abc","abc","abc");
                    // Check connection
                    if (mysqli_connect_errno()) 
                    {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                    }

                    $result = mysqli_query($con,"SELECT amenities FROM amenities");
                    echo "<select name='amenities'>";
                    while($row = mysqli_fetch_array($result)) 
                    {
                    echo "<option value='" . $row['amenities'] . "'>" . $row['amenities'] . "</option>";
                    }   
                    echo "</select>";   
                    mysqli_close($con);
                ?>  
            </div>
    </div>    
    <div class="form-group">
        <label class="col-lg-3 control-label">Attribute:</label>
            <div class="col-lg-8">
                <input class="form-control" name="attribute" value="" type="" required>
            </div>
    </div>

    <div class="form-group">
        <label class="col-lg-3 control-label"></label>
            <div class="submit">
             <input class="btn btn-primary" value="Save " type="submit" name="submit">    
            </div>  
    </div>
</form>

现在我希望当从下拉菜单中选择舒适性时,该特定设施下的所有属性也应以表格形式显示在同一页面上。输入和显示属性的代码在同一页面上完成。

问题是我能够添加属性,但它们没有显示在屏幕上。我用于显示的代码是

<?php
    $con=mysqli_connect("abc","abc","abc","abc");
    // Check connection
    if (mysqli_connect_errno()) 
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

    $result = mysqli_query($con,"SELECT * FROM attributes  WHERE amenities = '".$amenities."'");
    echo "<table class='table table-striped table-bordered table-hover'>
        <thead>
            <tr>
                <th>ID</th>
                <th>Attribute</th>
            </tr>
        </thead>";

    while($row = mysqli_fetch_array($result)) 
        {
            echo "<tbody data-link='row' class='rowlink'>";
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['attribute'] . "</td>";      
            echo "</tr>";
            echo "</tbody>";    
        }
    echo "</table>";
    mysqli_close($con);
?>

2 个答案:

答案 0 :(得分:0)

如果您从列表中选择选项并显示其他选项您必须使用AJAX或/和javascript来执行此操作。

答案 1 :(得分:0)

    Try it,i think you didn't select Database name


$con=mysqli_connect("localhost","root","");
    // Check connection
    if (mysqli_connect_errno()) 
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    mysql_select_db("test") or die(" error database");

    $result = mysqli_query($con,"SELECT * FROM attributes  WHERE amenities = '".$amenities."'");