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 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加