我想檢查一下,預訂ID是否不在數據庫中,以及它是否大於今天。數據庫檢查部分工作。但如果條件成立,這部分不會經歷。我認爲if
else
陳述有問題。如何正確使用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;
}
?>
你在哪裏把'$ _POST [「bid」]'作爲'$ bid'? –
你不應該使用任何mysql _ * - 函數。他們已被棄用,因爲PHP 5.5和完全刪除在PHP 7.0 – Manish
嘗試'elseif'而不是'else if' – RST