如何在PHP MySQL中循环中运行嵌套语句?

时间:2016-11-11 12:56:28

标签: php mysql

我在while循环中创建if语句。

我在表格中有三行数据,如下所示

row 1 : **timeout 0830 , timein 1030** 

row 2 : **timeout 1230 , timein 1300**

row 3 : **timeout 1400 , timein 1730**

问题是,输出显示如下

Time added ! Duplicate Time added

数据仍然在第1和第3行添加。我不知道为什么。

我想要的是,如果任何一行重复而不添加数据,则显示错误。

假设我输入第二行的数据,即1230和1300.我希望输出显示为:

重复

第1行和第2行不会添加任何数据,因为其中一行是重复的。

任何解决方案?

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

    if(isset($_POST['Submit'])){    

        $user_id        = $_POST['user_id'];
        $timeout        = $_POST['timeout'];        
        $timein         = $_POST['timein'];

        $sql       = "SELECT * FROM table WHERE user_id='$user_id'";
        $get       = mysqli_query($connect, $sql);
        if($get && mysqli_num_rows($get) > 0 ){
            while($run2 = mysqli_fetch_assoc($get)){
                $timeout_new    = $run2['timeout'];
                $timein_new     = $run2['timein'];  
                if(($timeout >= $timeout_new) && ($timein <= $timein_new)){
                    echo "Duplicate !";
                }
                else{
                    $add         = "INSERT INTO table (timeout,        timein)
                                               VALUES ('$timeout',     '$timein')";
                    $addDateTime = mysqli_query($connect,$add);
                    echo "Time added !";
                }
            }
            mysqli_free_result($get);
        }
    }
?>
<form action="dd.php" method="post">    
    <table>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>User ID : </td>
            <td><input type ="text" name="user_id" size="30"></td>
        </tr>   
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Time out : </td>
            <td><input type ="text" name="timeout" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Time in : </td>
            <td><input type ="text" name="timein" size="30"></td>
        </tr>
    </table>    

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

1 个答案:

答案 0 :(得分:0)

尝试在数据库级别进行timeout timein比较,而不是php。

将您的代码更改为类似内容以查看其是否有效:

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

    if (isset($_POST['Submit'])) {    
        $user_id        = $_POST['user_id'];
        $timeout        = $_POST['timeout'];        
        $timein         = $_POST['timein'];

        $sql       = "SELECT * FROM table WHERE user_id='{$user_id}' AND timeout >= {$timeout} AND timein <= {$timein}";
        $get       = mysqli_query($connect, $sql);
        if($get && mysqli_num_rows($get) > 0 ){
            echo "Duplicate !";
        } else {
            $add         = "INSERT INTO table (timeout, timein)
                            VALUES ('{$timeout}', '{$timein}')";
            $addDateTime = mysqli_query($connect, $add);
            echo "Time added !";
        }
        mysqli_free_result($get);
    }
?>