Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <!Doctype HTML>
  2. <html>
  3.  
  4.     <head>
  5.         <title>Aufgabe 2</title>
  6.         <meta charset="UTF-8">
  7.         <link rel="stylesheet" type="text/css" href="index.css">
  8.     </head>
  9.  
  10.     <body>
  11.         <h1 style="text-decoration: underline">Mitarbeiter</h1>
  12.         <form method="post" action="" id="list" name="list">
  13.             <select name="list" id="list" size="15%" style="width: 40%;">
  14.                 <?php $c = new Main(); $c->Refresh(); ?>
  15.             </select>
  16.         </form>
  17.         <br>
  18.         <br>
  19.         <form method="post" action="">
  20.             <a class="inputbox">ID:</a><br> <input type="number" name="id" id="id"><br>
  21.             <a class="inputbox">Forename:</a><br> <input type="text" name="forename" id="forename"><br>
  22.             <a class="inputbox">Name:</a><br> <input type="text" name="name" id="name"><br>
  23.             <a class="inputbox">City:</a><br> <input type="text" name="city" id="city"><br><br>
  24.             <button type="submit" name="sent" id="sent" value="sent">Hinzufügen</button>
  25.         </form>
  26.     </body>
  27.  
  28. </html>
  29.  
  30. <?php
  31.  
  32. class Main
  33. {
  34.     public $servername = "localhost";
  35.     public $username = "admin";
  36.     public $password = "admin";
  37.     public $dbname = "aufgabe02";
  38.  
  39.     public $edit = false;
  40.  
  41.     function Refresh()
  42.     {
  43.         $con = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
  44.  
  45.         if (!$con) {
  46.             die("Connection Problem: " . $con->connect_error);
  47.         }
  48.  
  49.         $sql = "SELECT * FROM aufgabe02.employee";
  50.         $result = $con->query($sql);
  51.         if (!$result)
  52.         {
  53.             die("SQL Problem: " . $con->error);
  54.         }
  55.  
  56.         if ($result->num_rows > 0) {
  57.             while ($row = $result->fetch_assoc())
  58.             {
  59.                 $option = $row["id"] . " " . $row["forename"] . " " . $row["name"] . " " . $row["city"];
  60.                 echo "<option>" . $option . "</option>";
  61.             }
  62.         }
  63.  
  64.         $con->close();
  65.     }
  66.  
  67.     function AddUser($id, $forename, $name, $city)
  68.     {
  69.         $con = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
  70.  
  71.         if (!$con) {
  72.             die("Connection Problem: " . $con->connect_error);
  73.         }
  74.  
  75.         $sql = "INSERT INTO aufgabe02.employee (id, forename, name, city) VALUES ('" . $id . "', '" . $forename . "', '" . $name . "', '" . $city . "')";
  76.  
  77.         if (!$con->query($sql) === TRUE)
  78.         {
  79.             echo "Error: " . $sql . "<br>" . $con->error;
  80.         }
  81.  
  82.         $con->close();
  83.  
  84.         $this->edit = false;
  85.     }
  86.  
  87. }
  88.  
  89.  
  90. if(isset($_POST['sent']))
  91. {
  92.    $id = $_POST["id"];
  93.    $forename = $_POST["forename"];
  94.    $name = $_POST["name"];
  95.    $city = $_POST["city"];
  96.  
  97.    if(preg_match("/^[1-9]+$/", $id) && preg_match("/^[a-zA-Z]+$/", $forename) && preg_match("/^[a-zA-Z]+$/", $name) && preg_match("/^[a-zA-Z]+$/", $city))
  98.    {
  99.        $t = new Main();
  100.  
  101.        if($t->edit)
  102.        {
  103.            $t->AddUser("edit", "edit", "edit", "edit");
  104.        }
  105.        else
  106.        {
  107.            $t->AddUser($id, $forename, $name, $city);
  108.        }
  109.    }
  110.  
  111. }
  112.  
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement