Webbserverprogrammering 1

Show sourcecode

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

webbserverprogrammering/exercises/mysqlintro/kryptering/

admin.php
dbconnection.php
deletepost.php
hemlig.php
index.php
insertpost.php
lås.php
selectposts.php
updatepost.php

updatepost.php

174 lines UTF-8 Windows (CRLF)
<?php 
session_start
();
?>
<!-- updatepost.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Update</title>
</head>

<body>
<?php
include ('dbconnection.php');
include(
'lås.php');
$message null;
$getid null;
$getfirstname null;
$getlastname null;
$getuser null;
$getpassword null;
$getadmin null;

if (isset(
$_POST['firstname']) && isset($_POST['lastname']) && 
  isset(
$_POST['id']) && !empty($_POST['firstname']) && 
  !empty(
$_POST['lastname']) && !empty($_POST['id'])) {
      
    
$id $_POST['id'];
    
$firstname $_POST['firstname'];
    
$lastname $_POST['lastname'];
    
$username $_POST['username'];
    
$password $_POST['password'];
    
$admin $_POST['admin'];
    
    try {    
        
$password password_hash($passwordPASSWORD_DEFAULT);
        
# prepare
        
$sql "UPDATE anvandareKrypt SET firstname=?, lastname=?, username=?, password=?, admin=?
          WHERE id=?"
;
        
$stmt $dbconn->prepare($sql);
        
# the data we want to insert
        
$data = array($firstname$lastname$username$password$admin$id);
        
# execute width array-parameter
        
$stmt->execute($data);
            
        
$message .= "<br />Record updated successfully.<br />";
        
// clear form from info
        
$_GET['id'] = null;
    }
    catch(
PDOException $e)
        {
        
$message .= $sql "<br>" $e->getMessage();
    }
} else {
    
$message .= "<br />Först väljer du en post. 
      Sen måste du fylla i alla fälten!<br /><br />"
;
}

if (isset(
$_GET['id']) && !empty($_GET['id']) ) {
    
$id $_GET['id'];
    
    try {    
        
# prepare
        
$sql "SELECT * FROM anvandareKrypt WHERE id=?";
        
$stmt $dbconn->prepare($sql);
        
# the data we want to insert
        
$data = array($id);
        
# execute width array-parameter
        
$stmt->execute($data);
            
        
$res $stmt->fetch(PDO::FETCH_ASSOC);
        
$getid htmlentities($res['id']);
        
$getfirstname htmlentities($res['firstname']);
        
$getlastname htmlentities($res['lastname']);
        
$getuser htmlentities($res['username']);
        
$getadmin htmlentities($res['admin']);
        
        
$message .= "<br />Record was selected successfully.<br />";
    }
    catch(
PDOException $e)
        {
        
$message .= $sql "<br>" $e->getMessage();
    }
} else {

    
$message .= "<br />Välj en ny post att uppdatera.<br /><br />";

}

echo 
$message;

    
?>
        <form method="post" action=""> 
        <table> 
            <tr>
            <td>Förnamn*:</td>
            <td>
                <input type="text" name="firstname" size="40" maxlength="30" 
                  value="<?= $getfirstname?>">
            </td>
            </tr> 
            <tr>
            <td>Efternamn*:</td>
            <td>
                <input type="text" name="lastname" size="40" maxlength="30" 
                  value="<?= $getlastname?>">
            </td>
            </tr>  
            <tr>
            <td>Användarnamn:</td>
            <td>
                <input type="text" name="username" size="20" maxlength="30" 
                  value="<?= $getuser?>">
            </td>
            </tr>
            <tr>
            <td>Lösenord:</td>
            <td>
                <input type="text" name="password" size="20" maxlength="20" 
                  value="">
            </td>
            </tr> 
            <tr>
            <td>Admin:</td>
            <td>
                <input type="text" name="admin" 
                  value="<?= $getadmin?>">
            </td>
            </tr> 
            <tr>
            <td>* = obligatoriskt</td>
            <td>
                <button type="submit">Lägg till</button>
                <input type="hidden" name="id" value="<?= $getid?>">
            </td>
            </tr> 
        </table> 
    </form>
<?php    
    
// Ouput table with all posts
    /*** The SQL SELECT statement ***/
    
$sql "SELECT * FROM anvandareKrypt";
    
$stmt $dbconn->prepare($sql);
    
// fetch width column names, create a table
    
$data = array();  
    
$stmt->execute($data);
    
$output "<table><caption>En ostylad tabell!</caption>";
    while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
        
$idx htmlentities($res['id']);
        
$first htmlentities($res['firstname']);
        
$last htmlentities($res['lastname']);
        
$username htmlentities($res['username']);
        
$password htmlentities($res['password']);
        
$admin htmlentities($res['admin']);
        
        
$output .= "<tr>".
            
"<td><a href='?id=$idx'>$idx</a></td>".
            
"<td>$first</td>".
            
"<td>$last</td>".
            
"<td>$username</td>".
            
"<td>$password</td>".
            
"<td>$admin</td>".
        
"</tr>";
        
    }
    
$output .= "</table>";
    echo 
"$output";

    
$dbconn null;
?>

<button onclick="window.location.href='admin.php'">Meny</button>
</body>
</html>