Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 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="index.php">
  20.             <a class="inputbox">ID:</a><br> <input type="number" name="id" id="id"><br>
  21.             <a class="inputbox" name="forename">Forename:</a><br> <input type="text" name="forename" id="forename"><br>
  22.             <a class="inputbox" name="name">Name:</a><br> <input type="text" name="name" id="name"><br>
  23.             <a class="inputbox" name="city">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.         {
  47.             die("Connection Problem: ". $con->connect_error);
  48.         }
  49.  
  50.         $sql = "SELECT * FROM aufgabe02.employee";
  51.         $result = $con->query($sql);
  52.         if(!$result)
  53.         {
  54.             die("SQL Problem: ". $con->error);
  55.         }
  56.  
  57.         if ($result->num_rows > 0)
  58.         {
  59.             while ($row = $result->fetch_assoc())
  60.             {
  61.                 $option = $row["id"] . " " . $row["forename"] . " " . $row["name"] . " " . $row["city"];
  62.                 echo "<option>" . $option . "</option>";
  63.             }
  64.         }
  65.  
  66.         $con->close();
  67.     }
  68.  
  69.     function AddUser($id, $forename, $name, $city)
  70.     {
  71.         $con = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
  72.  
  73.         if(!$con)
  74.         {
  75.             die("Connection Problem: ". $con->connect_error);
  76.         }
  77.  
  78.         $sql = "INSERT INTO aufgabe02.employee (id, forename, name, city) VALUES ('".$id."', '".$forename."', '".$name."', '".$city."')";
  79.  
  80.         if (!$con->query($sql) === TRUE)
  81.         {
  82.             echo "Error: " . $sql . "<br>" . $con->error;
  83.         }
  84.  
  85.         $con->close();
  86.  
  87.         $this->edit = false;
  88.     }
  89. }
  90.  
  91. if(isset($_POST['sent']) && !empty($_POST["sent"]))
  92. {
  93.    $id = $_POST["id"];
  94.    $forename = $_POST["forename"];
  95.    $name = $_POST["name"];
  96.    $city = $_POST["city"];
  97.  
  98.    if(preg_match("/^[1-9]+$/", $id) && preg_match("/^[a-zA-Z]+$/", $forename) && preg_match("/^[a-zA-Z]+$/", $name) && preg_match("/^[a-zA-Z]+$/", $city))
  99.    {
  100.        $t = new Main();
  101.  
  102.        if($t->edit)
  103.        {
  104.            $t->AddUser("edit", "edit", "edit", "edit");
  105.        }
  106.        else
  107.        {
  108.            $t->AddUser($id, $forename, $name, $city);
  109.        }
  110.    }
  111.  
  112. }
  113.  
  114.  
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement