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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化