Webbserverprogrammering 1

Show sourcecode

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

webbserverprogrammering/exercises/mysqlintro/garage/

dbconnection.php
insert.php
select.php
select3d.php
selectOwnerOrder.php
selectSortByG.php

selectSortByG.php

100 lines UTF-8 Windows (CRLF)
<?php 
session_start
();
?>
<!-- selectposts.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Select</title>
<style type="text/css">
    body{
        font-family: arial;
    }
    table{
        width: 200px;
        margin: auto;
        border: 1px black solid;
    }
</style>
</head>

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

try {

     
//The SQL SELECT statement 
    
$sql "SELECT * FROM owner ORDER BY name ASC";
    
$stmt $dbconn->prepare($sql);

    
$data = array();  
    
$stmt->execute($data);
    
$output "<table><caption>Användare:</caption><th>OwnerID</th><th>Namn</th>";
    while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
        
$output .= "<tr>".
            
"<td>".htmlentities($res['ownerid'])."</td>".
            
"<td>".htmlentities($res['name'])."</td>".
        
"</tr>";
    }
    
$output .= "</table>";
    echo 
"$output";
}
catch(
PDOException $e)
{
    echo 
$sql "<br />" $e->getMessage();
}
try {

     
//The SQL SELECT statement 
    
$sql "SELECT * FROM car ORDER BY garage ASC";
    
$stmt $dbconn->prepare($sql);

    
$data = array();  
    
$stmt->execute($data);


    
$output "<table><caption>Registrerade bilar:</caption>
    <th>Bil ID</th>
    <th>RegNr</th>
    <th>Färg</th>
    <th>Garage</th>
    <th>Owner</th>"
;
    while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
        
$g $res["garage"];
        
$sql2 "SELECT garage.name FROM garage WHERE garageid=$g";
        
$stmt2 $dbconn->prepare($sql2);
        
$data2 = array();  
        
$stmt2->execute($data2);  
        
$res2 $stmt2->fetch(PDO::FETCH_ASSOC); 
        
$o $res["owner"];
        
$sql3 "SELECT owner.name FROM owner WHERE ownerid=$o";
        
$stmt3 $dbconn->prepare($sql3);
        
$data3 = array();  
        
$stmt3->execute($data3);  
        
$res3 $stmt3->fetch(PDO::FETCH_ASSOC);        


        
$output .= "<tr>".
            
"<td>".htmlentities($res['carid'])."</td>".
            
"<td>".htmlentities($res['regnr'])."</td>".
            
"<td>".htmlentities($res['color'])."</td>".
            
"<td>".htmlentities($res2['name'])."</td>".
            
"<td>".htmlentities($res3['name'])."</td>".
        
"</tr>";
    }
    
$output .= "</table>";
    echo 
"$output";
}
catch(
PDOException $e)
{
    echo 
$sql "<br />" $e->getMessage();
}

$dbconn null;

?>
<button onclick="window.location.href='../garage'">Meny</button>
</body>
</html>