dstxpei5823 2016-11-11 12:56
浏览 95
已采纳

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

I create if statement inside while loop .

And I have three rows of data in table like below

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

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

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

The problem is , the output show like this

Time added ! Duplicate Time added

And the data still added in the 1st and 3rd row. I don't know why.

What I want is to display errors if any one of the rows is duplicate without adding the data.

Let say I input data for the 2nd row which is 1230 and 1300. I want the output to appear as:

Duplicate

And 1st and 2nd row will not adding any data since one the rows is duplicate.

Any solution ?

<?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条回答 默认 最新

  • doubishi8303 2016-11-11 14:19
    关注

    Try to do the timeout timein comparison in a database level and not php.

    Change your code to something like this to see if it works:

    <?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);
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题