gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:00:15 25 4
gpt4 key购买 nike

我在 mysql 数据库中有三组 datetime 类型,其中有两列 dtfromdtto

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

问题是如果我在数据库中输入相同的日期,输出无法显示“重复日期”

总是显示“未找到数据!”

有人可以帮忙吗?

<?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>

最佳答案

尝试检查错误。使用 echo mysqli_error($con);相反,回显“未找到日期!”;

并将您的查询更改为

 $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."'";

关于php - 如何在 PHP MySQL 中检查每一行的 DATE 和 DATETIME?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40559978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com