Advertisement
Guest User

Untitled

a guest
Aug 1st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.63 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  5. <style>
  6. table, th, td {
  7.     border: 1px solid black;
  8. }
  9. body {
  10.     background-color: #F0F0F0;
  11. }
  12. </style>
  13.  
  14. <?php
  15. $servername = "localhost";
  16. $username = "root";
  17. $password = "";
  18. $dbname = "tharwa";
  19.  
  20. // Create connection
  21. $conn = new mysqli($servername, $username, $password,$dbname);
  22.  
  23. // Check connection
  24. if ($conn->connect_error) {
  25.     die("Connection failed: " . $conn->connect_error);
  26. }
  27. echo " <div class='alert alert-primary' role='alert'>";
  28. echo "Connected successfully";
  29. echo " </div>"
  30.  
  31. ?>
  32. </head>
  33. <body>
  34.  
  35. <div class="container-fluid">
  36. <h4><b>Create Table</b></h4>
  37. <form action="welcome.php" method="post">
  38. <input type="submit" name="createtbl" class="btn btn-dark" value = "Create">
  39. </form>
  40. <br>
  41.  
  42. <h4><b>Populate Table</b></h4>
  43. <form action="welcome.php" method="post">
  44. <input type="submit" name="popultbl" class="btn btn-dark" value = "Populate">
  45. </form>
  46. <br>
  47.  
  48. <h4><b>Drop Table</b></h4>
  49. <form action="welcome.php" method="post">
  50. <input type="submit" name="droptbl" class="btn btn-danger" value = "Drop">
  51. </form>
  52. <br>
  53.  
  54. <h4><b>Show Table</b></h4>
  55. <form action="welcome.php" method="post">
  56. <input type="submit" name="showtbl" class="btn btn-dark" value = "Show">
  57. </form>
  58. <br>
  59.  
  60. <h3><p><b>Table: </b></p></h3>
  61.  
  62. <?php
  63. if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['createtbl']))
  64.     {
  65.         createtable();
  66.  
  67.     }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['popultbl']))
  68.     {
  69.         populatetable();
  70.  
  71.     }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['droptbl']))
  72.     {
  73.         droptable();
  74.        
  75.     }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['showtbl']))
  76.     {
  77.         showtable();
  78.  
  79.     }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['bookroom']))
  80.     {
  81.         updatetable($_POST["bookroom"]);
  82.     }
  83.    
  84. function showtable() {
  85.     $sql = "SELECT * FROM hotelrooms";
  86.     global $conn;
  87.     $result = $conn->query($sql);
  88.     if ($result->num_rows > 0) {
  89.         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>";
  90.         // output data of each row
  91.         while($row = $result->fetch_assoc()) {
  92.             echo "<tr><td>".$row["roomnmb"]."</td><td>".$row["roomavb"]."</td><td>".$row["roombooker"]."</td>";
  93.             if($row["roomavb"] == "Available"){
  94.                 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>";
  95.             }else{
  96.                 echo "<td class='table-danger'></td>";
  97.             }
  98.             echo "</tr>";
  99.         }
  100.         echo "</table>";
  101.     } else {
  102.         echo "<div class='alert alert-danger' role='alert'>";
  103.         echo "0 results";
  104.         echo "</div>";
  105.     }
  106. }
  107.  
  108. function updatetable($roomnumber){
  109.     $sql = "UPDATE  hotelrooms
  110.            SET roomavb = 'Not Available', roombooker='".$_POST["booker"]."' WHERE roomnmb = $roomnumber";
  111.         global $conn;
  112.         if ($conn->query($sql) === TRUE) {
  113.             echo "<div class='alert alert-success' role='alert'> " ;
  114.             echo "Room Booked Successfully";
  115.             echo "</div>";
  116.         } else {
  117.             echo "<div class='alert alert-danger' role='alert'>";
  118.             echo "Error Booking room: " . $conn->error;
  119.             echo "</div>";
  120.         }
  121. }
  122.  
  123. function createtable() {
  124.     // sql to create table
  125.      $sql = "CREATE TABLE hotelrooms (
  126.     roomnmb Int(10) NOT NULL PRIMARY KEY,
  127.     roomavb Varchar(30) NOT NULL,
  128.     roombooker Varchar (120)
  129.     )";
  130.      global $conn;
  131.      if ($conn->query($sql) === TRUE) {
  132.          echo "<div class='alert alert-success' role='alert'> " ;
  133.          echo "Table hotelrooms created successfully";
  134.          echo "</div>";
  135.      } else {
  136.          echo "<div class='alert alert-danger' role='alert'>";
  137.          echo "Error creating table: " . $conn->error;
  138.          echo "</div>";
  139.      }
  140.  }
  141.  function droptable(){
  142.      $sql = "DROP TABLE hotelrooms";
  143.      global $conn;
  144.      if ($conn->query($sql) === TRUE) {
  145.         echo "<div class='alert alert-success' role='alert'> " ;
  146.         echo "Table hotelrooms dropped successfully";
  147.         echo "</div>";
  148.     } else {
  149.         echo "<div class='alert alert-danger' role='alert'>";
  150.         echo "Error dropping table: " . $conn->error;
  151.         echo "</div>" ;
  152.     }
  153. }
  154.  
  155. function populatetable () {
  156.     $sql = "INSERT INTO hotelrooms (roomnmb, roomavb,roombooker)
  157.    VALUES ('301', 'Available','');";
  158.     $sql .= "INSERT INTO hotelrooms (roomnmb, roomavb,roombooker)
  159.    VALUES ('302', 'Not Available','Ahmed');";
  160.     $sql .= "INSERT INTO hotelrooms (roomnmb, roomavb,roombooker)
  161.    VALUES ('303', 'Available','');";
  162.  
  163.      global $conn;
  164.      if ($conn->multi_query($sql) === TRUE) {
  165.         echo "<div class='alert alert-success' role='alert'> " ;
  166.         echo "Table hotelrooms populated successfully";
  167.         echo "</div>";
  168.     } else {
  169.         echo "<div class='alert alert-danger' role='alert'>";
  170.         echo "Error populating table: " . $conn->error;
  171.         echo "</div>";
  172.     }
  173. }
  174. ?>
  175.  
  176. </div>
  177. </body>
  178.  
  179. <?php
  180. $conn->close();
  181. ?>
  182. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement