Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/quiz/

admin.php
createQuiz.php
createadmin.php
logIn.php
myStats.php
playquiz.php
result.php
scoreboard.php
signIn.php
start.php
startsida.php
stats.php
style.css
tables.php
update.php

update.php

77 lines UTF-8 Windows (CRLF)
<?php 
  
/** @var PDO $dbconn */
  
include ("start.php");

  if (
$_COOKIE["type"] != "admin") {
    
header("location: startsida.php");
  }
?>

<?php 
  
// Om alla värden är satta, uppdatera kontot med de nya värdena
  
if (isset($_POST["id"]) && !empty($_POST["id"]) &&
  isset(
$_POST["username"]) && !empty($_POST["username"]) && 
  isset(
$_POST["password"]) && !empty($_POST["password"]) &&
  isset(
$_POST["type"]) && !empty($_POST["type"])) {
    
$id = htmlspecialchars($_POST["id"]);
    
$user = htmlspecialchars($_POST["username"]);
    
$pwd = htmlspecialchars($_POST["password"]);
    
$type = htmlspecialchars($_POST["type"]);

    try {    
        
$sql = "UPDATE quizUsers SET username=?, password=?, type=? 
          WHERE id=?"
;
        
$stmt = $dbconn->prepare($sql);
        
        
$data = array($user, $pwd, $type, $id);
        
$stmt->execute($data);
    }
    catch(
PDOException $e) {
      echo 
$sql . "<br>" . $e->getMessage();
    }
  }
?>

<?php 
  $id 
= null;
  
$user = null;
  
$pwd = null;
  
$type = null;

  if (isset(
$_GET["id"])) {
    try {
      
// Välj kontot med det inskickade id:t
      
$id = $_GET["id"];
      
$sql = "SELECT * FROM quizUsers WHERE id=?";
      
$stmt = $dbconn->prepare($sql);

      
$data = array($id);
      
$stmt->execute($data);

      
$res = $stmt->fetch(PDO::FETCH_ASSOC);

      
// Spara kontots värden
      
$user = $res["username"];
      
$pwd = $res["password"];
      
$type = $res["type"];
    }
    catch(
PDOException $e)
        {
        
$message .= $sql . "<br>" . $e->getMessage();
    }
  }
?>

  <!-- Skriv ut all kontoinformation i ett formulär där de kan ändras -->
  <form method="post">
    <table>    
      <tr><td>Användarnamn: </td><td><input type="text" name="username" value="<?= $user; ?>"><td></tr>
      <tr><td>Lösenord: </td><td><input type="text" name="password" value="<?= $pwd; ?>"></td></tr>
      <tr><td>Typ: </td><td><input type="text" name="type" value="<?= $type; ?>"></td></tr>
    </table>

    <input type="hidden" name="id" value="<?= $id ?>">
    <input type="submit" value="Uppdatera">
  </form>
</body>
</html>