我在这里有问题

如何检查任一输入是否在数据范围内?第一个if语句仅在两个数据具有相同的值或两个数据在数据范围之间时才执行

但是如果其中一个数据在范围内,则不会执行

我的问题是,如何检查输入之一是否在范围之间?

代码如下:

<?php
    $connect      = mysqli_connect("", "root", "", "");
    global $connect;

    if(isset($_POST['Submit'])){
        $time_out   = $_POST['time_out'];
        $time_enter = $_POST['time_enter'];

        $sql = "SELECT * FROM table";
        $get = mysqli_query($connect,$sql);
        if($get && mysqli_num_rows($get) > 0 ){
            while($row = mysqli_fetch_assoc($get)){

            $time_out_db    = $row['time_out'];
            $time_enter_db  = $row['time_enter'];

                if(($time_out >= $time_out_db) && ($time_enter <= $time_enter_db))
                {
                    echo "Time is in between";
                }
                else if (){
                    echo "Either one of the time_out or time_enter is in the range";
                }
            }
            mysqli_free_result($get);
        }else{
            echo "Nothin here !";
        }
    }
?>
<form action="time.php" method="post">
    <table>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Time out : </td>
            <td><input type ="text" name="time_out" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Time enter : </td>
            <td><input type ="text" name="time_enter" size="30"></td>
        </tr>
    </table>

    <p><input class="btnSuccess" type ="submit" name="Submit" value="Submit"> </p>
</form>


谢谢。

最佳答案

在第二个or中使用||if)条件:

<?php
    $connect      = mysqli_connect("", "root", "", "");
    global $connect;

    if(isset($_POST['Submit'])){
        $time_out   = $_POST['time_out'];
        $time_enter = $_POST['time_enter'];

        $sql = "SELECT * FROM table";
        $get = mysqli_query($connect,$sql);
        if($get && mysqli_num_rows($get) > 0 ){
            while($row = mysqli_fetch_assoc($get)){

            $time_out_db    = $row['time_out'];
            $time_enter_db  = $row['time_enter'];

                if(($time_out >= $time_out_db) && ($time_enter <= $time_enter_db))
                {
                    echo "Time is in between";
                }
                else if (($time_out >= $time_out_db) || ($time_enter <= $time_enter_db)){
                    echo "Either one of the time_out or time_enter is in the range";
                }
            }
            mysqli_free_result($get);
        }else{
            echo "Nothin here !";
        }
    }
?>
<form action="time.php" method="post">
    <table>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Time out : </td>
            <td><input type ="text" name="time_out" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Time enter : </td>
            <td><input type ="text" name="time_enter" size="30"></td>
        </tr>
    </table>

    <p><input class="btnSuccess" type ="submit" name="Submit" value="Submit"> </p>
</form>

关于php - 如何检查查询之一是否在PHP MySQL中的数据范围内?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40606559/

10-12 07:34
查看更多