简体   繁体   English

如何正确使用if else语句?

[英]How to use if else statement correctly?

I want check, whether booking id is not in the database as well as it is greater than today. 我想检查预订ID是否不在数据库中以及是否大于今天。 The database checking part working. 数据库检查部分正在工作。 But this part not going through if the condition is true. 但是,如果条件为真,则此部分不会进行。 I think something wrong with the if else statements. 我认为if else语句有问题。

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

I have include full code here. 我在这里包含完整的代码。 booking Id bid coming from a form. 来自表单的预订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;
    }
?>

Try this: 尝试这个:

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

I'm guessing your $checkindate is not in the same format (Ynj) as your $today variable. 我猜想您的$checkindate$today变量的格式(Ynj)不同。

Check your $checkindate is same as $today date. 检查您的$checkindate是否与$today日期相同。

Try to check $checkindate format is like date("Ymd") (2016-09-16). 尝试检查$checkindate格式是否类似于date("Ymd") (2016-09-16)。 IF so then $today will be like 如果是这样的话, $today会像

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

Check the php manual 检查PHP手册

Also i suggest to go for mysqli_* instead of using mysql_*. 我也建议去mysqli_ *而不是使用mysql_ *。 Because It has been deprecated since php 5.5 and completely removed in php 7.0 . 因为从php 5.5开始不推荐使用,并且在php 7.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";
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM