gpt4 book ai didi

php - 比较数据库中的日期是否大于当前日期

转载 作者:行者123 更新时间:2023-12-01 00:46:08 25 4
gpt4 key购买 nike

我卡在这个了。

我想要完成的事情听起来很容易,但我仍然无法让它工作...

我有几个用户存储在数据库中。他们可以在登录后访问页面。但是每个用户都有一个结束日期。因此,如果这一天过去了,他将无法再看到该页面并将被重定向到另一个页面。但是每个用户都有不同的日期。

当用户输入他的凭据时,将创建一个 $_SESSION 来存储他的登录名。我需要 sql 使用此 $_SESSION 值从特定用户获取日期。

我目前拥有的:

$sql="SELECT * FROM $tbl_name WHERE licentiehouder=$naamLicentiehouder";
$naamLicentiehouder = $_SESSION['doorsturen'];
$result=mysql_query($sql);
$row = mysql_fetch_row($result);
$mydate = $row['vervaldatum'];
$curdate=strtotime("now");

if($curdate <= $mydate && $_SESSION['doorsturen'] == 'userONE') {
header("Location: userONE.php");
} else if ($curdate <= $mydate && $_SESSION['doorsturen'] == 'userTWO') {
header("Location: userTWO.php");
} else if($curdate > $mydate) {
header("Location: extend_license.php");
}

再说一次,每个用户都有自己的许可证,该许可证将在确切的日期到期。此日期存储在数据库中。因此,如果 userONE 登录,则 $_SESSION 值设置为 userONE。 sql 读取此值并仅获取与此值匹配的行。如果今天(当前日期)大于存储的日期(因此他的许可证已过期),他将被重定向到一个页面以延长他的许可证。如果没有,他将能够看到他的个人页面。

希望有人能帮忙?!

最佳答案

您的变量 $naamLicentiehouder 在您运行查询时未定义。尝试:

$naamLicentiehouder = $_SESSION['doorsturen'];
$sql="SELECT * FROM $tbl_name WHERE licentiehouder='$naamLicentiehouder'";

mysql_query() 等也已贬值。使用 mysqli。我更喜欢面向对象的方法,因为它可以节省大量重复输入。

为了澄清,面向对象的方法,在一个单独的 restricted 页面上,我们将调用 connect.php:

<?php
// reusable db() function can be called inside other functions
function db(){
return new mysqli('host', 'username', 'password', 'database');
}
?>

现在在您的其他页面上:

<?php
include_once 'restricted/connect.php'; $db = db();
if($db->connect_error)die("Connection Failure: {$db->connect_error}");
$naamLicentiehouder = $_SESSION['doorsturen']; // I would shorted this variable
if($result = $db->query("SELECT vervaldatum FROM your_table_name WHERE licentiehouder='$naamLicentiehouder'")){
if($result->num_rows > 0){
$row = $result->fetch_object(); $mydate = $row->vervaldatum;
$curdate = strtotime('now');
if($curdate <= $mydate && $naamLicentiehouder === 'userONE'){
header('LOCATION: userONE.php'); die;
}
elseif($curdate <= $mydate && $naamLicentiehouder === 'userTWO'){
header('LOCATION: userTWO.php'); die;
}
elseif($curdate > $mydate){
header('LOCATION: extend_license.php'); die;
}
else{
die('Date Issue.');
}
}
else{
die('No results were found.');
}
}
else{
die('Error :'.$db->error);
}
$result->free(); $db->close();

关于php - 比较数据库中的日期是否大于当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21791553/

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