Webbserverprogrammering 1

Show sourcecode

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

ramverket/exercises/mysql-intro/

ovn_sqlintr1.php
ovn_sqlintr2.php

ovn_sqlintr1.php

139 lines UTF-8 Windows (CRLF)
<?php
  
// Title: MySQL Intro 1
  
error_reporting(-1); // Report all type of errors
  
ini_set('display_errors'1); // Display all errors
  
ini_set('output_buffering'0); // Do not buffer outputs, write directly
?>

<?php include("../../incl/connect_db.php"); ?>

<!DOCTYPE html>
<html lang="sv">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>MySQL Intro 1</title>
</head>
<body>
  <?php 
    
function createTable() {
     return 
"CREATE TABLE friends (
      id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
      first_name VARCHAR(30) NOT NULL,
      last_name VARCHAR(30) NOT NULL,
      mobile VARCHAR(50),
      email VARCHAR(50)
      )"
;
    }

    function 
getTableRow($conn$key$value) {
      
$sql "SELECT * FROM friends where $key = '$value'";
      
$stmt $conn->prepare($sql);
      
$stmt->execute();

      return 
$stmt;
    }

    function 
getAllTableRows($conn) {
      
$sql "SELECT * FROM friends";
      
$stmt $conn->prepare($sql);
      
$stmt->execute();

      return 
$stmt;
    }

    function 
insertTableRow($conn$first_name$last_name$mobile$email) {
      
$sql "INSERT INTO friends (first_name, last_name, mobile, email) VALUES (?, ?, ?, ?)";
      
$stmt $conn->prepare($sql);
      
$stmt->execute([$first_name$last_name$mobile$email]);
    }

    function 
deleteTableRow($conn$id) {
      
$sql "DELETE FROM friends WHERE id = ?";
      
$stmt $conn->prepare($sql);
      
$stmt->execute([$id]);
    }

    function 
deleteAllTableRows($conn) {
      
$sql "DELETE FROM friends";
      
$stmt $conn->prepare($sql);
      
$stmt->execute();
    }

    function 
updateTableRow($conn$id$first_name$last_name$mobile$email) {
      
$sql "UPDATE friends SET first_name = ?, last_name = ?, mobile = ?, email = ? WHERE id = ?";
      
$stmt $conn->prepare($sql);
      
$stmt->execute([$first_name$last_name$mobile$email$id]);
    }

    if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
      if (isset(
$_POST['delete_all'])) {
        
deleteAllTableRows($conn);
      }

      if (isset(
$_POST['insert_row'])) {
        
insertTableRow($conn$_POST['first_name'], $_POST['last_name'], $_POST['mobile'], $_POST['email']);
      }

      if (isset(
$_POST['delete_row'])) {
        
deleteTableRow($conn$_POST['id']);
      }

      if (isset(
$_POST['update_row'])) {
        
updateTableRow($conn$_POST['id'], $_POST['first_name'], $_POST['last_name'], $_POST['mobile'], $_POST['email']);
      }

      
header("Location: ovn_sqlintr1.php");
      exit();
    }

    
$data getAllTableRows($conn);

    echo 
"<h1>Tabell med kompisar</h1>";
    echo 
"<br><table style='width: 100%; padding: 20px; background-color: lightgray; border-radius: 10px;'>";
    echo 
"<tr><th>ID</th><th>Förnamn</th><th>Efternamn</th><th>Telefonnummer</th><th>E-post</th></tr>";
    
    while (
$row $data->fetch(PDO::FETCH_ASSOC)) {
      echo 
"<tr style='font-weight: 50;'>
      <td>
{$row['id']}</td>
      <td>
{$row['first_name']}</td>
      <td>
{$row['last_name']}</td>
      <td>
{$row['mobile']}</td>
      <td>
{$row['email']}</td>
      <td><form action='' method='POST'><input type='hidden' name='id' value='
{$row['id']}'><input type='submit' name='delete_row' value='Ta bort'></form></td>
      </tr>"
;
    }

    if (
$data->rowCount() === 0) {
      echo 
"<tr><td colspan='5' style='padding-top: 10px;'><i>Inga kompisar hittades<i></td></tr>";
    } else {
      echo 
"<tr><th><form action='' method='POST'><input type='submit' name='delete_all' value='Radera alla'></form></th></tr>";
    }

    echo 
"</table><br><br>";
  
?>

  <div style="display: flex; gap: 10px">
    <form action="" method="POST" style="align-items: center; display: flex; flex-direction: column; gap: 10px; background-color: lightgray; border-radius: 10px; padding: 20px; width: fit-content">
      <input type="text" name="first_name" placeholder="Förnamn">
      <input type="text" name="last_name" placeholder="Efternamn">
      <input type="number" name="mobile" placeholder="Telefonnummer">
      <input type="email" name="email" placeholder="E-post">
      <input type="submit" name="insert_row" value="Lägg till kompis">
    </form>
    <form action="" method="POST" style="align-items: center; display: flex; flex-direction: column; gap: 10px; background-color: lightgray; border-radius: 10px; padding: 20px; width: fit-content">
      <input type="number" name="id" placeholder="ID">
      <input type="text" name="first_name" placeholder="Förnamn">
      <input type="text" name="last_name" placeholder="Efternamn">
      <input type="number" name="mobile" placeholder="Telefonnummer">
      <input type="email" name="email" placeholder="E-post">
      <input type="submit" name="update_row" value="Uppdatera kompis">
    </form>
  </div>
  <style>
    th {
      text-align: start;
    }
  </style>
</body>
</html>