如何防止PHP MySQL中的可重复输出?

时间:2016-11-13 07:47:16

标签: php mysql

我在mysql数据库中创建了三行数据,其中有四列 dtfrom,dtto,timeout,timein

我有三组行

dtfrom      dtto        timeout     timein
2016-11-11  2016-11-11  0830     1030
2016-11-12  2016-11-12  0830     1030
2016-11-13  2016-11-13  0830     1030

然而,"时间没有通过!" 的输出显示三次,如下所示

时间没过去了!时间没过了!时间没过了!

我想要的是错误"时间未通过!" 仅对所有查询显示一次。因为如果我有数百个数据行,那就有问题了。错误将出现在数百个时间不是吗?

第二个问题是,如果输入是0845 - 1730或0845 - 0945 ,无法运行错误。 如果输入与数据库0830 - 1030

完全相同,则仅运行错误

有人可以帮忙吗?代码如下

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

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

        $dtfrom         = $_POST['dtfrom'];
        $dtfrom_user    = strtotime($dtfrom);
        $dtto           = $_POST['dtto'];
        $dtto_user      = strtotime($dtto);

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

        $sql            = "SELECT * FROM table WHERE user_id='{$user_id}'  AND dtfrom >= '{$dtfrom}' AND dtto <= '{$dtto}'";
        $run            = mysqli_query($connect, $sql);
        if($run && mysqli_num_rows($run) > 0 )
        {               
            while($result = mysqli_fetch_assoc($run))
            {
                $sql1   = "SELECT * FROM table WHERE user_id='{$user_id}'  AND timeout >= '{$timeout}' AND timein <= '{$timein}'";
                $run1   = mysqli_query($connect,$sql1);
                if($run1 && mysqli_num_rows($run1) > 0)
                {
                    while($result1 = mysqli_fetch_array($run1))
                    {
                        echo "Time not passed !<br/>";
                    }
                mysqli_free_result($run1);
                }
                else
                {
                    echo "Date & Time passed !";
                    echo mysqli_error($connect);
                }
            }
        mysqli_free_result($run);
        }
        else
        {
            echo "Date passed !";
            echo mysqli_error($connect);
        }
    }
?>
<form action="tt.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>Date from : </td>
            <td><input type ="text" name="dtfrom" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Date to : </td>
            <td><input type ="text" name="dtto" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Timeout : </td>
            <td><input type ="text" name="timeout" size="30"></td>
        </tr>
                <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Timein : </td>
            <td><input type ="text" name="timein" size="30"></td>
        </tr>
    </table>    

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

提前致谢。

0 个答案:

没有答案