Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/sql-intro/sql-intro3/

create-car.php
createtable.php
insertgarage-owner.php
sql-intro3-index.php
sql-intro3a.php
sql-intro3b.php
sql-intro3c.php
sql-intro3d.php

create-car.php

127 lines UTF-8 Windows (CRLF)
<!-- insertpost.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Namnlöst dokument</title>
</head>

<body>
<?php
$message 
null;
if (isset(
$_POST['reg']) && isset($_POST['färg']) && 
    isset(
$_POST['garage']) && isset($_POST['ägare']) && 
    !empty(
$_POST['reg']) && !empty($_POST['färg']) && 
    !empty(
$_POST['garage']) && !empty($_POST['ägare'])) {

    
$regnum $_POST['reg'];
    
$färg $_POST['färg'];
    
$garage $_POST['garage'];
    
$ägare $_POST['ägare'];

    
    include (
'../../../dbconnection.php');
    try {    
        
# prepare
        
$sql "INSERT INTO car (regnr, color, garage, owner) 
          VALUES (?, ?, ?, ?)"
;
        
$stmt $dbconn->prepare($sql);
        
# the data we want to insert
        
$data = array($regnum$färg$garage$ägare);
        
# 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 "<br>" $e->getMessage();
    }
    
    
$dbconn null;
} else {
    
$message .= "<br />Skriv in!<br /><br />";
}
echo 
$message;
?>

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

try {
  
$sql "SELECT * FROM garage";
  
$stmt $dbconn->prepare($sql);
  
$data = array();  
  
$stmt->execute($data);

  
$garageidlist = array();

  while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
    
$garagex htmlentities($res['garageid']);

    
array_push($garageidlist$garagex);
  }


  
$sql "SELECT * FROM owner";
  
$stmt $dbconn->prepare($sql);
  
$data = array();  
  
$stmt->execute($data);

  
$ägaridlist = array();

  while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
    
$ägarx htmlentities($res['ownerid']);

    
array_push($ägaridlist$ägarx);
  }
}
catch(
PDOException $e) {
  echo 
$sql "<br />" $e->getMessage();
}

?>

<form method="post" action=""> 
<table> 
<tr>
<td>Registreringsnummer:</td>
<td><input type="text" name="reg" size=40 maxlength=100>
</td>
</tr> 
<tr>
<td>Färg:</td>
<td><input type="text" name="färg" size=40 maxlength=100></td></tr> 
<tr>
<tr>
<td>Garage:</td>
<td>
<select name="garage"> 
<?php
foreach ($garageidlist as $g){
  echo 
'<option value='.$g.'>'.$g.'</option>';
}
?>
</select>
</td></tr> 
<tr>
<tr>
<td>Ägare:</td>
<td>
<select name="ägare"> 
<?php
foreach ($ägaridlist as ){
  echo 
'<option value='..'>'..'</option>';
}
?>
</select>
</td></tr> 
<tr>
<td>* = obligatoriskt</td>
<td><button type="submit">Lägg till</button></td></tr> 
</table> 
</form>
</body>
</html>