Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/databaser/databaser3/

car.php
createtables.php
databaser3.php
databaser3a.php
databaser3b.php
databaser3c.php
databaser3d.php
databaser3e.php
deletetables.php
garage.php
owner.php
start.php

car.php

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

<head>
  <meta charset="utf-8">
  <title>Bilar</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 bil</h1>
  <?php
  
if (isset($_POST["action"])) {
    
$message null;
    if (
$_POST["action"] == "Lägg till" &&
      isset(
$_POST['regnr']) && isset($_POST['color']) && 
      isset(
$_POST['garage']) && isset($_POST['owner']) &&
      !empty(
$_POST['regnr']) && !empty($_POST['color']) && 
      !empty(
$_POST['garage']) && !empty($_POST['owner'])
    ) {
      
$regnr $_POST['regnr'];
      
$color $_POST['color'];
      
$garage $_POST['garage'];
      
$owner $_POST['owner'];

      try {
        
# prepare
        
$sql "INSERT INTO car (regnr, color, garage, owner) 
            VALUES (?, ?, ?, ?)"
;
        
$stmt $dbconn->prepare($sql);
        
# the data we want to insert
        
$data = array($regnr$color$garage$owner);
        
# 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>Registreringsnummer:</td>
        <td><input type="text" name="regnr" size=40 maxlength=100></td>
      </tr>
      <tr>
        <td>Färg:</td>
        <td><input type="text" name="color" size=20 maxlength=20></td>
      </tr>
      <tr>
        <td>Garagets ID:</td>
        <td><select name="garage">
          <?php 
            $sql 
"SELECT * FROM garage";
            
$stmt $dbconn->prepare($sql);
            
// fetch width column names, create a table
            
$data = array();
            
$stmt->execute($data);
            while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
              
$garageid htmlentities($res['garageid']);
              
$name htmlentities($res['name']);

              echo 
"<option value='$garageid'>$name</option>";
            }
          
?>
        </select></td>
      </tr>
      <tr>
        <td>Ägarens ID:</td>
        <td><select name="owner">
          <?php 
            $sql 
"SELECT * FROM owner";
            
$stmt $dbconn->prepare($sql);
            
// fetch width column names, create a table
            
$data = array();
            
$stmt->execute($data);
            while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
              
$ownerid htmlentities($res['ownerid']);
              
$name htmlentities($res['name']);

              echo 
"<option value='$ownerid'>$name</option>";
            }
            echo 
"$output";
          
?>
        </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 car WHERE carid=?";
    
$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 car";
$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)) {
  
$carid htmlentities($res['carid']);
  
$regnr htmlentities($res['regnr']);
  
$color htmlentities($res['color']);
  
$garage htmlentities($res['garage']);
  
$owner htmlentities($res['owner']);

  
$output .= "<tr>" .
    
"<td>$carid</td>" .
    
"<td>$regnr</td>" .
    
"<td>$color</td>" .
    
"<td>$garage</td>" .
    
"<td>$owner</td>" .
    
"<td><form method='post' action=''>" .
    
"<input type='hidden' name='id' value='$carid'>" .
    
"<button type='submit'>Ta bort</button></form></td>" .
    
"</tr>";
}
$output .= "</table>";
echo 
"$output";

$dbconn null;
?>

</html>