简体   繁体   中英

How to run nested statement inside while loop in 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>

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);
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM