如果日期< =当前日期,则重定向

时间:2016-06-04 11:54:45

标签: php sql

如果数据库中的date在今天之前,我想检查数据库中的date并将用户重定向到另一个页面。

我目前有:

<?php
 $user_name = "root";
 $password = "";
 $database = "db";
 $server = "localhost";

 $db_handle = mysql_connect($server, $user_name, $password);
 $db_found = mysql_select_db($database, $db_handle);

 if ($db_found) {
  $result = mysql_query("SELECT `date` FROM users WHERE user_id='1'");
  $res = mysql_fetch_row($result);

  if ($res[0] == 1){
   header ("Location: ./error.php");
  }                        
  else
  {
   echo ' ';
  }
 }
 else
 {
  print "Database NOT Found.";
  mysql_close($db_handle);
 }
?>

我怎样才能使这个工作?

1 个答案:

答案 0 :(得分:1)

以下内容会将其添加到您的数据库查询中,如果找到两者,则只返回一行...

$user_name = "root";
$password = "";
$database = "db";
$server = "localhost";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if (!$db_found) {
    mysql_close($db_handle);
    print "Database NOT Found.";
    exit();
}

$result = mysql_query("SELECT 1 FROM users WHERE user_id='1' AND date < CURDATE()");
if ($res = mysql_fetch_row($result)) {
    //user matched and date is larger that current date
    mysql_close($db_handle);
    header ("Location: ./error.php");
    exit();
}                        

//user not matched or date is not larger that current date
//do something else