Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/databaser/databaser2/

admin.php
createtable.php
databaser2.php
selectposts.php
start.php
updateposts.php
user.php

updateposts.php

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

<body>
<?php
include ('../../../dbconnection.php');
  
$message null;
  
$getid null;
  
$getfirstname null;
  
$getlastname null;
  
$getusername null;
  
$getpwd null;
  
$gettyp null;
  
$getlastupdate null;
  
  if (
    isset(
$_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['username']) && 
    isset(
$_POST['pwd']) && isset($_POST['typ']) &&
    !empty(
$_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['username']) && 
    !empty(
$_POST['pwd']) && !empty($_POST['typ'])
  ) {

    
$id $_POST['id'];
    
$firstname $_POST['firstname'];
    
$lastname $_POST['lastname'];
    
$username $_POST['username'];
    
$pwd $_POST['pwd'];
    
$typ $_POST['typ'];

    try {
      
# prepare
      
$sql "UPDATE Users SET firstname=?, lastname=?, username=?, pwd=?, typ=?, lastupdate=now() 
            WHERE id=?"
;
      
$stmt $dbconn->prepare($sql);
      
# the data we want to insert
      
$data = array($firstname$lastname$username$pwd$typ$id);
      
# execute width array-parameter
      
$stmt->execute($data);

      
$message .= "Record updated successfully.<br />";
      
// clear form from info
      
$_GET['id'] = null;
    } catch (
PDOException $e) {
      
$message .= $sql "<br>" $e->getMessage();
    }
  } else {
    
$message .= "Först väljer du en post. 
        Sen måste du fylla i informationen!<br /><br />"
;
  }

  if (isset(
$_GET['id']) && !empty($_GET['id'])) {
    
$id $_GET['id'];

    try {
      
# prepare
      
$sql "SELECT * FROM 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']);
      
$getfirstname htmlentities($res['firstname']);
      
$getlastname htmlentities($res['lastname']);
      
$getusername htmlentities($res['username']);
      
$getpwd htmlentities($res['pwd']);
      
$gettyp htmlentities($res['typ']);
      
$getlastupdate htmlentities($res['lastupdate']);

      
$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="40"
          value="<?= $getfirstname?>">
      </td>
    </tr>
    <tr>
      <td>Efternamn*:</td>
      <td>
        <input type="text" name="lastname" size="40" maxlength="40"
          value="<?= $getlastname?>">
      </td>
    </tr>
    <tr>
      <td>Användarnamn:</td>
      <td>
        <input type="text" name="username" size="30" maxlength="30"
          value="<?= $getusername?>">
      </td>
    </tr>
    <tr>
      <td>Lösenord:</td>
      <td>
        <input type="password" name="pwd" size="30" maxlength="30"
          value="<?= $getpwd?>">
      </td>
    </tr>
    <tr>
      <td>Typ:</td>
      <td>
        <input type="text" name="typ" size="30" maxlength="30"
          value="<?= $gettyp?>">
      </td>
    </tr>
    <tr>
      <td>Senast updaterad:</td>
      <td>
        <input type="text" name="lastupdate" size="30" maxlength="30"
          value="<?= $getlastupdate?>">
      </td>
    </tr>
    <tr>
      <td>* = obligatoriskt</td>
      <td>
        <input type="submit" name="action" value="Uppdatera">
        <input type="hidden" name="id" value="<?= $getid?>">
      </td>
    </tr>
  </table>
</form>
<?php    
    
// Ouput table with all posts
/*** The SQL SELECT statement ***/
$sql "SELECT * FROM Users";
$stmt $dbconn->prepare($sql);
// fetch width column names, create a table
$data = array();
$stmt->execute($data);
$output "<table class='list'>";
while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
  
$idx htmlentities($res['id']);
  
$first htmlentities($res['firstname']);
  
$last htmlentities($res['lastname']);

  
$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>