本文介绍了如何检查PHP中DATE和DATETIME的每一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三组 datetime
类型,两列 dtfrom
和 dtto
在mysql数据库中
第1行:dtfrom 2016-11-11,dtto 2016-11-11
/ pre>
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数据库);
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))
{
回覆日期重复;
}
mysqli_free_result($ run);
}
else
{
echoDate not found!;
}
}
?>
< form action =datetime.phpmethod =post>
< table>
< tr>
< td>< i class =fa fa-unlock-alt>< / i> < / td>
< td>用户ID:< / td>
< td>< input type =textname =user_idsize =30>< / td>
< / tr>
< tr>
< td>< i class =fa fa-unlock-alt>< / i> < / td>
< td> Date from:< / td>
< td>< input type =datename =dtfromsize =30>< / td>
< / tr>
< tr>
< td>< i class =fa fa-unlock-alt>< / i> < / td>
< td>日期到:< / td>
< td>< input type =datename =dttosize =30>< / td>
< / tr>
< / table>
< p>< input class =btnSuccesstype =submitname =Submitvalue =Submit> < / p>
< / form>
解决方案尝试检查错误。使用echo mysqli_error($ con);而是回覆Date not found!;
并将您的查询更改为
$ 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。'和dtto <='$ dtto_user
I have three sets of
datetime
type with two columnsdtfrom
anddtto
in mysql databaserow 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>
解决方案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."'";
这篇关于如何检查PHP中DATE和DATETIME的每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!