Advertisement
Guest User

Untitled

a guest
Aug 1st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.14 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. table, th, td {
  6.     border: 1px solid black;
  7. }
  8. </style>
  9.  
  10. <?php
  11. $servername = "localhost";
  12. $username = "root";
  13. $password = "";
  14. $dbname = "tharwa";
  15.  
  16. // Create connection
  17. $conn = new mysqli($servername, $username, $password,$dbname);
  18.  
  19. // Check connection
  20. if ($conn->connect_error) {
  21.     die("Connection failed: " . $conn->connect_error);
  22. }
  23. echo "Connected successfully";
  24.  
  25. ?>
  26. </head>
  27. <body>
  28.  
  29. <p><b>Create Table</b></p>
  30. <form action="welcome.php" method="post">
  31. <input type="submit" name="createtbl" value = "Create">
  32. </form>
  33.  
  34. <p><b>Populate Table</b></p>
  35. <form action="welcome.php" method="post">
  36. <input type="submit" name="popultbl" value = "Populate">
  37. </form>
  38.  
  39. <p><b>Drop Table</b></p>
  40. <form action="welcome.php" method="post">
  41. <input type="submit" name="droptbl" value = "Drop">
  42. </form>
  43.  
  44. <p><b>Show Table</b></p>
  45. <form action="welcome.php" method="post">
  46. <input type="submit" name="showtbl" value = "Show">
  47. </form>
  48.  
  49. <p>Table: </p>
  50.  
  51. <?php
  52. $tblresults = "f ";
  53. if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['createtbl']))
  54.     {
  55.         createtable();
  56.  
  57.     }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['popultbl']))
  58.     {
  59.         populatetable();
  60.  
  61.     }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['droptbl']))
  62.     {
  63.         droptable();
  64.        
  65.     }elseif($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['showtbl']))
  66.     {
  67.         showtable();
  68.     }
  69.    
  70. function showtable() {
  71.     $sql = "SELECT * FROM hotelrooms";
  72.     global $conn;
  73.     $result = $conn->query($sql);
  74.     if ($result->num_rows > 0) {
  75.         echo "<table><tr><th>Room Number</th><th>Room Availability</th></tr>";
  76.         // output data of each row
  77.         while($row = $result->fetch_assoc()) {
  78.             echo "<tr><td>".$row["roomnmb"]."</td><td>".$row["roomavb"]."</td></tr>";
  79.         }
  80.         echo "</table>";
  81.     } else {
  82.         echo "0 results";
  83.     }
  84. }
  85.  
  86. function createtable() {
  87.     // sql to create table
  88.      $sql = "CREATE TABLE hotelrooms (
  89.     roomnmb Int(10) NOT NULL PRIMARY KEY,
  90.     roomavb Varchar(30) NOT NULL
  91.     )";
  92.      global $conn;
  93.      if ($conn->query($sql) === TRUE) {
  94.          echo "Table hotelrooms created successfully";
  95.      } else {
  96.          echo "Error creating table: " . $conn->error;
  97.      }
  98.  }
  99.  function droptable(){
  100.      $sql = "DROP TABLE hotelrooms";
  101.      global $conn;
  102.      if ($conn->query($sql) === TRUE) {
  103.         echo "Table hotelrooms dropped successfully";
  104.     } else {
  105.         echo "Error dropping table: " . $conn->error;
  106.     }
  107. }
  108.  
  109. function populatetable () {
  110.     $sql = "INSERT INTO hotelrooms (roomnmb, roomavb)
  111.    VALUES ('301', 'Available');";
  112.     $sql .= "INSERT INTO hotelrooms (roomnmb, roomavb)
  113.    VALUES ('302', 'Not Available');";
  114.     $sql .= "INSERT INTO hotelrooms (roomnmb, roomavb)
  115.    VALUES ('303', 'Available');";
  116.  
  117.      global $conn;
  118.      if ($conn->multi_query($sql) === TRUE) {
  119.         echo "Table hotelrooms populated successfully";
  120.     } else {
  121.         echo "Error populating table: " . $conn->error;
  122.     }
  123. }
  124. ?>
  125.  
  126. </body>
  127.  
  128. <?php
  129. $conn->close();
  130. ?>
  131. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement