Webbserv1: Källkod
Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/projects/slutprojekt/

blala.php
chat.php
createtable.php
delete.php
deletetable.php
deletetables.php
fetch_messages.php
filhantering/
footer.php
header.php
home.php
login.php
new_password.php
password_reset.php
profile.php
send_message.php
signup.php
verify.php

delete.php

154 lines UTF-8 Windows (CRLF)
<?php
session_start
();
if (
$_SESSION['admin']!=1) {
    
header('Location: login.php');
}
include(
'../../dbconnection.php');
include (
'header.php');
?>
<!DOCTYPE html>
<html lang="sv">
<head>
  <style>
      body {
          font-family: Arial, sans-serif;
          padding: 2rem;
      }
      label, select, input {
          margin-top: 1rem;
          display: block;
      }
      .message {
          margin-top: 1rem;
          padding: 1rem;
          background-color: #f1f1f1;
          border-left: 4px solid #0077cc;
      }
      table {
          width: 100%;
          border-collapse: collapse;
          margin-top: 2rem;
      }
      table, th, td {
          border: 1px solid #ddd;
      }
      th, td {
          padding: 8px;
          text-align: left;
      }
  </style>
</head>
<body>
<?php
class DatabaseHandler {
  private 
$pdo;
  private 
$allowedTables = ['webusers''pictures''interests''friends''messages'];

  public function 
__construct(PDO $pdo) {
      
$this->pdo $pdo;
  }

  public function 
getAllRows(string $table): array {
      if (!
in_array($table$this->allowedTables)) {
          return [];
      }

      try {
          
$stmt $this->pdo->prepare("SELECT * FROM `$table`");
          
$stmt->execute();
          return 
$stmt->fetchAll(PDO::FETCH_ASSOC);
      } catch (
PDOException $e) {
          return [];
      }
  }

  public function 
deleteById(string $tableint $id): string {
      if (!
in_array($table$this->allowedTables)) {
          return 
"Ogiltig tabell vald.";
      }

      try {
          
$stmt $this->pdo->prepare("DELETE FROM `$table` WHERE id = :id");
          
$stmt->bindParam(':id'$idPDO::PARAM_INT);
          
$stmt->execute();

          if (
$stmt->rowCount() > 0) {
              return 
"Raden med ID $id har tagits bort från '$table'.";
          } else {
              return 
"Ingen rad hittades med ID $id i '$table'.";
          }
      } catch (
PDOException $e) {
          return 
"Fel vid borttagning: " $e->getMessage();
      }
  }
}

$meddelande "";
$tabellData = [];
$selectedTable = isset($_POST['tabell']) ? $_POST['tabell'] : 'webusers'// Bevara valt tabell

if ($_SERVER["REQUEST_METHOD"] === "POST") {
  
$tabell $_POST["tabell"] ?? 'webusers';
  
$id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;

  
$db = new DatabaseHandler($dbconn);

  if (isset(
$_POST['delete'])) {
      
$meddelande $db->deleteById($tabell$id);
  }

  
$tabellData $db->getAllRows($tabell);
}
?>



  <h1>Ta bort rad från tabell</h1>

  <?php if ($meddelande): ?>
      <div class="message"><?php echo htmlspecialchars($meddelande); ?></div>
  <?php endif; ?>

  <form method="post">
      <label for="tabell">Välj tabell:</label>
      <select name="tabell" id="tabell" required onchange="this.form.submit()">
          <option value="webusers" <?php echo ($selectedTable === 'webusers') ? 'selected' ''?>>webusers</option>
          <option value="pictures" <?php echo ($selectedTable === 'pictures') ? 'selected' ''?>>pictures</option>
          <option value="interests" <?php echo ($selectedTable === 'interests') ? 'selected' ''?>>interests</option>
          <option value="friends" <?php echo ($selectedTable === 'friends') ? 'selected' ''?>>friends</option>
          <option value="messages" <?php echo ($selectedTable === 'messages') ? 'selected' ''?>>messages</option>
      </select>
  </form>

  <?php if (count($tabellData) > 0): ?>
      <h2>Rader i tabellen '<?php echo htmlspecialchars($selectedTable); ?>'</h2>
      <table>
          <thead>
              <tr>
                  <th>ID</th>
                  <th>Data</th>
                  <th>Åtgärd</th>
              </tr>
          </thead>
          <tbody>
              <?php foreach ($tabellData as $rad): ?>
                  <tr>
                      <td><?php echo htmlspecialchars($rad['id']); ?></td>
                      <td><?php echo htmlspecialchars(json_encode($rad)); ?></td>
                      <td>
                          <form method="post" style="display:inline;">
                              <input type="hidden" name="tabell" value="<?php echo htmlspecialchars($selectedTable); ?>">
                              <input type="hidden" name="id" value="<?php echo htmlspecialchars($rad['id']); ?>">
                              <button type="submit" name="delete">Radera</button>
                          </form>
                      </td>
                  </tr>
              <?php endforeach; ?>
          </tbody>
      </table>
  <?php else: ?>
      <p>Inga rader att visa i tabellen '<?php echo htmlspecialchars($selectedTable); ?>'.</p>
  <?php endif; ?>

</body>
</html>