Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/databaser/databaser4/

cost.php
createtables.php
databaser4.php
deck.php
deletecost.php
deletedeck.php
deletepitches.php
display.php
pitches.php

deck.php

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

<head>
  <meta charset="utf-8">
  <title>Lek</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 kort</h1>
  <?php
  
if (isset($_POST["action"])) {
    
$message null;
    if (
$_POST["action"] == "Lägg till" &&
      isset(
$_POST['name']) && isset($_POST['pitch']) && 
      !empty(
$_POST['name']) && !empty($_POST['pitch'])
    ) {
      
      
$name $_POST['name'];
      
$pitch $_POST['pitch'];
      
$defence $_POST['defence'];
      
$power $_POST['power'];
      
$cost $_POST['cost'];

      try {
        
# prepare
        
$sql "INSERT INTO deck (name, pitch, defence, power, cost) 
            VALUES (?, ?, ?, ?, ?)"
;
        
$stmt $dbconn->prepare($sql);
        
# the data we want to insert
        
$data = array($name$pitch$defence$power$cost);
        
# 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>Namn:</td>
        <td><input type="text" name="name" size=40 maxlength=100></td>
      </tr>
      <tr>
        <td>Pitch:</td>
        <td><select name="pitch">
          <?php 
            $sql 
"SELECT * FROM pitches";
            
$stmt $dbconn->prepare($sql);
            
// fetch width column names, create a table
            
$data = array();
            
$stmt->execute($data);
            while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
              
$pitch "";
              if (isset(
$res['pitch'])) {
                
$pitch htmlentities($res['pitch']);
              }
              
              
$color htmlentities($res['color']);

              echo 
"<option value='$pitch'>($pitch$color</option>";
            }
          
?>
        </select></td>
      </tr>
      <tr>
        <td>Defence:</td>
        <td><input type="number" name="defence" size=40 maxlength=10></td>
      </tr>
      <tr>
        <td>Power:</td>
        <td><input type="number" name="power" size=40 maxlength=10></td>
      </tr>
      <tr>
        <td>Cost:</td>
        <td><select name="cost">
          <?php 
            $sql 
"SELECT * FROM cost ORDER BY cost";
            
$stmt $dbconn->prepare($sql);
            
// fetch width column names, create a table
            
$data = array();
            
$stmt->execute($data);
            while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
              
$cost "";
              if (isset(
$res['cost'])) {
                
$cost htmlentities($res['cost']);
              }

              echo 
"<option>$cost</option>";
            }
          
?>
        </select></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 deck 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 deck";
$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)) {
  
$name htmlentities($res['name']);

  
$pitch "none";
  if (
null !== $res['pitch']) {
    
$pitch htmlentities($res['pitch']);
  }

  
$defence "none";
  if (
null !== $res['defence']) {
    
$defence htmlentities($res['defence']);
  }

  
$power "none";
  if (
null !== $res['power']) {
    
$power htmlentities($res['power']);
  }
  
  
$cost "none";
  if (
null !== $res['cost']) {
    
$cost htmlentities($res['cost']);
  }

  
$output .= "<tr>" .
    
"<td>$id</td>" .
    
"<td>$name</td>" .
    
"<td>$pitch</td>" .
    
"<td>$defence</td>" .
    
"<td>$power</td>" .
    
"<td>$cost</td>" .
    
"<td><form method='post' action=''>" .
    
"<input type='hidden' name='id' value='$id'>" .
    
"<button type='submit'>Ta bort</button></form></td>" .
    
"</tr>";
}
$output .= "</table>";
echo 
"$output";

$dbconn null;
?>

</html>