View sourcecode

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

edit.php

199 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
// ===========================================================================================
//
// edit.php
//
// Description:
//
// Author: Johan Hällgren
// Comment: Här har jag valt att sköta bägge tabellerna från samma fil och även ta bort i samma fil.
// Naturligtvis går det att dela upp detta i flera filer och sedan med länkar gå till respektive fil.


// -------------------------------------------------------------------------------------------
//
// Requires and includes
//
require_once('dbConn.php');
require_once(
'common.php');
require_once(
'functions.php');


// -------------------------------------------------------------------------------------------
//
// Take care of GET-variables
//
$action = (isset($_GET['action']) && $_GET['action']!="") ? $_GET['action'] : "";
$table = (isset($_GET['table']) && $_GET['table']!="") ? $_GET['table'] : "";
$id = (isset($_GET['id']) && $_GET['id']!="") ? $_GET['id'] : "";


// -------------------------------------------------------------------------------------------
//
// Variables for this page
//
$mess = isset($mess) ? "<p class=\"mess\">".$mess."</p>" "";



// -------------------------------------------------------------------------------------------
//
// First delete a post if we recieve informaton about
//
// Om någon post skall tas bort så gör vi detta först innan
// resten av sidan processas.
//
// Att ta bort från Car kan aldrig bli problem
// Att ta bort Person som äger Car går inte! Här måste vi bestämma hur detta skall göras,
// skall en borttagen person tappa kopplingen till bilar, alltså alla bilar som X äger sätts
// till null innan personen tas bort eller skall även bilarna falla?
//
if($action == "delete"){
    if(
$table == "car"){
        
$sql "DELETE FROM car WHERE carId = :carId;";
        
$prm = array(
        
'carId' => $id);
    } else if (
$table == "person"){
        
$sql "DELETE FROM person WHERE personId = :personId;";
        
$prm = array(
        
'personId' => $id);
    }
    
# kör sql
    
$stmt $pdo->prepare($sql);
    
$stmt->execute($prm);
    if(
$stmt->rowCount() == 0){
        
$mess "Inga rader togs bort....";
    } else {
        
$mess "Posten med id " .$id" togs bort.";
    }
}


// -------------------------------------------------------------------------------------------
//
// Create the content
// Car form
//

$owner = <<<EOD
<select for=personId name=personId>
    <option value='0' selected>No owner</option>
EOD;

$sql "SELECT * FROM person;";
$stmt $pdo->prepare($sql);
$stmt->execute();
$res $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach (
$res as $row) {
    
$owner .= "\n  <option value='{$row['personId']}'>{$row['firstName']} {$row['familyName']} </option>";
}
$owner .= "</select>";


// -------------------------------------------------------------------------------------------
//
// Create the content
// First box, this is the one with the form to select the table
//

$carSelected = isset($_GET['table']) && $_GET['table']=="car" "selected" "";
$personSelected = isset($_GET['table']) && $_GET['table']=="person" "selected" "";

$formRadioButton = <<< EOD
<script>
function submitForm() {
    document.myform.submit();
}
</script>
<form method="GET" action="?" name="myform">
    <fieldset>
        <ol>
            <li>
                <label for="table">Välj tabell</label>
                <select for=table name=table id="mySelect" onchange="submitForm()">
                    <option value='' selected>no table</option>
                    <option value='car' 
{$carSelected}>Car</option>
                    <option value='person' 
{$personSelected}>Person</option>
                </select>
            </li>
        </ol>
    </fieldset>
</form>
<p id="demo"></p>
EOD;

// -------------------------------------------------------------------------------------------
//
// Create the content
// Print the whole table with links for edit and delete.
//
$tableName = (isset($_GET['table']) && $_GET['table']!="") ? $_GET['table'] : "";
$table "";            // Skapar tom variabel för att slippa error när ingen tabell skall ritas ut

$arr="";
if(
$tableName !=""){
    switch (
$tableName) {
        case 
"car":
            
$headline "Lista alla bilar";
            
$arr = array("carId""personId""registrationNr""make""model""year""price");
            
$sql "SELECT * FROM car;";
            break;

        case 
"person":
            
$headline "Lista alla personer";
            
$arr = array("personId""firstName""familyName""birthDate""hasLicence");
            
$sql "SELECT * FROM person;";
            break;

         default:
             break;
     }
    
$table createTableFromSqlEdit($arr$sql$tableName);
}


// -----------------------------------------------------------------------------
//
// Sätt aside och content
//
$aside2 "";
$content $aside2 == "" "content_full" "content";


// ----------------------------------------------------------------------
//
// Content
//
$html = <<< EOD
  <article class="{$content}">
    <h1>Uppdatera och ta bort post</h1>
    
{$mess}

    <section class="box">
        <h2>Litet formulär</h2>
        <p>
{$formRadioButton}</p>
    </section>

    <section class="box">
        <h2>
{$tableName}</h2>
        <p>
{$table}</p>
        <p>Ta bort en post med "x". Editlänken funkar inte för tillfället.</p>
    </section>
</article>
EOD;


// -----------------------------------------------------------------------------
//
// Create the html-page
//

$title         "Car";
$charset     "UTF-8";
$style         "stilmall.css";

// Här sköts sidbygget och utskriften.
include_once("page.php");
?>