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

selectposts.php

153 lines UTF-8 Windows (CRLF)
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Select</title>

  <style>
    .list {
      color: black;
      border: 3px solid black;
    }

    .list td {
      padding: 5px;
    }
  </style>

</head>

<body>
  <?php
  
include('../../../dbconnection.php'); ?>

  <br>
  <h1>Lägg till kompis</h1>
  <?php
  
if (isset($_POST["action"])) {
    
$message null;
    if (
$_POST["action"] == "Lägg till" &&
      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'])
    ) {
      
$firstname $_POST['firstname'];
      
$lastname $_POST['lastname'];
      
$username $_POST['username'];
      
$pwd $_POST['pwd'];
      
$typ $_POST['typ'];

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

        echo 
"New record created successfully";
        
$lastId $dbconn->lastInsertId();
        echo 
"id på sista posten: $lastId";
      } catch (
PDOException $e) {
        echo 
$sql $e->getMessage();
      }
    } else {
      
$message .= "Du måste fylla i informationen!<br /><br />";
    }
    echo 
$message;
  }
  
?>
  <form method="post" action="">
    <table>
      <tr>
        <td>Förnamn:</td>
        <td><input type="text" name="firstname" size=40 maxlength=100>
        </td>
      </tr>
      <tr>
        <td>Efternamn:</td>
        <td><input type="text" name="lastname" size=40 maxlength=100></td>
      </tr>
      <tr>
        <td>Användarnamn:</td>
        <td><input type="text" name="username" size=20 maxlength=20></td>
      </tr>
      <tr>
        <td>Lösenord:</td>
        <td><input type="password" name="pwd" size=30 maxlength=30></td>
      </tr>
      <tr>
        <td>Typ:</td>
        <td><input type="text" name="typ" size=30 maxlength=30></td>
      </tr>
      <tr>
        <td><input type="submit" name="action" value="Lägg till"></td>
      </tr>
    </table>
  </form>
</body>


<br>
<br>
<h1>Ta bort</h1>
<?php
$message 
null;
$id null;

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

  try {
    
# prepare
    
$sql "DELETE FROM Users WHERE id=?";
    
$stmt $dbconn->prepare($sql);
    
# the data we want to insert
    
$data = array($id);
    
# execute width array-parameter
    
$stmt->execute($data);

    
$message .= "Record deleted successfully.<br />";
  } catch (
PDOException $e) {
    
$message .= $sql "<br>" $e->getMessage();
  }
} else {
  
$message .= "<br />";
}

echo 
$message;

    
// 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>$idx</td>" .
    
"<td>$first</td>" .
    
"<td>$last</td>" .
    
"<td><form method='post' action=''>" .
    
"<input type='hidden' name='id' value='$idx'>" .
    
"<button type='submit'>Ta bort</button></form></td>" .
    
"</tr>";
}
$output .= "</table>";
echo 
"$output";

$dbconn null;
?>

</html>