Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/projects/slutprojekt/

class/
create-categories.php
create-recipe.php
css/
db_content.php
forgot_password.php
include/
login.php
logout.php
recipe-search.php
recipe.php
reset_password.php
signin.php
start.php
tabeller/
verify.php

db_content.php

215 lines UTF-8 Windows (CRLF)
<?php
session_start
();
include(
'../../dbconnection.php');
ob_clean();
include(
'include/session-variables.php');

if(
$_SESSION['admin'] != true) {
  
header("Location: login.php");
  exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="css/styles.css">
  <title>Hantera Data</title>
  <style>
    .container {
      padding: 20px;
    }
    h1 {
      text-align: center;
      margin-bottom: 20px;
    }
    .table-container {
      overflow-x: auto;
      margin-bottom: 20px;
      border: 1px solid #ccc;
      border-radius: 5px;
    }
    .data-table {
      width: 100%;
      border-collapse: collapse;
    }
    .data-table th, .data-table td {
      padding: 8px 12px;
      border: 1px solid #ddd;
      text-align: left;
    }
    .data-table th {
      background-color: #f2f2f2;
      font-weight: bold;
    }
    .data-table tbody tr:nth-child(even) {
      background-color: #f9f9f9;
    }
    .delete-button {
      background-color: #f44336;
      color: white;
      border: none;
      padding: 6px 10px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 14px;
      border-radius: 4px;
      cursor: pointer;
    }
    .delete-button:hover {
      background-color: #d32f2f;
    }
    .success-message {
      color: green;
      margin-bottom: 10px;
      padding: 10px;
      border: 1px solid green;
      background-color: #e6ffe6;
      border-radius: 5px;
    }
    .error-message {
      color: red;
      margin-bottom: 10px;
      padding: 10px;
      border: 1px solid red;
      background-color: #ffe6e6;
      border-radius: 5px;
    }
  </style>
</head>
<body>
  <div id="page-container">
    <div id="content-wrap">
      <?php include('include/header.php'); ?>
      <div class="container">
        <h1>Hantera Data</h1>
        <?php
        
if (isset($_GET['delete_table']) && isset($_GET)) {
          
$tableName $_GET['delete_table'];
          
$idToDelete null;
          
$primaryKeyColumn '';

          if (
$tableName === 'users' && isset($_GET['user_id'])) {
            
$primaryKeyColumn 'user_id';
            
$idToDelete $_GET['user_id'];
          } elseif (
$tableName === 'categories' && isset($_GET['category_id'])) {
            
$primaryKeyColumn 'category_id';
            
$idToDelete $_GET['category_id'];
          } elseif (
$tableName === 'recipes' && isset($_GET['recipe_id'])) {
            
$primaryKeyColumn 'recipe_id';
            
$idToDelete $_GET['recipe_id'];
          } elseif (
$tableName === 'recipe_categories' && isset($_GET['recipe_id'])) {
            
$primaryKeyColumn 'recipe_id';
            
$idToDelete $_GET['recipe_id'];
            
$categoryToDelete $_GET['category_id'] ?? null;
          } elseif (
$tableName === 'comments' && isset($_GET['comment_id'])) {
            
$primaryKeyColumn 'comment_id';
            
$idToDelete $_GET['comment_id'];
          } elseif (
$tableName === 'ratings' && isset($_GET['rating_id'])) {
            
$primaryKeyColumn 'rating_id';
            
$idToDelete $_GET['rating_id'];
          } elseif (
$tableName === 'favorites' && isset($_GET['favorite_id'])) {
            
$primaryKeyColumn 'favorite_id';
            
$idToDelete $_GET['favorite_id'];
          }

          if (
$primaryKeyColumn && $idToDelete !== null) {
            try {
              
$sql "DELETE FROM $tableName WHERE $primaryKeyColumn = :id";
              if (
$tableName === 'recipe_categories' && isset($categoryToDelete)) {
                
$sql .= " AND category_id = :category_id";
              }
              
$stmt $dbconn->prepare($sql);
              
$stmt->bindParam(':id'$idToDelete);
              if (
$tableName === 'recipe_categories' && isset($categoryToDelete)) {
                
$stmt->bindParam(':category_id'$categoryToDelete);
              }
              
$stmt->execute();

              if (
$stmt->rowCount() > 0) {
                echo 
"<div class='success-message'>Raden med ID $idToDelete i tabellen $tableName har tagits bort.</div>";
              } else {
                echo 
"<div class='error-message'>Kunde inte ta bort raden med ID $idToDelete i tabellen $tableName.</div>";
              }
            } catch (
PDOException $e) {
              echo 
"<div class='error-message'>Fel vid borttagning från $tableName: " htmlspecialchars($e->getMessage()) . "</div>";
            }
          } else {
            echo 
"<div class='error-message'>Ogiltiga parametrar för borttagning.</div>";
          }
        }

        
displayTable($dbconn'users');
        
displayTable($dbconn'categories');
        
displayTable($dbconn'recipes');
        
displayTable($dbconn'recipe_categories');
        
displayTable($dbconn'comments');
        
displayTable($dbconn'ratings');
        
displayTable($dbconn'favorites');

        function 
displayTable($dbconn$tableName) {
          try {
            
$sql "SELECT * FROM $tableName";
            
$stmt $dbconn->prepare($sql);
            
$stmt->execute();
            
$results $stmt->fetchAll(PDO::FETCH_ASSOC);

            if (
$results) {
              echo 
"<div class='table-container'>";
              echo 
"<h2>Tabell: $tableName</h2>";
              echo 
"<table class='data-table'>";
              echo 
"<thead><tr>";
              foreach (
$results[0] as $column => $value) {
                echo 
"<th>" htmlspecialchars($column) . "</th>";
              }
              echo 
"<th>Åtgärder</th>";
              echo 
"</tr></thead><tbody>";

              foreach (
$results as $row) {
                echo 
"<tr>";
                foreach (
$row as $column => $value) {
                  echo 
"<td>" htmlspecialchars($value) . "</td>";
                }

                
$primaryKeyColumn '';
                if (
$tableName === 'users'$primaryKeyColumn 'user_id';
                elseif (
$tableName === 'categories'$primaryKeyColumn 'category_id';
                elseif (
$tableName === 'recipes'$primaryKeyColumn 'recipe_id';
                elseif (
$tableName === 'recipe_categories'$primaryKeyColumn 'recipe_id';
                elseif (
$tableName === 'comments'$primaryKeyColumn 'comment_id';
                elseif (
$tableName === 'ratings'$primaryKeyColumn 'rating_id';
                elseif (
$tableName === 'favorites'$primaryKeyColumn 'favorite_id';

                if (
$primaryKeyColumn) {
                  
$deleteUrl "?delete_table=$tableName&$primaryKeyColumn=" htmlspecialchars($row[$primaryKeyColumn]);
                  if (
$tableName === 'recipe_categories' && isset($row['category_id'])) {
                    
$deleteUrl .= "&category_id=" htmlspecialchars($row['category_id']);
                  }
                  echo 
"<td><a class='delete-button' href='$deleteUrl' onclick='return confirm(\"Är du säker på att du vill ta bort denna rad?\");'>Ta bort</a></td>";
                } else {
                  echo 
"<td>Ingen primärnyckel</td>";
                }

                echo 
"</tr>";
              }

              echo 
"</tbody></table>";
              echo 
"</div><br>";
            } else {
              echo 
"<p>Ingen data hittades i tabellen $tableName.</p><br>";
            }
          } catch (
PDOException $e) {
            echo 
"<div class='error-message'>Fel vid hämtning av data från $tableName: " htmlspecialchars($e->getMessage()) . "</div><br>";
          }
        }
        
?>
      </div>
      <?php include('include/footer.php'); ?>
    </div>
  </div>
</body>
</html>