一尘不染

仅在从下拉菜单中选择值时才显示结果

sql

此代码是我的搜索表单的演示部分。实际的搜索表单包含3个下拉列表,从下拉列表中选择的值将作为关键字进行搜索。此下拉列表包含芒果,苹果,葡萄等水果。搜索代码运行正常。但是问题在于下拉列表中显示的第一个选项是“选择”(不包含任何值),低于该值会开始实际的水果列表,但是在为第一个页面加载时仍会选择第一个水果的值时间
。我想做的是,当页面首次加载时,即下拉列表中的值是Select(选择),则什么都不会显示,之后,当用户从下拉列表中选择值并点击Submit(提交)按钮时,结果才出现应该得到展示

<div class="col-md-3 col-sm-5">
    <div class="media">
        <div class="media-body">
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "db";

                // Create connection
                $con = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$con) {
                    die("Connection failed: " . mysqli_connect_error());
                }

                $sql = "SELECT fruits FROM fruits";
                $result = $con->query($sql);
                echo "<label for='fruits'>Treatment Type: </label>";
                echo "<select name='fruits' id='fruits' class='form-control'><option value=''>--Select--</option>";
                while($row = $result->fetch_assoc()) {
                echo "<option value='" . $row['fruits'] . "'>" . $row['fruits'] . "</option>";
                }
                echo "</select>";
            ?>
        </div>
    </div>
</div>

搜索部分的代码

<?php
    $con=mysqli_connect("","","","");// Check connection
    if (mysqli_connect_errno()) 
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $fruits = mysqli_real_escape_string($con, $_POST['fruits']);

        $sql1 = "SELECT * FROM treatment WHERE fruits LIKE '%$fruits%'";
        $result = mysqli_query($con, $sql1);
        echo "<table class='table table-striped table-bordered responsive'>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Fruits</th>
                </tr>
            </thead>";

            if (mysqli_num_rows($result) > 0) 
                {
                    while($row = mysqli_fetch_assoc($result)) 
                        {
                            echo "<tbody data-link='row' class='rowlink'>";
                                echo "<tr>";
                                    echo "<td><a href='#'>" . $row['name'] . "</a></td>";
                                    echo "<td>" . $row['type'] . "</td>";       
                                    echo "<td>" . $row['fruits'] . "</td>";

                                echo "</tr>";
                            echo "</tbody>";    
                        }

                }
            else 
                {
                    echo "0 results";
                }
            echo "</table>";
            mysqli_close($con);
?>

如果有人可以指导我将不胜感激

PS(编辑部分)

<div class="col-md-3 col-sm-5">
    <div class="media">
        <div class="media-body">
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "db";

                // Create connection
                $con = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$con) 
                {
                    die("Connection failed: " mysqli_connect_error());
                }

                $sql = "SELECT fruits FROM fruits";
                $result = $con->query($sql); ?>
                echo "<label for="fruits">Treatment Type: </label>";
                echo "<select name="fruits" id="fruits" class="form-control">
                <option value="" <?php if(!isset($_POST['fruits'])) { ?>selected<?php } ?>>--Select--</option>";
                <?php 
                while($row = $result->fetch_assoc()) { ?>

                echo "<option value="<?php echo $row['fruits']; ?>" <?php if(isset($_POST['fruits']) && $_POST['fruits'] == $row['fruits']) { ?>selected<?php } ?>><?php echo $row['fruits']; ?></option>";
                <?php } ?>
            </select>
        </div>
    </div>
</div>

阅读 194

收藏
2021-05-16

共1个答案

一尘不染

在各<option>部分中,只需修改一下即可。添加selected条件:

<div class="col-md-3 col-sm-5">
    <div class="media">
        <div class="media-body">
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "db";

                // Create connection
                $con = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$con) {
                    die("Connection failed: " . mysqli_connect_error());
                }

                $sql = "SELECT fruits FROM fruits";
                $result = $con->query($sql); ?>
            <label for="fruits">Treatment Type: </label>
            <select name="fruits" id="fruits" class="form-control">
                <option value="" <?php if(!isset($_POST['fruits']) || (isset($_POST['fruits']) && empty($_POST['fruits']))) { ?>selected<?php } ?>>--Select--</option>
                <?php 
                while($row = $result->fetch_assoc()) {
                ?>
                <option value="<?php echo $row['fruits']; ?>" <?php if(isset($_POST['fruits']) && $_POST['fruits'] == $row['fruits']) { ?>selected<?php } ?>><?php echo $row['fruits']; ?></option>
                <?php } ?>
            </select>
        </div>
    </div>
</div>

结果页:

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

        $fruits =   mysqli_real_escape_string($con, $_POST['fruits']);

        if(!empty($fruits)) {
            $sql1   =   "SELECT * FROM treatment WHERE fruits LIKE '%$fruits%'";
            $result =   mysqli_query($con, $sql1);
            $count  =   mysqli_num_rows($result);
        }
        else
            $count  =   0; ?>

        <table class='table table-striped table-bordered responsive'>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Fruits</th>
                </tr>
            </thead>
        <?php
            if ($count > 0)  {
                while($row = mysqli_fetch_assoc($result)) {
        ?>
            <tbody data-link='row' class='rowlink'>
                <tr>
                    <td><a href='#'><?php echo $row['name']; ?></a></td>
                    <td><?php echo $row['type']; ?></td>
                    <td><?php echo $row['fruits']; ?></td>
                </tr>
            </tbody>
        <?php
                }
            }
            else
                echo "0 results";
        ?>
        </table>
        <?php mysqli_close($con); ?>
2021-05-16