Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- table, th, td {
- border: 1px solid black;
- }
- </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 "Connected successfully";
- ?>
- </head>
- <body>
- <p><b>Create Table</b></p>
- <form action="welcome.php" method="post">
- <input type="submit" name="createtbl" value = "Create">
- </form>
- <p><b>Populate Table</b></p>
- <form action="welcome.php" method="post">
- <input type="submit" name="popultbl" value = "Populate">
- </form>
- <p><b>Drop Table</b></p>
- <form action="welcome.php" method="post">
- <input type="submit" name="droptbl" value = "Drop">
- </form>
- <p><b>Show Table</b></p>
- <form action="welcome.php" method="post">
- <input type="submit" name="showtbl" value = "Show">
- </form>
- <p>Table: </p>
- <?php
- $tblresults = "f ";
- 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();
- }
- function showtable() {
- $sql = "SELECT * FROM hotelrooms";
- global $conn;
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- echo "<table><tr><th>Room Number</th><th>Room Availability</th></tr>";
- // output data of each row
- while($row = $result->fetch_assoc()) {
- echo "<tr><td>".$row["roomnmb"]."</td><td>".$row["roomavb"]."</td></tr>";
- }
- echo "</table>";
- } else {
- echo "0 results";
- }
- }
- function createtable() {
- // sql to create table
- $sql = "CREATE TABLE hotelrooms (
- roomnmb Int(10) NOT NULL PRIMARY KEY,
- roomavb Varchar(30) NOT NULL
- )";
- global $conn;
- if ($conn->query($sql) === TRUE) {
- echo "Table hotelrooms created successfully";
- } else {
- echo "Error creating table: " . $conn->error;
- }
- }
- function droptable(){
- $sql = "DROP TABLE hotelrooms";
- global $conn;
- if ($conn->query($sql) === TRUE) {
- echo "Table hotelrooms dropped successfully";
- } else {
- echo "Error dropping table: " . $conn->error;
- }
- }
- function populatetable () {
- $sql = "INSERT INTO hotelrooms (roomnmb, roomavb)
- VALUES ('301', 'Available');";
- $sql .= "INSERT INTO hotelrooms (roomnmb, roomavb)
- VALUES ('302', 'Not Available');";
- $sql .= "INSERT INTO hotelrooms (roomnmb, roomavb)
- VALUES ('303', 'Available');";
- global $conn;
- if ($conn->multi_query($sql) === TRUE) {
- echo "Table hotelrooms populated successfully";
- } else {
- echo "Error populating table: " . $conn->error;
- }
- }
- ?>
- </body>
- <?php
- $conn->close();
- ?>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement