Search for new ideas!

Beginners Hotel Management System in PHP

Project: Beginners Hotel Management System
Introduction: It is a simple management system containing two sections:
1.User: User can find out whether there is any room available at any particular date
User Module
2.Admin: Admin can manage, update and create new bookings
Admin Module

Difficulty: Beginner
Estimated Time: 30 minutes
Languages: PHP and Mysql
Tools Required: Xampp, Notepad++

Download


Step1. Database creation
  • Open PHPMyAdmin using http://localhost/phpmyadmin in XAMPP 
  • Create Database: HOTEL 
  • Create Table:
  • CREATE TABLE IF NOT EXISTS `rooms_info` (
      `booking_id` int(11) NOT NULL AUTO_INCREMENT,
      `room_no` varchar(55) NOT NULL,
      `check_in` date NOT NULL,
      `check_out` date NOT NULL,
      `no_of_rooms` int(11) NOT NULL,
      PRIMARY KEY (`booking_id`),
      KEY `room_no` (`room_no`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10;
    
  • Insert some dummy data for a start
  • INSERT INTO `rooms_info` (`booking_id`, `room_no`, `check_in`, `check_out`, `no_of_rooms`) VALUES
    (1, '1', '2013-01-02', '2013-01-03', 3),
    (2, '2', '2013-01-03', '2013-01-04', 3),
    (3, '3', '2013-01-01', '2013-01-04', 2),
    (4, '4,5', '2013-01-04', '2013-01-05', 2),
    (5, '2,3,4', '2013-01-05', '2013-01-25', 2),
    (6, '5,6,8', '2013-02-07', '2013-02-28', 3),
    (7, '4,5,6', '2013-10-05', '2013-11-09', 3),
    (8, '2,3,4', '2013-03-05', '2013-03-08', 3),
    (9, '5,6,7', '2013-01-05', '2013-01-08', 3);
    
Step2. User Interface

Create index.php file where user can submit reservation form as well as gets update about reservation status
index.php
<?php
include('config.php');
if (isset($_GET['submit'])) {
    /* Getting data from form submitted at index.php page */
    $user_checkin_date = $_GET['checkin_date'];
    $user_checkin_month = $_GET['checkin_month'];
    $user_checkin_year = $_GET['checkin_year'];
    $user_checkout_date = $_GET['checkout_date'];
    $user_checkout_month = $_GET['checkout_month'];
    $user_checkout_year = $_GET['checkout_year'];
    $persons = $_GET['persons']; // this variable will be used when we scale up this application
    $rooms = $_GET['rooms'];
    /* End of collecting data */

//Check for any invalid submissions, you should apply as much validation as possible
    if ($user_checkin_month == 2 || $user_checkin_month == 4 || $user_checkin_month == 6 || $user_checkin_month == 9 || $user_checkin_month == 11 || $user_checkout_month == 2 || $user_checkout_month == 4 || $user_checkout_month == 6 || $user_checkout_month == 9 || $user_checkout_month == 11) {
        if ($user_checkin_date >= 31 || $user_checkout_date >= 31) {
            $error = 1;
            $error_type[] = "For month $user_checkin_month 31st is invalid Date";
        } else if ($user_checkin_month == 2 || $user_checkout_month == 2) {
            if ($user_checkin_date >= 28 || $user_checkout_date >= 28) {
                $error = 1;
                $error_type[] = 'Feb Date Error';
            }
        } else {
            $error = 0;
        }
    } else {
        $error = 0;
    }
    if ($user_checkin_date >= $user_checkout_date) {

        $error = 1;
        $error_type[] = "Check out date can't be equal or lesser than check in date , please try again!!";
    }

// converting date into yyyy-mm-dd format
    $user_checkin_date = "$user_checkin_year" . "-" . "$user_checkin_month" . "-" . "$user_checkin_date";
    $user_checkout_date = "$user_checkout_year" . "-" . "$user_checkout_month" . "-" . "$user_checkout_date";
// info convert into unix timestamp
    $user_checkin_date = strtotime($user_checkin_date);
    $user_checkout_date = strtotime($user_checkout_date);
    global $total_rooms_available;
    $total_rooms_available = 10;
    while ($row = mysql_fetch_array($result)) {
        $check_in = $row['check_in'];
        $check_out = $row['check_out'];
        $no_of_rooms = $row['no_of_rooms'];
        $check_in = strtotime($check_in);
        $check_out = strtotime($check_out);
// test for availability with check in and check out date
        if ($check_in <= $user_checkin_date && $user_checkin_date < $check_out) {

            $room_available = 0;
            $total_rooms_available = $total_rooms_available - $no_of_rooms;
        } // end of if check in loop
        else {
            if ($check_in < $user_checkout_date && $user_checkout_date <= $check_out) {
                $room_available = 0;
                $total_rooms_available = $total_rooms_available - $no_of_rooms;
            } // end of if checkout loop
            else {
                $room_available = 1;
            } // end of else check out loop
        }
    }
}// end of if submit button has pressed  
?>  
<h3>Reservation:</h3>
<form action="index.php" id="reservation-form">
    <fieldset>
        <div class="field"><label>Check In:</label><select class="select1" name="checkin_date"><option>01</option><option>02</option><option>03</option><option>04</option><option>05</option><option>06</option><option>07</option><option>08</option><option>09</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option><option>26</option><option>27</option><option>28</option><option>29</option><option>30</option><option>31</option></select><select class="select1" name="checkin_month"><option>01</option><option>02</option><option>03</option><option>04</option><option>05</option><option>06</option><option>07</option><option>08</option><option>09</option><option>10</option><option>11</option><option>12</option></select><select class="select2" name="checkin_year"><option>2013</option><option>2014</option><option>2015</option><option>2016</option></select></div>
        <div class="field"><label>Check Out:</label><select class="select1" name="checkout_date"><option>01</option><option>02</option><option>03</option><option>04</option><option>05</option><option>06</option><option>07</option><option>08</option><option>09</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option><option>26</option><option>27</option><option>28</option><option>29</option><option>30</option><option>31</option></select><select class="select1" name="checkout_month"><option>01</option><option>02</option><option>03</option><option>04</option><option>05</option><option>06</option><option>07</option><option>08</option><option>09</option><option>10</option><option>11</option><option>12</option></select><select class="select2" name="checkout_year"><option>2013</option><option>2014</option><option>2015</option><option>2016</option></select></div>
        <div class="field"><label>Persons:</label> <select class="select1" name="persons"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select> </div>
        <div class="field"><label>Rooms:</label><select class="select1" name="rooms"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select></div>
        <div class="button"><span><span><input type="submit" name="submit" value="Check Availability" /></span></span></div>
    </fieldset>
</form>
<?php if (isset($_GET['submit'])) { ?>    

    <h2>Reservation Update!</h2>
    <?php
    if ($error) {
        foreach ($error_type as $value) {
            echo"$value<br />";
        }
    } else {
        if ($rooms < $room_available && $room_available != 0) {
            echo "Only $room_available room is available instead of $rooms rooms required by you";
        } else {
            echo "$total_rooms_available rooms are available , call 0000000(hotel number) to book now";
        }
    }// end of else about check in date lesser than check out date
} // end of if isset get submit loop
?>

 Step3. Admin Section 
In admin section, we will need mainly three files and one class, first admin/index.php and second admin/login_success.php third admin/Logout.php and fourth one is third party class tc_calendar(optional) which helps us to show javascript calendar.

  • Create new folder Admin
  • Create index file and paste php source shown below
  • // user name and password for admin, preferred way is to create database user table inside database
    $username = "hotel";
    $password = "123456";
    if (isset($_POST['Submit'])) {
    // username and password sent from form 
        $myusername = $_POST['myusername'];
        $mypassword = $_POST['mypassword'];
    
    // To protect MySQL injection 
        $myusername = stripslashes($myusername);
        $mypassword = stripslashes($mypassword);
        $myusername = mysql_real_escape_string($myusername);
        $mypassword = mysql_real_escape_string($mypassword);
    } // end of isset post submit
    ?>
    
    
    <p><br /><br />Welcome to Administrator Page of <a href="http://hotel.url">Hotel!</a></p>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
        <form name="form1" method="post" action="./">
            <td>
                <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                    <tr>
                        <td colspan="3"><strong>Member Login </strong></td>
                    </tr>
                    <tr>
                        <td width="78">Username</td>
                        <td width="6">:</td>
                        <td width="294"><input name="myusername" type="text" id="myusername"></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td>:</td>
                        <td><input name="mypassword" type="password" id="mypassword"></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td><input type="submit" name="Submit" value="Login"></td>
                    </tr>
                    <?php
                    if (isset($_POST['Submit'])) {
                        echo"<tr><td COLSPAN=3>";
    // If result matched $myusername and $mypassword, table row must be 1 row
                        if ($myusername == $username && $mypassword == $password) {
    
    // Register $myusername, $mypassword and redirect to file "login_success.php"
                            $_SESSION['username'] = $username;
    
                            header("location:login_success.php");
                        } else {
                            echo "<p style=\"background-color:#FFE0FF\">Wrong Username or Password ! <a href=\"../Admin\">Login Again</a></p>";
                        }
                        echo"</td></tr>";
                    }
                    ?>
                </table>
            </td>
        </form>
    </tr>
    </table>
  • login_sucess.php
  • <?php
    ############### Code
    // Check if session is not registered, redirect back to main page. 
    // Put this code in first line of web page. 
    
    session_start();
    if (!isset($_SESSION["username"])) {
        echo"Invalid username and password<br />";
        echo"Please try again";
        echo"<a href=\"./index.php\">Click Here</a>";
        header("location:index.php");
    }
    include("../config.php");
    ?>
    <script language="javascript" src="calendar.js"></script>  
    
    <h1> Admin Page </h1>
    <p><br /><br />Welcome to Administrator Page of <a href="http://hotel.url">Hotel!</a></p>
    <?php
    if (isset($_SESSION["username"])) {
        echo"<a href=\"./Logout.php\">Log Out!</a>";
    }
    
    if (isset($_POST['submit'])) {
    
        $booking_1 = $_POST['booking1'];
        $room_no_1 = $_POST['room_no1'];
        $check_in_1 = $_POST['check_in1'];
        $check_out_1 = $_POST['check_out1'];
        $no_of_rooms_1 = $_POST['no_of_rooms1'];
        $result = mysql_query("UPDATE  `hotel`.`rooms_info` SET  `room_no` =  '$room_no_1',
    `check_in` =  '$check_in_1',
    `check_out` =  '$check_out_1',
    `no_of_rooms` =  '$no_of_rooms_1' WHERE  `rooms_info`.`booking_id` = $booking_1;")
                or die(mysql_error());
    
    
        $booking_2 = $_POST['booking2'];
        $room_no_2 = $_POST['room_no2'];
        $check_in_2 = $_POST['check_in2'];
        $check_out_2 = $_POST['check_out2'];
        $no_of_rooms_2 = $_POST['no_of_rooms2'];
        $result = mysql_query("UPDATE  `hotel`.`rooms_info` SET  `room_no` =  '$room_no_2',
    `check_in` =  '$check_in_2',
    `check_out` =  '$check_out_2',
    `no_of_rooms` =  '$no_of_rooms_2' WHERE  `rooms_info`.`booking_id` = $booking_2;")
                or die(mysql_error());
    
    
        $booking_3 = $_POST['booking3'];
        $room_no_3 = $_POST['room_no3'];
        $check_in_3 = $_POST['check_in3'];
        $check_out_3 = $_POST['check_out3'];
        $no_of_rooms_3 = $_POST['no_of_rooms3'];
        $result = mysql_query("UPDATE  `hotel`.`rooms_info` SET  `room_no` =  '$room_no_3',
    `check_in` =  '$check_in_3',
    `check_out` =  '$check_out_3',
    `no_of_rooms` =  '$no_of_rooms_3' WHERE  `rooms_info`.`booking_id` = $booking_3;")
                or die(mysql_error());
    
    
        $booking_4 = $_POST['booking4'];
        $room_no_4 = $_POST['room_no4'];
        $check_in_4 = $_POST['check_in4'];
        $check_out_4 = $_POST['check_out4'];
        $no_of_rooms_4 = $_POST['no_of_rooms4'];
        $result = mysql_query("UPDATE  `hotel`.`rooms_info` SET  `room_no` =  '$room_no_4',
    `check_in` =  '$check_in_4',
    `check_out` =  '$check_out_4',
    `no_of_rooms` =  '$no_of_rooms_4' WHERE  `rooms_info`.`booking_id` = $booking_4;")
                or die(mysql_error());
    
    
        $booking_5 = $_POST['booking5'];
        $room_no_5 = $_POST['room_no5'];
        $check_in_5 = $_POST['check_in5'];
        $check_out_5 = $_POST['check_out5'];
        $no_of_rooms_5 = $_POST['no_of_rooms5'];
        $result = mysql_query("UPDATE  `hotel`.`rooms_info` SET  `room_no` =  '$room_no_5',
    `check_in` =  '$check_in_5',
    `check_out` =  '$check_out_5',
    `no_of_rooms` =  '$no_of_rooms_5' WHERE  `rooms_info`.`booking_id` = $booking_5;")
                or die(mysql_error());
    
    
        $booking_6 = $_POST['booking6'];
        $room_no_6 = $_POST['room_no6'];
        $check_in_6 = isset($_REQUEST["date6"]) ? $_REQUEST["date6"] : "";
        $check_out_6 = isset($_REQUEST["date7"]) ? $_REQUEST["date7"] : "";
        $no_of_rooms_6 = $_POST['no_of_rooms6'];
        if ($room_no_6 != '') {
            $result = mysql_query("INSERT INTO `hotel`.`rooms_info` (`booking_id`, `room_no`, `check_in`, `check_out`, `no_of_rooms`) VALUES ('$booking_6', '$room_no_6', '$check_in_6', '$check_out_6', '$no_of_rooms_6');")
                    or die(mysql_error());
        }
        echo "<p style=\"background-color:#E0FFFF;\">Saved Successfully, View changes on Site <a href=\"../\" target=\"_blank\">Click Here</a></p>";
    }
    
    $result = mysql_query("SELECT * 
    FROM  `rooms_info` 
    ORDER BY  `rooms_info`.`booking_id` DESC 
    LIMIT 0 , 5")
            or die(mysql_error());
    
    while ($data = mysql_fetch_array($result)) {
        $booking_id[] = $data['booking_id'];
        $room_no[] = $data['room_no'];
        $check_in[] = $data['check_in'];
        $check_out[] = $data['check_out'];
        $no_of_rooms[] = $data['no_of_rooms'];
    }
    
    
    $new_booking_id = $booking_id[0] + 1;
    ?>
    <table border="1">
        <tr><th>Booking Details</th><th></th><th>New Booking</th><th>Booked Rooms</th></tr>
        <tr><td>
                <table>
                    <form id="form1" name="form1" method="post" action="">
    <?php
    //get class into the page
    require_once('./classes/tc_calendar.php');
    
    //instantiate class and set properties
    $myCalendar = new tc_calendar("date6", true);
    $myCalendar->setIcon("./images/iconCalendar.gif");
    $myCalendar->setDate(2, 1, 2013);
    $myCalendar7 = new tc_calendar("date7", true);
    $myCalendar7->setIcon("./images/iconCalendar.gif");
    $myCalendar7->setDate(3, 1, 2013);
    ?>
                        <tr><td> <label>Booking ID</label></td><td> <input name="booking1" type="text" size="32" maxlength="40" value="<?php echo $booking_id[0]; ?>"  /></td></tr>
                        <tr><td><label>Room No</label></td><td> <input name="room_no1" type="text" size="32" maxlength="40" value="<?php echo $room_no[0]; ?>" /></td></tr>
                        <tr><td><label>Check In</label> </td><td><input name="check_in1" type="text" size="32" maxlength="40" value="<?php echo $check_in[0]; ?>" /></td></tr>  
                        <tr><td><label>Check Out</label> </td><td><input name="check_out1" type="text" size="32" maxlength="40" value="<?php echo $check_out[0]; ?>" /></td></tr>  
                        <tr><td><label>No of Rooms</label> </td><td><input name="no_of_rooms1" type="text" size="32" maxlength="40" value="<?php echo $no_of_rooms[0]; ?>" /></td></tr><br />  
    
    
    
                </table>
                <table>
    
    
                    <tr><td> <label>Booking ID</label></td><td> <input name="booking2" type="text" size="32" maxlength="40" value="<?php echo $booking_id[1]; ?>"  /></td></tr>
                    <tr><td><label>Room No</label></td><td> <input name="room_no2" type="text" size="32" maxlength="40" value="<?php echo $room_no[1]; ?>" /></td></tr>
                    <tr><td><label>Check In</label> </td><td><input name="check_in2" type="text" size="32" maxlength="40" value="<?php echo $check_in[1]; ?>" /></td></tr>  
                    <tr><td><label>Check Out</label> </td><td><input name="check_out2" type="text" size="32" maxlength="40" value="<?php echo $check_out[1]; ?>" /></td></tr>
                    <tr><td><label>No of Rooms</label> </td><td><input name="no_of_rooms2" type="text" size="32" maxlength="40" value="<?php echo $no_of_rooms[1]; ?>" /></td></tr><br />
    
    
                </table>
                <table>
    
    
                    <tr><td> <label>Booking ID</label></td><td> <input name="booking3" type="text" size="32" maxlength="40" value="<?php echo $booking_id[2]; ?>"  /></td></tr>
                    <tr><td><label>Room No</label></td><td> <input name="room_no3" type="text" size="32" maxlength="40" value="<?php echo $room_no[2]; ?>" /></td></tr>
                    <tr><td><label>Check In</label> </td><td><input name="check_in3" type="text" size="32" maxlength="40" value="<?php echo $check_in[2]; ?>" /></td></tr>  
                    <tr><td><label>Check Out</label> </td><td><input name="check_out3" type="text" size="32" maxlength="40" value="<?php echo $check_out[2]; ?>" /></td></tr>
                    <tr><td><label>No of Rooms</label> </td><td><input name="no_of_rooms3" type="text" size="32" maxlength="40" value="<?php echo $no_of_rooms[2]; ?>" /></td></tr><br />  
    
    
                </table>
                <table>
    
    
                    <tr><td> <label>Booking ID</label></td><td> <input name="booking4" type="text" size="32" maxlength="40" value="<?php echo $booking_id[3]; ?>"  /></td></tr>
                    <tr><td><label>Room No</label></td><td> <input name="room_no4" type="text" size="32" maxlength="40" value="<?php echo $room_no[3]; ?>" /></td></tr>
                    <tr><td><label>Check In</label> </td><td><input name="check_in4" type="text" size="32" maxlength="40" value="<?php echo $check_in[3]; ?>" /></td></tr>  
                    <tr><td><label>Check Out</label> </td><td><input name="check_out4" type="text" size="32" maxlength="40" value="<?php echo $check_out[3]; ?>" /></td></tr>
                    <tr><td><label>No of Rooms</label> </td><td><input name="no_of_rooms4" type="text" size="32" maxlength="40" value="<?php echo $no_of_rooms[3]; ?>" /></td></tr>
    
    
    
                </table>
                <table>
    
    
                    <tr><td> <label>Booking ID</label></td><td> <input name="booking5" type="text" size="32" maxlength="40" value="<?php echo $booking_id[4]; ?>"  /></td></tr>
                    <tr><td><label>Room No</label></td><td> <input name="room_no5" type="text" size="32" maxlength="40" value="<?php echo $room_no[4]; ?>" /></td></tr>
                    <tr><td><label>Check In</label> </td><td><input name="check_in5" type="text" size="32" maxlength="40" value="<?php echo $check_in[4]; ?>" /></td></tr>  
                    <tr><td><label>Check Out</label> </td><td><input name="check_out5" type="text" size="32" maxlength="40" value="<?php echo $check_out[4]; ?>" /></td></tr>
                    <tr><td><label>No of Rooms</label> </td><td><input name="no_of_rooms5" type="text" size="32" maxlength="40" value="<?php echo $no_of_rooms[4]; ?>" /></td></tr><br /> 
    
    
                </table>
            </td>
            <td>&nbsp; &nbsp;</td>
            <td>
    
                <table>
    
    
                    <tr><td> <label>Booking ID</label></td><td> <input name="booking6" type="text" size="32" maxlength="40" value="<?php echo $new_booking_id; ?>"  /></td></tr>
                    <tr><td><label>Room No</label></td><td> <input name="room_no6" type="text" size="32" maxlength="40" value="" /></td></tr>
                    <tr><td><label>Check In</label> </td><td><?php //output the calendar
                        $myCalendar->writeScript();
    ?></td></tr>  
                    <tr><td><label>Check Out</label> </td><td><?php //output the calendar
                        $myCalendar7->writeScript();
    ?></td></tr>  
                    <tr><td><label>No of Rooms</label> </td><td><input name="no_of_rooms6" type="text" size="32" maxlength="40" value="" /></td></tr><br />
                </table>
            </td>
            <td>
    <?php
    $result = mysql_query("SELECT * 
    FROM  `rooms_info` 
    ORDER BY  `rooms_info`.`booking_id` DESC")
            or die(mysql_error());
    
    global $total_rooms_available;
    $total_rooms_available = 10;
    $booked_room = array();
    $today_date = date("Y-m-d");
    $today_date = strtotime($today_date);
    $tommorow_date = date("Y-m-d", strtotime("+1 day"));
    $tommorow_date = strtotime($tommorow_date);
    while ($row = mysql_fetch_array($result)) {
        $check_in = $row['check_in'];
        $check_out = $row['check_out'];
        $room_no = $row['room_no'];
        $no_of_rooms = $row['no_of_rooms'];
        $check_in = strtotime($check_in);
        $check_out = strtotime($check_out);
    // test for availability with check in and check out date
        if ($check_in <= $today_date && $today_date < $check_out) {
    
            $room_available = 0;
            $total_rooms_available = $total_rooms_available - $no_of_rooms;
            $booked_room[] = $room_no;
        } // end of if check in loop
        else {
            if ($check_in < $tommorow_date && $tommorow_date <= $check_out) {
                $room_available = 0;
                $total_rooms_available = $total_rooms_available - $no_of_rooms;
                $booked_room[] = $room_no;
            } // end of if checkout loop
            else {
                $room_available = 1;
            } // end of else check out loop
        }
    }
    echo "Available Rooms: $total_rooms_available";
    echo "<br />Booked Room No. <br />";
    foreach ($booked_room as $value) {
        echo "<br />$value<br />";
    }
    ?>  
            </td>
        </tr>
        <tr><td><input name="submit" type="submit" value="Save" /> </td></tr>
    </form>
    </table>
    
    </div>  
    </body>
    </html>
  • Logout.php
  • <?php
    
    // Put this code in first line of web page. 
    session_start();
    session_destroy();
    header("location:index.php");
    ?>
In this tutorial we have covered basics of the hotel management system.You can extend it as much as you want. I hope it helped you in understanding the logic behind these types of systems.

No comments :