From 4fa83b7f8bf254930a9921dad10989ff836923a0 Mon Sep 17 00:00:00 2001
From: Peter Gerwinski <peter.gerwinski@hs-bochum.de>
Date: Wed, 13 Dec 2023 21:50:44 +0100
Subject: [PATCH] PHP-Beispiele 7.12.2023

---
 20231207/index-01.html  |  8 +++++++
 20231207/index-02.html  |  9 ++++++++
 20231207/school-01.php  | 45 +++++++++++++++++++++++++++++++++++++++
 20231207/school-02.php  | 47 +++++++++++++++++++++++++++++++++++++++++
 20231207/school.html    | 10 +++++++++
 20231207/tiere-01.php   | 32 ++++++++++++++++++++++++++++
 20231207/tiere-02.php   | 34 +++++++++++++++++++++++++++++
 20231207/tiere-03.php   | 30 ++++++++++++++++++++++++++
 20231207/tiere-04.php   | 29 +++++++++++++++++++++++++
 20231207/tiere-05.php   | 30 ++++++++++++++++++++++++++
 20231207/welcome-01.php |  5 +++++
 20231207/welcome-02.php |  6 ++++++
 12 files changed, 285 insertions(+)
 create mode 100644 20231207/index-01.html
 create mode 100644 20231207/index-02.html
 create mode 100644 20231207/school-01.php
 create mode 100644 20231207/school-02.php
 create mode 100644 20231207/school.html
 create mode 100644 20231207/tiere-01.php
 create mode 100644 20231207/tiere-02.php
 create mode 100644 20231207/tiere-03.php
 create mode 100644 20231207/tiere-04.php
 create mode 100644 20231207/tiere-05.php
 create mode 100644 20231207/welcome-01.php
 create mode 100644 20231207/welcome-02.php

diff --git a/20231207/index-01.html b/20231207/index-01.html
new file mode 100644
index 0000000..f6702da
--- /dev/null
+++ b/20231207/index-01.html
@@ -0,0 +1,8 @@
+<html>
+  <body>
+    <form action="welcome-01.php" method="post">
+      Your name: <input type="text" name="name"><br>
+      <input type="submit">
+    </form>
+  </body>
+</html> 
diff --git a/20231207/index-02.html b/20231207/index-02.html
new file mode 100644
index 0000000..f03af4b
--- /dev/null
+++ b/20231207/index-02.html
@@ -0,0 +1,9 @@
+<html>
+  <body>
+    <form action="welcome-02.php" method="post">
+      Your name: <input type="text" name="name"><br>
+      <input type="hidden" name="answer" value="42">
+      <input type="submit">
+    </form>
+  </body>
+</html> 
diff --git a/20231207/school-01.php b/20231207/school-01.php
new file mode 100644
index 0000000..fb680c4
--- /dev/null
+++ b/20231207/school-01.php
@@ -0,0 +1,45 @@
+<html>
+  <body>
+    <?php
+      try
+        {
+          $db = new PDO ('pgsql: host = localhost; dbname = testdb; user = dbs; password = 1234');
+        }
+      catch (\PDOException $e)
+        {
+          echo '<p>';
+          echo $e->getMessage();
+          echo '</p>';
+          echo '<p>';
+          exit ('cannot connect to database');
+          echo '</p>';
+        }
+      $db->exec ('INSERT INTO students (first_name, family_name) VALUES ( '
+                 .$_POST["first_name"].', '.$_POST["familty_name"]
+                 .' );');
+      $stmt = $db->query ('SELECT * FROM student;');
+      echo '<table><tr>';
+      while ($row = $stmt->fetch())
+        {
+          echo '<tr><td>';
+          echo $row['id'];
+          echo '</td><td>';
+          echo $row['first_name'];
+          echo '</td><td>';
+          echo $row['family_name'];
+          echo '</td></tr>';
+        }
+      echo '</tr></table>';
+    ?>
+  </body>
+</html> 
+
+<!-- Aus den Log-Dateien des Web-Servers:
+
+  [Thu Dec 07 12:50:06.952167 2023] [php:error] [pid 163970] [client ::1:52394]
+  PHP Fatal error:  Uncaught PDOException: SQLSTATE[42601]: Syntax error: 7
+  FEHLER:  Syntaxfehler bei \xc2\xbb)\xc2\xab\nLINE 1: ... INTO students
+  (first_name, family_name) VALUES ( Peter,  );\n
+  ^ in /var/www/dbs/school-01.php:17\nStack trace:\n#0
+  /var/www/dbs/school-01.php(17): PDO->exec()\n#1 {main}\n  thrown in
+  /var/www/dbs/school-01.php on line 17
diff --git a/20231207/school-02.php b/20231207/school-02.php
new file mode 100644
index 0000000..21016a8
--- /dev/null
+++ b/20231207/school-02.php
@@ -0,0 +1,47 @@
+<html>
+  <body>
+    <?php
+      try
+        {
+          $db = new PDO ('pgsql: host = localhost; dbname = testdb; user = dbs; password = 1234');
+        }
+      catch (\PDOException $e)
+        {
+          echo '<p>';
+          echo $e->getMessage();
+          echo '</p>';
+          echo '<p>';
+          exit ('cannot connect to database');
+          echo '</p>';
+        }
+      $db->exec ("INSERT INTO students (first_name, family_name) VALUES ( '"
+                 .$_POST["first_name"]."', '".$_POST["family_name"]
+                 ."' );");
+      $stmt = $db->query ('SELECT * FROM students;');
+      echo '<table><tr>';
+      while ($row = $stmt->fetch())
+        {
+          echo '<tr><td>';
+          echo $row['id'];
+          echo '</td><td>';
+          echo $row['first_name'];
+          echo '</td><td>';
+          echo $row['family_name'];
+          echo '</td></tr>';
+        }
+      echo '</tr></table>';
+    ?>
+  </body>
+</html> 
+
+<!-- Aus den Log-Dateien des Web-Servers:
+
+  [Thu Dec 07 12:50:06.952167 2023] [php:error] [pid 163970] [client ::1:52394]
+  PHP Fatal error:  Uncaught PDOException: SQLSTATE[42601]: Syntax error: 7
+  FEHLER:  Syntaxfehler bei \xc2\xbb)\xc2\xab\nLINE 1: ... INTO students
+  (first_name, family_name) VALUES ( Peter,  );\n
+  ^ in /var/www/dbs/school-01.php:17\nStack trace:\n#0
+  /var/www/dbs/school-01.php(17): PDO->exec()\n#1 {main}\n  thrown in
+  /var/www/dbs/school-01.php on line 17
+
+-->
diff --git a/20231207/school.html b/20231207/school.html
new file mode 100644
index 0000000..4299afc
--- /dev/null
+++ b/20231207/school.html
@@ -0,0 +1,10 @@
+<html>
+  <body>
+    <h3>Register new student</h3>
+    <form action="school-02.php" method="post">
+      First name: <input type="text" name="first_name"><br>
+      Family name: <input type="text" name="family_name"><br>
+      <input type="submit">
+    </form>
+  </body>
+</html> 
diff --git a/20231207/tiere-01.php b/20231207/tiere-01.php
new file mode 100644
index 0000000..734d144
--- /dev/null
+++ b/20231207/tiere-01.php
@@ -0,0 +1,32 @@
+<html>
+  <body>
+    <?php
+      $db = new PDO ('pgsql: host = localhost; dbname = testdb; user = dbs; password = abcd');
+      $stmt = $db->query ('SELECT * FROM tier;');
+      echo '<table><tr>';
+      while ($row = $stmt->fetch())
+        {
+          echo '<tr><td>';
+          echo $row['name'];
+          echo '</td><td>';
+          echo $row['tierart'];
+          echo '</tr></tr>';
+        }
+      echo '</tr></table>';
+    ?>
+  </body>
+</html> 
+
+<!-- Aus den Log-Dateien des Web-Servers:
+
+  [Thu Dec 07 12:27:14.834918 2023] [php:error] [pid 130668] [client ::1:60980]
+  PHP Fatal error:  Uncaught PDOException: SQLSTATE[08006] [7] connection to
+  server at "localhost" (::1), port 5432 failed: FATAL:
+  Passwort-Authentifizierung f\xc3\xbcr Benutzer \xc2\xbbdbs\xc2\xab
+  fehlgeschlagen\nconnection to server at "localhost" (::1), port 5432 failed:
+  FATAL:  Passwort-Authentifizierung f\xc3\xbcr Benutzer \xc2\xbbdbs\xc2\xab
+  fehlgeschlagen in /var/www/dbs/tiere-01.php:4\nStack trace:\n#0
+  /var/www/dbs/tiere-01.php(4): PDO->__construct()\n#1 {main}\n  thrown in
+  /var/www/dbs/tiere-01.php on line 4
+
+-->
diff --git a/20231207/tiere-02.php b/20231207/tiere-02.php
new file mode 100644
index 0000000..0df5e72
--- /dev/null
+++ b/20231207/tiere-02.php
@@ -0,0 +1,34 @@
+<html>
+  <body>
+    <?php
+      try
+        {
+          $db = new PDO ('pgsql: host = localhost; dbname = testdb; user = dbs; password = abcd');
+        }
+      catch (\PDOException $e)
+        {
+          echo $e->getMessage();
+        }
+      $stmt = $db->query ('SELECT * FROM tier;');
+      echo '<table><tr>';
+      while ($row = $stmt->fetch())
+        {
+          echo '<tr><td>';
+          echo $row['name'];
+          echo '</td><td>';
+          echo $row['tierart'];
+          echo '</tr></tr>';
+        }
+      echo '</tr></table>';
+    ?>
+  </body>
+</html> 
+
+<!-- Aus den Log-Dateien des Web-Servers:
+
+  [Thu Dec 07 12:32:31.413292 2023] [php:error] [pid 163972] [client ::1:55210]
+  PHP Fatal error:  Uncaught Error: Call to a member function query() on null in
+  /var/www/dbs/tiere-02.php:12\nStack trace:\n#0 {main}\n  thrown in
+  /var/www/dbs/tiere-02.php on line 12
+
+-->
diff --git a/20231207/tiere-03.php b/20231207/tiere-03.php
new file mode 100644
index 0000000..93941fe
--- /dev/null
+++ b/20231207/tiere-03.php
@@ -0,0 +1,30 @@
+<html>
+  <body>
+    <?php
+      try
+        {
+          $db = new PDO ('pgsql: host = localhost; dbname = testdb; user = dbs; password = abcd');
+        }
+      catch (\PDOException $e)
+        {
+          echo '<p>';
+          echo $e->getMessage();
+          echo '</p>';
+          echo '<p>';
+          exit ('cannot connect to database');
+          echo '</p>';
+        }
+      $stmt = $db->query ('SELECT * FROM tier;');
+      echo '<table><tr>';
+      while ($row = $stmt->fetch())
+        {
+          echo '<tr><td>';
+          echo $row['name'];
+          echo '</td><td>';
+          echo $row['tierart'];
+          echo '</tr></tr>';
+        }
+      echo '</tr></table>';
+    ?>
+  </body>
+</html> 
diff --git a/20231207/tiere-04.php b/20231207/tiere-04.php
new file mode 100644
index 0000000..5f78851
--- /dev/null
+++ b/20231207/tiere-04.php
@@ -0,0 +1,29 @@
+<html>
+  <body>
+    <?php
+      try
+        {
+          $db = new PDO ('pgsql: host = localhost; dbname = testdb; user = dbs; password = abcd');
+        }
+      catch (\PDOException $e)
+        {
+          echo '<p>';
+          echo $e->getMessage();
+          echo '</p>';
+          echo '<p>';
+          exit ("cannot connect to database\n</p></body></html>");
+        }
+      $stmt = $db->query ('SELECT * FROM tier;');
+      echo '<table><tr>';
+      while ($row = $stmt->fetch())
+        {
+          echo '<tr><td>';
+          echo $row['name'];
+          echo '</td><td>';
+          echo $row['tierart'];
+          echo '</tr></tr>';
+        }
+      echo '</tr></table>';
+    ?>
+  </body>
+</html> 
diff --git a/20231207/tiere-05.php b/20231207/tiere-05.php
new file mode 100644
index 0000000..5990373
--- /dev/null
+++ b/20231207/tiere-05.php
@@ -0,0 +1,30 @@
+<html>
+  <body>
+    <?php
+      try
+        {
+          $db = new PDO ('pgsql: host = localhost; dbname = testdb; user = dbs; password = 1234');
+        }
+      catch (\PDOException $e)
+        {
+          echo '<p>';
+          echo $e->getMessage();
+          echo '</p>';
+          echo '<p>';
+          exit ('cannot connect to database');
+          echo '</p>';
+        }
+      $stmt = $db->query ('SELECT * FROM tier;');
+      echo '<table><tr>';
+      while ($row = $stmt->fetch())
+        {
+          echo '<tr><td>';
+          echo $row['name'];
+          echo '</td><td>';
+          echo $row['tierart'];
+          echo '</tr></tr>';
+        }
+      echo '</tr></table>';
+    ?>
+  </body>
+</html> 
diff --git a/20231207/welcome-01.php b/20231207/welcome-01.php
new file mode 100644
index 0000000..de6e75c
--- /dev/null
+++ b/20231207/welcome-01.php
@@ -0,0 +1,5 @@
+<html>
+  <body>
+    Hello, <?php echo $_POST["name"]; ?>!
+  </body>
+</html> 
diff --git a/20231207/welcome-02.php b/20231207/welcome-02.php
new file mode 100644
index 0000000..ba73e93
--- /dev/null
+++ b/20231207/welcome-02.php
@@ -0,0 +1,6 @@
+<html>
+  <body>
+    Hello, <?php echo $_POST["name"]; ?>!<br/>
+    The answer is <?php echo $_POST["answer"]; ?>.
+  </body>
+</html> 
-- 
GitLab