Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbserverprogrammering/exercises/mysql/

ovning_1.php

ovning_1.php

106 lines UTF-8 Windows (CRLF)
<?php
    $servername 
"localhost";
    
$username "root";
    
$password "";
    
$dbname "mindatabas";
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
  <title>Document</title>

  <style>
    .insert-form {
      display: flex;
      flex-direction: row;
    }
  </style>
</head>
<body>
  <h1>Databas: kompisar</h1>


  <form method="post" class="insert-form">
    <input required type="text" name="förnamn" id="förnamn" placeholder="Förnamn">
    <input required type="text" name="efternamn" id="efternamn" placeholder="Efternamn">
    <input required type="tel" name="mobil" id="mobil" placeholder="Mobil">
    <input required type="email" name="epost" id="epost" placeholder="Epost">

    <button type="submit">Skicka</button>
  </form>

  <?php
    
if (isset($_POST["epost"])) {
      
$fornamn htmlspecialchars($_POST["förnamn"]);
      
$efternamn htmlspecialchars($_POST["efternamn"]);
      
$mobil htmlspecialchars($_POST["mobil"]);
      
$epost htmlspecialchars($_POST["epost"]);

      try {
        
$conn = new PDO("mysql:host=$servername;dbname=$dbname"$username$password);
        
// set the PDO error mode to exception
        
$conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        
$sql "INSERT INTO kompisar (förnamn, efternamn, mobil, epost) VALUES (:fornamn, :efternamn, :mobil, :epost)";
        
$stmt $conn->prepare($sql);
        
$stmt->bindParam(':fornamn'$fornamn);
        
$stmt->bindParam(':efternamn'$efternamn);
        
$stmt->bindParam(':mobil'$mobil);
        
$stmt->bindParam(':epost'$epost);
        
$stmt->execute();
        echo 
"New record created successfully";
      } catch (
PDOException $e) {
        echo 
$sql "<br>" $e->getMessage();
      }

      
$conn null;
    }
  
?>

  <?php
    
echo "<table style='border: solid 1px black;'>";
    echo 
"<tr><th>Id</th><th>Förnamn</th><th>Efternamn</th><th>Mobil</th><th>Epost</th></tr>";

    class 
TableRows extends RecursiveIteratorIterator {
      function 
__construct($it) {
        
parent::__construct($itself::LEAVES_ONLY);
      }
      
#[\ReturnTypeWillChange]
      
function current() {
        return 
"<td style='width:150px;border:1px solid black;'>" htmlspecialchars(parent::current()) . "</td>";
      }
      
#[\ReturnTypeWillChange]
      
function beginChildren() {
        echo 
"<tr>";
      }
      
#[\ReturnTypeWillChange]
      
function endChildren() {
        echo 
"</tr>\n";
      }
    }
    try {
      
$conn = new PDO("mysql:host=$servername;dbname=$dbname"$username$password);
      
$conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
      
$sql "SELECT idnummer, förnamn, efternamn, mobil, epost FROM kompisar;";
      
$stmt $conn->prepare($sql);
      
$stmt->execute();

      
$result $stmt->fetchAll(PDO::FETCH_ASSOC);
      foreach(new 
TableRows(new RecursiveArrayIterator($result)) as $k=>$v) {
        echo 
$v;
      }
    } catch (
PDOException $e) {
      echo 
"Select error: " $e->getMessage();
    }
    
$conn null;
    echo 
"</table>";
  
?>

  <?php
    
  ?>
</body>
</html>