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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀