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

create-categories.php

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

include(
'include/session-variables.php');
include(
'class/categoryClass.php');

if(
$_SESSION['admin'] != true) {
  
header("Location: login.php");
  exit;
}

$category = new Category($dbconn);

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" href="css/styles.css">
  <style>
    .current2 {
      text-decoration:underline !important ;
    }
    main {
      text-align:center;
    }
    table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 20px;
    }
    table, th, td {
      border: 1px solid black;
    }
    th, td {
      padding: 10px;
      text-align: left;
    }
    th {
      background-color: #f2f2f2;
    }
    .content{
      display:grid;
      grid-template-columns:1fr 1fr 1fr;
 
    }
    .categories-table{
      justify-self:left;
    }
    @media screen and (max-width: 700px){
      .content{
        grid-template-columns:1fr;
      }
      .categories-table{
      justify-self:center;
    }
    }


  </style>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Skapa kategori</title>
</head>
<body>
  <div id="page-container">
    <div id="content-wrap">
      <?php
        
include('include/header.php');
      
?>
      <main>
        <div class="content">
          <div></div>
          <div>
            <?php
            
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['name'])) {
              
$name $_POST['name'];
              
$category->createCategory($name);
              echo 
"<p style='color:green;'>Kategorin '$name' skapades!</p>";
            }
            
?>

            <h1>Skapa kategori</h1>
            <form action="" method="post">
              Skriv in namnet på kategorin: <br>
              <input type="text" name="name" required>
              <input type="submit" value="Skapa">
            </form>
          </div>

          <div class="categories-table">
            <h2>Existerande kategorier:</h2>
            <table>
              <tr>
                <th>Id</th>
                <th>Kategorinamn</th>
              </tr>
              <?php
              $sql 
"SELECT * FROM categories ORDER BY name ASC";
              
$stmt $dbconn->prepare($sql);
              
$stmt->execute();
              
$categories $stmt->fetchAll();

              if (
$categories) {
                foreach (
$categories as $category) {
                  echo 
"<tr>";
                  echo 
"<td>" $category['category_id'] . "</td>";
                  echo 
"<td>" $category['name'] . "</td>";
                  echo 
"</tr>";
                }
              } else {
                echo 
"<tr><td colspan='2'>Inga kategorier tillgängliga.</td></tr>";
              }
              
?>
            </table>
          </div>  
        </div>
        
      </main>
      <?php
        
include('include/footer.php');
      
?>
    </div>
  </div>
</body>
</html>