如何正确使用if else语句?

时间:2016-09-16 07:28:39

标签: php

我想检查一下,预订ID是否不在数据库中,以及它是否比今天更大。数据库检查部分工作。但如果条件成立,这部分不会通过。我认为if else语句有问题。

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

我在这里包含完整代码。从表单中预订身份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;
    }
?>

3 个答案:

答案 0 :(得分:0)

试试这个:

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

我猜测您的$checkindate格式(Y-n-j)与$today变量的格式不同。

答案 1 :(得分:0)

检查您的private final DoubleProperty value = new SimpleDoubleProperty(); public DoubleProperty valueProperty() { return value; } public void setValue(double value) { this.value.set(value); } public double getValue() { return value.get(); } 是否与$checkindate日期相同。

尝试检查$today格式如$checkindate(2016-09-16)。如果是,那么date("Y-m-d")就像

$today

检查php manual

另外我建议使用mysqli_ *而不是使用mysql_ *。因为它已经从PHP 5.5被弃用并在php 7.0中被完全删除。

答案 2 :(得分: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";
}
?>