PHP预订可用性日期范围

时间:2014-05-14 06:37:37

标签: php mysql sql

我是php编程的新手。

我的表格如下

reservation_id        roomtype        dor           dco           los           status
100                   superior        2014-05-15    2014-05-18    3           checked-in
101                   deluxe          2014-05-20    2014-05-23    3           checked-in
102                   single          2014-05-24    2014-05-28    4           checked-in

dor是预订日期或入住日期

dco是退房日期

los是逗留时间

现在我对房间供应感到困惑,因为我是编程新手。

案件是这样的。例如,预订ID 103不能预订2014-05-15至2014-05-18的高级房间,因为它是通过预订100预订的。因此,如果103想要在该日期预订,他/她必须选择其他类型豪华或单人的房间。或者如果103想要预订高级房间,他/她必须在2014-05-18之后预订,这是可用的。

它的PHP代码是什么?

下面是我的预订表格中的PHP代码

    <?php 
    include 'core/init.php';

    protect_page();
    include 'includes/overall/header.php' ; 



    //if form is being submitted
    if(empty($_POST)=== false)
    {
        //to validate whether user enters smtg or not otherwise no point continue to do the next validation
        //create an array
        $required_fields = array ('user_id','full_name','passport','dor','dco');
        foreach($_POST as $key=>$value)
        {

            //if the key (value) in array of $required_fields is true which is empty
            if(empty($value) && in_array ($key, $required_fields) === true )
            {
                $errors[] = 'You must filled up all of the fields';
                //the validation can happen to more than 1 field
                break 1;
            }
        }

        if(empty($errors) === true)

        {
            if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $_POST['dor']))
            {
                echo 'Input your Date of Reservation correctly!';
                ?>
                Click <a href="reservation.php">here</a> to try again!
                <?php
                exit();
            }

            else if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $_POST['dco']))
            {
                echo 'Input your Check-out date correctly!';
                ?>
                Click <a href="reservation.php">here</a> to try again!
                <?php
                exit();
            }

            else if (!preg_match('/^[1-9][0-9]{0,2}$/', $_POST['num_of_rooms'])) 
            {
                echo 'Your Number of rooms must be filled!';
                ?>
                </br>
                Click <a href="reservation.php">here</a> to try again!
                <?php
                exit();
            }

            else if (strtotime($_POST['dor']) < time()) 
            {
                echo 'Date of reservation cannot be in the past!';
                ?>
                </br>
                Click <a href="reservation.php">here</a> to try again!
                <?php
                exit();
            }

            else if (strtotime($_POST['dco']) < strtotime($_POST['dor'])) 
            {
                echo 'Check-out date cannot be before Date of Reservation!';
                ?>
                </br>
                Click <a href="reservation.php">here</a> to try again!
                <?php
                exit();
            }

        }
    }
    //what does this line does is that to check whether success is in the end of the URL
    if(isset($_GET['success']) && empty($_GET['success']))
    {

        view_reservation();

    }
    else
    {//if there are no errors
        if (empty($_POST) === false && empty($errors) === true)
        {
            user_reservation();
        }

        //
        else if (empty($errors) === false)
        {
            echo output_errors($errors);
        }

    ?>
    <link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
    <script type="text/javascript" src="js/jsDatePick.min.1.3.js"></script>

    <script type="text/javascript">
        window.onload = function(){
            new JsDatePick({
                useMode:2,
                target:"dor",
                dateFormat:"%Y-%m-%d"

            });

            new JsDatePick({
                useMode:2,
                target:"dco",
                dateFormat:"%Y-%m-%d"

            });
        };


    </script>



    <h1>RESERVATION </h1>

    <form action="" method="post">
    <fieldset>
    <legend> 
        <font size="6">Please input your information correctly</font>
    </legend>

    <p>

    <form action="" method="post">
        <ul>
            <li>
                Full name*: <br>
                <input type="text" name="fullname">
            </li>
            <li>
                Contact No.: <br>
                <input type="text" name="contactno">
            </li>
            <li>
                IC/Passport*: <br>
                <input type="text" name="passport">
            </li>
            <li>
                Room Type*: <br>
                <select name="roomtype" id="roomtype">
                <option value="">Select</option>
                <option value="Single">Single (RM 100)</option>
                <option value="Superior">Superior (RM 200)</option>
                <option value="Deluxe">Deluxe (RM 300)</option>
              </select>
            </li>
            <li>
                Number of Rooms*:</li>
                <select name="num_of_rooms" id="num_of_rooms">
                <option value="">Select</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
              </select>
                <br>
            <li>
                Date of reservation*: <br>
                <input type="text" size="12" id= "dor" name="dor"/>
            </li>
            <li>
                Check-out Date*: <br>
                <input type="text" size="12" id= "dco" name="dco"/>
            </li>

              <input type="submit" value="Submit">
            <input type="reset" value="Clear" >
            <li>
            <br>
        </ul>
    </form>

    <?php
    }
    include 'includes/overall/footer.php' ;
    ?>

及以下是插入mysql的功能

function user_reservation ()
{
// Connecting, selecting database
$link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully<br>';
mysql_select_db('hotel_reservation3') or die('Could not select database');
//echo 'Selected system database - user successfully<br>';

//determine roomprice based on roomtype
if($_POST['roomtype'] == "Deluxe"){
    $roomprice = 300;
    }
else if($_POST['roomtype'] == "Superior"){
    $roomprice = 200;
    }
else{
    $roomprice = 100;
    }

// Store query in variable
$query = "INSERT INTO reservation (user_id,fullname,contactno,passport,roomtype,roomprice,num_of_rooms,dor,dco,bookingdate,length_of_stay,payment)
                    VALUES
                    (
                    '".$_SESSION['user_id']."',
                    '".mysql_real_escape_string($_POST['fullname'])."',
                    '".mysql_real_escape_string($_POST['contactno'])."',
                    '".mysql_real_escape_string($_POST['passport'])."',
                    '".mysql_real_escape_string($_POST['roomtype'])."',
                    $roomprice,
                    '".mysql_real_escape_string($_POST['num_of_rooms'])."',
                    '".mysql_real_escape_string($_POST['dor'])."',
                    '".mysql_real_escape_string($_POST['dco'])."',
                    NOW(),
                    DATEDIFF(dco,dor),
                    (roomprice*num_of_rooms*length_of_stay)
                    )";

// Performing SQL query
$result = mysql_query($query) 
or die('Query failed: ' . mysql_error());

//echo "Success inserting record!";

// Closing connection
mysql_close($link);

header("Location:reservation.php?success");
}

0 个答案:

没有答案