CastelShal

Web Prac 7

Sep 3rd, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.41 KB | None | 0 0
  1. index.php
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <style>
  8.         form {
  9.             font-size: 1.2rem;
  10.         }
  11.         form div{
  12.             margin: 10px;
  13.         }
  14.  
  15.         h1{
  16.             text-align: center;
  17.         }
  18.     </style>
  19.     <title>Form</title>
  20. </head>
  21. <body>
  22.     <form action="insert.php" method="post">
  23.         <h1>Employee ID</h1>
  24.         <fieldset>
  25.             <div>
  26.                 <label for="empid">Employee ID: </label>
  27.                 <input type="number" name="id" id="empid"/>
  28.             </div>
  29.             <div>
  30.                 <label for="empname">Employee Name:</label>
  31.                 <input type="text" name="name" id="empname"/>
  32.             </div>
  33.             <div>
  34.                 <label for="empdept">Employee Dept:</label>
  35.                 <input type="text" name="dept" id="empdept"/>
  36.             </div>
  37.             <div>
  38.                 <label for="emppos">Employee Position:</label>
  39.                 <input type="text" name="pos" id="emppos"/>
  40.             </div>
  41.             <div><input type="submit" value="Submit">&nbsp;<input type="reset" value="Reset"></div>
  42.         </fieldset>
  43.     </form>
  44. </body>
  45. </html>
  46.  
  47. insert.php
  48. <?php
  49. $hostname = "localhost";
  50. $username = "root";
  51. $password = "";
  52. $database = "urmum";
  53.  
  54. $conn = mysqli_connect($hostname, $username, $password, $database);
  55.  
  56. if(!$conn){
  57.     die("Connection failed: ".mysqli_connect_error());
  58. }
  59. else{
  60.     echo "Connection successful!";
  61. }
  62.  
  63. $id = $_POST["id"];
  64. $name = $_POST["name"];
  65. $dept = $_POST["dept"];
  66. $pos = $_POST["pos"];
  67.  
  68. $query = "INSERT INTO daemployees(empid, empdept, empname, emppos) VALUES ('$id','$dept','$name','$pos')";
  69. if( mysqli_query($conn, $query) ){
  70.     echo "<br/>Data entered successfully";
  71.     header("Location: display.php");
  72.     exit;
  73. }
  74. else{
  75.     echo "Even a donkey is more useful than you!!";
  76. }
  77.  
  78. mysqli_close($conn);
  79. ?>
  80.  
  81. display.php
  82. <!DOCTYPE html>
  83. <html lang="en">
  84. <head>
  85.     <meta charset="UTF-8">
  86.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  87.     <title>The Table</title>
  88. </head>
  89. <body>
  90.     <?php
  91.         $hostname = "localhost";
  92.         $username = "root";
  93.         $password = "";
  94.         $database = "urmum";
  95.  
  96.         $conn = mysqli_connect($hostname, $username, $password, $database);
  97.  
  98.         if(!$conn){
  99.             die("Connection failed: ".mysqli_connect_error());
  100.         }
  101.         else{
  102.             $query = "SELECT * from daemployees";
  103.             $res = mysqli_query($conn, $query);
  104.             if(mysqli_num_rows($res) > 0){
  105.                 echo "<table border='2' cellpadding='5' cellspacing='0'><tr>
  106.                    <th>ID</th>
  107.                    <th>Name</th>
  108.                    <th>Department</th>
  109.                    <th>Position</th>
  110.                </tr>";
  111.  
  112.                 while( $row = mysqli_fetch_assoc($res) ){
  113.                    echo "<tr>
  114.                    <td>" .$row['empid']. "</td>
  115.                    <td>" .$row['empname']. "</td>
  116.                    <td>" .$row['empdept']. "</td>
  117.                    <td>" .$row['emppos']. "</td>
  118.                    </tr>";
  119.                 }
  120.  
  121.                 echo "</table>";
  122.             }
  123.             else{
  124.                 echo "No Data. Get fucked.";
  125.             }
  126.         }
  127.         mysqli_close($conn);
  128.     ?>
  129. </body>
  130. </html>
Add Comment
Please, Sign In to add comment