Webbserverprogrammering 1

Show sourcecode

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

webbserverprogrammering/projekt/quiz/

createtable.php
dbconnection.php
deletepost.php
deletetable.php
insertdefaultposts.php
insertposts.php
leaderboard.php
log_in.php
log_out.php
main.php
question_maker.php
quiz_form.php
quiz_list.php
quiz_maker.php
result.php
selectposts.php
sign_in.php
style.js
updateposts.php

updateposts.php

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

<body>
<?php
include ('dbconnection.php');
$message null;
$getid null;
$getusername null;
$getpassword null;

if (isset(
$_POST['username']) && isset($_POST['password']) && 
  isset(
$_POST['id']) && !empty($_POST['username']) && 
  !empty(
$_POST['password']) && !empty($_POST['id'])) {
      
    
$id $_POST['id'];
    
$username $_POST['username'];
    
$password $_POST['password'];
    
    try {    
        
# prepare
        
$sql "UPDATE quiz_users' SET username=?, password=?
          WHERE id=?"
;
        
$stmt $dbconn->prepare($sql);
        
# the data we want to insert
        
$data = array($username$password$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 minst förnamn och efternamn!<br /><br />"
;
}

if (isset(
$_GET['id']) && !empty($_GET['id']) ) {
    
$id $_GET['id'];
    
    try {    
        
# prepare
        
$sql "SELECT * FROM quiz_users' 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']);
        
$getusername htmlentities($res['username']);
        
$getpassword htmlentities($res['password']);
        
        
$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="username" size="40" maxlength="40" 
                  value="<?= $getusername?>">
            </td>
            </tr> 
            <tr>
            <td>Efternamn*:</td>
            <td>
                <input type="text" name="password" size="40" maxlength="40" 
                  value="<?= $getpassword?>">
            </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 quiz_users'";
    
$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['username']);
        
$last htmlentities($res['password']);
        
        
$output .= "<tr>".
            
"<td><a href='?id=$idx'>$idx</a></td>".
            
"<td>$first</td>".
            
"<td>$last</td>".
        
"</tr>";
        
    }
    
$output .= "</table>";
    echo 
"$output";

    
$dbconn null;
?>

</body>
</html>