dongmu5815 2016-11-12 05:49
浏览 52
已采纳

如何在PHP MySQL中检查DATE和DATETIME的每一行?

I have three sets of datetime type with two columns dtfrom and dtto in mysql database

row 1 : dtfrom 2016-11-11 , dtto 2016-11-11
row 2 : dtfrom 2016-11-12 , dtto 2016-11-12
row 3 : dtfrom 2016-11-13 , dtto 2016-11-13 

The problem is the output cannot show "Duplicate date" if I input the same date in database.

It always show "Data not found !"

Can somebody help here?

<?php
    $connect      = mysqli_connect("localhost", "root", "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);

        $sql            = "SELECT * FROM table WHERE user_id='{$user_id}'  AND dtfrom >= '{$dtfrom_user}' AND dtto <= '{$dtto_user}'";
        $run            = mysqli_query($connect, $sql); 
        if($run && mysqli_num_rows($run) > 0 )
        {
            while($result = mysqli_fetch_assoc($run))
            {
                echo "Date duplicate";
            }
        mysqli_free_result($run);
        }
        else
        {
            echo "Date not found !";
        }
    }
?>
<form action="datetime.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 ="date" name="dtfrom" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Date to : </td>
            <td><input type ="date" name="dtto" size="30"></td>
        </tr>
    </table>    

    <p><input class="btnSuccess" type ="submit" name="Submit" value="Submit"> </p>              
</form>
  • 写回答

1条回答 默认 最新

  • doubei5114 2016-11-12 05:57
    关注

    Try to check error . Use echo mysqli_error($con); instead echo "Date not found !";

    And Change your Query to

     $user_id        = $_POST['user_id'];
     $dtfrom         = $_POST['dtfrom'];
     $dtfrom_user    = strtotime($dtfrom);
     $dtto           = $_POST['dtto'];
     $dtto_user      = strtotime($dtto);
    
    
     $dtfrom_user  = date("Y-m-d",$dtfrom_user );
     $dtto_user  = date("Y-m-d",$dtto_user);
    
     $sql = "SELECT * FROM table WHERE user_id='".$user_id."'  AND dtfrom >= '".$dtfrom_user."' AND dtto <= '".$dtto_user."'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?