Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
- <style>
- table, th, td {
- border: 1px solid black;
- }
- body {
- background-color: #F0F0F0;
- }
- </style>
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "tharwa";
- // Create connection
- $conn = new mysqli($servername, $username, $password,$dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- echo " <div class='alert alert-primary' role='alert'>";
- echo "Connected successfully";
- echo " </div>"
- ?>
- </head>
- <body>
- <div class="container-fluid">
- <h4><b>Create Table</b></h4>
- <form action="welcome.php" method="post">
- <input type="submit" name="createtbl" class="btn btn-dark" value = "Create">
- </form>
- <br>
- <h4><b>Populate Table</b></h4>
- <form action="welcome.php" method="post">
- <input type="submit" name="popultbl" class="btn btn-dark" value = "Populate">
- </form>
- <br>
- <h4><b>Drop Table</b></h4>
- <form action="welcome.php" method="post">
- <input type="submit" name="droptbl" class="btn btn-danger" value = "Drop">
- </form>
- <br>
- <h4><b>Show Table</b></h4>
- <form action="welcome.php" method="post">
- <input type="submit" name="showtbl" class="btn btn-dark" value = "Show">
- </form>
- <br>
- <h3><p><b>Table: </b></p></h3>
- <?php
- if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['createtbl']))
- {
- createtable();
- }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['popultbl']))
- {
- populatetable();
- }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['droptbl']))
- {
- droptable();
- }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['showtbl']))
- {
- showtable();
- }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['bookroom']))
- {
- updatetable($_POST["bookroom"]);
- }
- function showtable() {
- $sql = "SELECT * FROM hotelrooms";
- global $conn;
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- echo "<table class='table table-striped'> <thead class='thead-dark'><tr><th>Room Number</th><th>Room Availability</th><th>Room Holder</th><th>Booking</th></tr></thead>";
- // output data of each row
- while($row = $result->fetch_assoc()) {
- echo "<tr><td>".$row["roomnmb"]."</td><td>".$row["roomavb"]."</td><td>".$row["roombooker"]."</td>";
- if($row["roomavb"] == "Available"){
- echo "<td><form action='welcome.php' method='post'> <div class='form-row'><div class='col'> <input type='text' class='form-control' placeholder ='First Name' name='booker'></div> <input type='hidden' name='bookroom' value='".$row["roomnmb"]."'><div class='col'><input type='submit' class='btn btn-success' value = 'Book'></div></div></form></td>";
- }else{
- echo "<td class='table-danger'></td>";
- }
- echo "</tr>";
- }
- echo "</table>";
- } else {
- echo "<div class='alert alert-danger' role='alert'>";
- echo "0 results";
- echo "</div>";
- }
- }
- function updatetable($roomnumber){
- $sql = "UPDATE hotelrooms
- SET roomavb = 'Not Available', roombooker='".$_POST["booker"]."' WHERE roomnmb = $roomnumber";
- global $conn;
- if ($conn->query($sql) === TRUE) {
- echo "<div class='alert alert-success' role='alert'> " ;
- echo "Room Booked Successfully";
- echo "</div>";
- } else {
- echo "<div class='alert alert-danger' role='alert'>";
- echo "Error Booking room: " . $conn->error;
- echo "</div>";
- }
- }
- function createtable() {
- // sql to create table
- $sql = "CREATE TABLE hotelrooms (
- roomnmb Int(10) NOT NULL PRIMARY KEY,
- roomavb Varchar(30) NOT NULL,
- roombooker Varchar (120)
- )";
- global $conn;
- if ($conn->query($sql) === TRUE) {
- echo "<div class='alert alert-success' role='alert'> " ;
- echo "Table hotelrooms created successfully";
- echo "</div>";
- } else {
- echo "<div class='alert alert-danger' role='alert'>";
- echo "Error creating table: " . $conn->error;
- echo "</div>";
- }
- }
- function droptable(){
- $sql = "DROP TABLE hotelrooms";
- global $conn;
- if ($conn->query($sql) === TRUE) {
- echo "<div class='alert alert-success' role='alert'> " ;
- echo "Table hotelrooms dropped successfully";
- echo "</div>";
- } else {
- echo "<div class='alert alert-danger' role='alert'>";
- echo "Error dropping table: " . $conn->error;
- echo "</div>" ;
- }
- }
- function populatetable () {
- $sql = "INSERT INTO hotelrooms (roomnmb, roomavb,roombooker)
- VALUES ('301', 'Available','');";
- $sql .= "INSERT INTO hotelrooms (roomnmb, roomavb,roombooker)
- VALUES ('302', 'Not Available','Ahmed');";
- $sql .= "INSERT INTO hotelrooms (roomnmb, roomavb,roombooker)
- VALUES ('303', 'Available','');";
- global $conn;
- if ($conn->multi_query($sql) === TRUE) {
- echo "<div class='alert alert-success' role='alert'> " ;
- echo "Table hotelrooms populated successfully";
- echo "</div>";
- } else {
- echo "<div class='alert alert-danger' role='alert'>";
- echo "Error populating table: " . $conn->error;
- echo "</div>";
- }
- }
- ?>
- </div>
- </body>
- <?php
- $conn->close();
- ?>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement