2016-09-16 69 views
0

我想檢查一下,預訂ID是否不在數據庫中,以及它是否大於今天。數據庫檢查部分工作。但如果條件成立,這部分不會經歷。我認爲ifelse陳述有問題。如何正確使用if語句?

else if($checkindate > $today) 
{ 
    $bidErr="This booking is not comming today. Please check again"; 
} 

我在這裏包括完整的代碼。預訂ID bid來自一個表單。

<?php 
    $today=date("Y-n-j"); 
    echo "<h4>Today is <font color='red'>".$today."</font></h4><br><br>"; 
    // define variables and set to empty values 
    $bidErr = ""; 
    $bid = ""; 

    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     $flag = 1; 
     if (empty($_POST["bid"])) { 
      $bidErr = "Booking ID is required."; 
      $flag=0; 
     } else { 
      $bid = test_input($_POST["bid"]); 
      // check if name only contains letters and whitespace 
      if (!preg_match("/^[0-9]*$/",$bid)) { 
       $bidErr = "Only Numbers are allowed"; 
       $flag=0; 
      } 
     } 
     include("connect.php"); 
     if($flag=="1"){ 
      $SQL="SELECT guestid,checkindate FROM bookings WHERE bookingid='$bid'"; 
      $run=mysql_query($SQL,$con) or die ("SQL error"); 
      $rec=mysql_fetch_array($run); 
      $row=mysql_num_rows($run); 

      $checkindate = $rec['checkindate']; 

      echo $checkindate; 

      if ($row < 1) { 
       $bidErr="Invalid BookingID. Please check again"; 
      } 
      else if($checkindate > $today) { 
       $bidErr="This booking is not comming today. Please check again"; 
      } else { 
       $_SESSION["chinbid"] = $bid ; 
       header("Location: checkinhandler.php"); 
       exit; 
      } 
     } 
    } 

    function test_input($data) { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlspecialchars($data); 
     return $data; 
    } 
?> 
+0

你在哪裏把'$ _POST [「bid」]'作爲'$ bid'? –

+1

你不應該使用任何mysql _ * - 函數。他們已被棄用,因爲PHP 5.5和完全刪除在PHP 7.0 – Manish

+0

嘗試'elseif'而不是'else if' – RST

回答

0

檢查您的$checkindate是否與$today日期相同。

嘗試檢查$checkindate格式與date("Y-m-d")(2016-09-16)類似。如果是的話$today會像

$today=date("Y-m-d"); 

檢查php manual

此外,我建議去mysqli_ *而不是使用mysql_ *。因爲它從PHP 5.5開始已被棄用,並在PHP 7.0中完全刪除。

+0

謝謝。我會檢查 – din

+0

IT的作品..這是$ today = date(「Y-n-j」); \t \t and checkindate format is(「Y-m-d」);謝謝 – din

0

試試這個:

else if(date("Y-n-j", strtotime($checkindate)) > $today) 

我猜你的$checkindate是不相同的格式(Y-N-J)作爲$today變量。

+0

好的我會檢查..謝謝 – din

+0

謝謝。日期格式錯誤,如你所說。我修好了它。 – din

0
<?php 
$row='0'; 
date_default_timezone_set('UTC'); 
$date=date_create("2016-09-15"); 
$today = date("Ymd"); 
$date= date_format($date, 'Ymd'); 
echo "Checkin date is " . $date. "<br>"; 
echo "Today is " . $today."<br>"; 
if ($row < '1'){ 
    echo "no records"; 
} elseif ($date == $today) { 
    echo "dates are the same"; 
} else { 
    echo "check in handler"; 
} 
?>